@opendatalabs/vana-sdk 3.3.0 → 3.4.1-canary.ba2cfd7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.d.ts +5 -3
- package/dist/index.browser.js +359 -75
- package/dist/index.browser.js.map +3 -3
- package/dist/index.node.cjs +375 -76
- package/dist/index.node.cjs.map +4 -4
- package/dist/index.node.d.ts +5 -3
- package/dist/index.node.js +359 -75
- package/dist/index.node.js.map +3 -3
- package/dist/protocol/eip712.cjs +58 -3
- package/dist/protocol/eip712.cjs.map +1 -1
- package/dist/protocol/eip712.d.ts +98 -6
- package/dist/protocol/eip712.js +52 -3
- package/dist/protocol/eip712.js.map +1 -1
- package/dist/protocol/escrow-deposit.cjs +89 -0
- package/dist/protocol/escrow-deposit.cjs.map +1 -0
- package/dist/protocol/escrow-deposit.d.ts +47 -0
- package/dist/protocol/escrow-deposit.js +60 -0
- package/dist/protocol/escrow-deposit.js.map +1 -0
- package/dist/protocol/escrow-deposit.test.d.ts +1 -0
- package/dist/protocol/escrow-flow.test.d.ts +21 -0
- package/dist/protocol/fee-registry.cjs +116 -0
- package/dist/protocol/fee-registry.cjs.map +1 -0
- package/dist/protocol/fee-registry.d.ts +151 -0
- package/dist/protocol/fee-registry.js +89 -0
- package/dist/protocol/fee-registry.js.map +1 -0
- package/dist/protocol/fee-registry.test.d.ts +1 -0
- package/dist/protocol/gateway.cjs +136 -9
- package/dist/protocol/gateway.cjs.map +1 -1
- package/dist/protocol/gateway.d.ts +227 -17
- package/dist/protocol/gateway.js +136 -9
- package/dist/protocol/gateway.js.map +1 -1
- package/dist/protocol/grants.cjs +24 -64
- package/dist/protocol/grants.cjs.map +1 -1
- package/dist/protocol/grants.d.ts +6 -13
- package/dist/protocol/grants.js +24 -63
- package/dist/protocol/grants.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
const ESCROW_DEPOSIT_ABI = [
|
|
3
|
+
{
|
|
4
|
+
type: "function",
|
|
5
|
+
name: "depositNative",
|
|
6
|
+
stateMutability: "payable",
|
|
7
|
+
inputs: [{ name: "account", type: "address" }],
|
|
8
|
+
outputs: []
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
type: "function",
|
|
12
|
+
name: "depositToken",
|
|
13
|
+
stateMutability: "nonpayable",
|
|
14
|
+
inputs: [
|
|
15
|
+
{ name: "account", type: "address" },
|
|
16
|
+
{ name: "token", type: "address" },
|
|
17
|
+
{ name: "amount", type: "uint256" }
|
|
18
|
+
],
|
|
19
|
+
outputs: []
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
function escrowContractAddress(config) {
|
|
23
|
+
return config.contracts.dataPortabilityEscrow;
|
|
24
|
+
}
|
|
25
|
+
function encodeDepositNativeData(input) {
|
|
26
|
+
return encodeFunctionData({
|
|
27
|
+
abi: ESCROW_DEPOSIT_ABI,
|
|
28
|
+
functionName: "depositNative",
|
|
29
|
+
args: [input.account]
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function encodeDepositTokenData(input) {
|
|
33
|
+
return encodeFunctionData({
|
|
34
|
+
abi: ESCROW_DEPOSIT_ABI,
|
|
35
|
+
functionName: "depositToken",
|
|
36
|
+
args: [input.account, input.token, input.amount]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function buildDepositNativeRequest(config, input) {
|
|
40
|
+
return {
|
|
41
|
+
to: escrowContractAddress(config),
|
|
42
|
+
data: encodeDepositNativeData({ account: input.account }),
|
|
43
|
+
value: input.amount
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function buildDepositTokenRequest(config, input) {
|
|
47
|
+
return {
|
|
48
|
+
to: escrowContractAddress(config),
|
|
49
|
+
data: encodeDepositTokenData(input)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
ESCROW_DEPOSIT_ABI,
|
|
54
|
+
buildDepositNativeRequest,
|
|
55
|
+
buildDepositTokenRequest,
|
|
56
|
+
encodeDepositNativeData,
|
|
57
|
+
encodeDepositTokenData,
|
|
58
|
+
escrowContractAddress
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=escrow-deposit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/protocol/escrow-deposit.ts"],"sourcesContent":["/**\n * On-chain deposit primitives for the DataPortabilityEscrow contract.\n *\n * The escrow holds the finalized balance that `/v1/escrow/pay` debits against.\n * A payer credits their balance by sending one of two function calls to the\n * escrow contract — depositNative (native VANA, amount via `msg.value`) or\n * depositToken (ERC-20, caller must `approve` the escrow first). The credited\n * `account` need not equal `msg.sender`, so a third party can fund someone\n * else's escrow.\n *\n * Once the on-chain tx lands, call `GatewayClient.submitEscrowDeposit({txHash})`\n * to announce it; the gateway reconciles it into the balance and surfaces it\n * under `getEscrowBalance(account).deposits.finalized`.\n *\n * These helpers are signer/transport-agnostic — they return the raw\n * `{to, data, value?}` request object so callers can feed it to any wallet\n * stack (viem `sendTransaction`/`writeContract`, ethers, wallet-rpc, MPC,\n * Safe transactions, etc.).\n *\n * @category Protocol\n */\nimport { encodeFunctionData } from \"viem\";\nimport type { DataPortabilityGatewayConfig } from \"./eip712\";\n\n// ABI for the two deposit entry points on DataPortabilityEscrow. Same shape\n// the gateway uses to decode pending/mined tx calldata at\n// /v1/escrow/deposit time (data-gateway/lib/escrow.ts:39).\nexport const ESCROW_DEPOSIT_ABI = [\n {\n type: \"function\",\n name: \"depositNative\",\n stateMutability: \"payable\",\n inputs: [{ name: \"account\", type: \"address\" }],\n outputs: [],\n },\n {\n type: \"function\",\n name: \"depositToken\",\n stateMutability: \"nonpayable\",\n inputs: [\n { name: \"account\", type: \"address\" },\n { name: \"token\", type: \"address\" },\n { name: \"amount\", type: \"uint256\" },\n ],\n outputs: [],\n },\n] as const;\n\nexport function escrowContractAddress(\n config: DataPortabilityGatewayConfig,\n): `0x${string}` {\n return config.contracts.dataPortabilityEscrow as `0x${string}`;\n}\n\nexport interface DepositNativeInput {\n // Address credited inside the escrow. Often the same as the wallet sending\n // the tx, but third-party funding is supported.\n account: `0x${string}`;\n amount: bigint;\n}\n\nexport interface DepositTokenInput {\n account: `0x${string}`;\n // ERC-20 contract address.\n token: `0x${string}`;\n amount: bigint;\n}\n\n// Shape compatible with viem's `sendTransaction` / `writeContract` request\n// objects. `value` is omitted on the ERC-20 path because the amount lives in\n// the token contract, not `msg.value`.\nexport interface DepositTransactionRequest {\n to: `0x${string}`;\n data: `0x${string}`;\n value?: bigint;\n}\n\nexport function encodeDepositNativeData(input: {\n account: `0x${string}`;\n}): `0x${string}` {\n return encodeFunctionData({\n abi: ESCROW_DEPOSIT_ABI,\n functionName: \"depositNative\",\n args: [input.account],\n });\n}\n\nexport function encodeDepositTokenData(\n input: DepositTokenInput,\n): `0x${string}` {\n return encodeFunctionData({\n abi: ESCROW_DEPOSIT_ABI,\n functionName: \"depositToken\",\n args: [input.account, input.token, input.amount],\n });\n}\n\n// Build the full tx request for a native-VANA deposit. Feed it straight to\n// `walletClient.sendTransaction({...req, account, chain})`. ERC-20 needs a\n// prior `approve(escrow, amount)` on the token — use viem's built-in\n// `erc20Abi` for that; the SDK doesn't bundle one to avoid the import surface.\nexport function buildDepositNativeRequest(\n config: DataPortabilityGatewayConfig,\n input: DepositNativeInput,\n): DepositTransactionRequest {\n return {\n to: escrowContractAddress(config),\n data: encodeDepositNativeData({ account: input.account }),\n value: input.amount,\n };\n}\n\nexport function buildDepositTokenRequest(\n config: DataPortabilityGatewayConfig,\n input: DepositTokenInput,\n): DepositTransactionRequest {\n return {\n to: escrowContractAddress(config),\n data: encodeDepositTokenData(input),\n };\n}\n"],"mappings":"AAqBA,SAAS,0BAA0B;AAM5B,MAAM,qBAAqB;AAAA,EAChC;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAAA,IACN,iBAAiB;AAAA,IACjB,QAAQ,CAAC,EAAE,MAAM,WAAW,MAAM,UAAU,CAAC;AAAA,IAC7C,SAAS,CAAC;AAAA,EACZ;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAAA,IACN,iBAAiB;AAAA,IACjB,QAAQ;AAAA,MACN,EAAE,MAAM,WAAW,MAAM,UAAU;AAAA,MACnC,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,MACjC,EAAE,MAAM,UAAU,MAAM,UAAU;AAAA,IACpC;AAAA,IACA,SAAS,CAAC;AAAA,EACZ;AACF;AAEO,SAAS,sBACd,QACe;AACf,SAAO,OAAO,UAAU;AAC1B;AAyBO,SAAS,wBAAwB,OAEtB;AAChB,SAAO,mBAAmB;AAAA,IACxB,KAAK;AAAA,IACL,cAAc;AAAA,IACd,MAAM,CAAC,MAAM,OAAO;AAAA,EACtB,CAAC;AACH;AAEO,SAAS,uBACd,OACe;AACf,SAAO,mBAAmB;AAAA,IACxB,KAAK;AAAA,IACL,cAAc;AAAA,IACd,MAAM,CAAC,MAAM,SAAS,MAAM,OAAO,MAAM,MAAM;AAAA,EACjD,CAAC;AACH;AAMO,SAAS,0BACd,QACA,OAC2B;AAC3B,SAAO;AAAA,IACL,IAAI,sBAAsB,MAAM;AAAA,IAChC,MAAM,wBAAwB,EAAE,SAAS,MAAM,QAAQ,CAAC;AAAA,IACxD,OAAO,MAAM;AAAA,EACf;AACF;AAEO,SAAS,yBACd,QACA,OAC2B;AAC3B,SAAO;AAAA,IACL,IAAI,sBAAsB,MAAM;AAAA,IAChC,MAAM,uBAAuB,KAAK;AAAA,EACpC;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End-to-end test of the deposit + payment flow.
|
|
3
|
+
*
|
|
4
|
+
* The SDK is signer- and transport-agnostic. To prove the helpers compose
|
|
5
|
+
* correctly we wire them through:
|
|
6
|
+
*
|
|
7
|
+
* - A real viem `WalletClient` against an in-memory L1 (a custom RPC
|
|
8
|
+
* transport routes JSON-RPC calls to a Map-backed chain). The wallet
|
|
9
|
+
* genuinely signs an EIP-1559 transaction with the depositNative
|
|
10
|
+
* calldata produced by buildDepositNativeRequest.
|
|
11
|
+
*
|
|
12
|
+
* - A fetch-mocked gateway that decodes that same calldata via
|
|
13
|
+
* ESCROW_DEPOSIT_ABI, credits a balance, and later recovers the
|
|
14
|
+
* GenericPayment EIP-712 signer with viem's recoverTypedDataAddress —
|
|
15
|
+
* same cryptographic checks the real gateway runs.
|
|
16
|
+
*
|
|
17
|
+
* Anything weaker than this (hand-rolled sendTx, hand-rolled sig recovery)
|
|
18
|
+
* would let the SDK's encoder or domain helpers drift silently. Going through
|
|
19
|
+
* viem's real wallet + sign paths is what makes this a useful e2e.
|
|
20
|
+
*/
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var fee_registry_exports = {};
|
|
20
|
+
__export(fee_registry_exports, {
|
|
21
|
+
FEE_REGISTRY_ABI: () => FEE_REGISTRY_ABI,
|
|
22
|
+
REGISTRATION_KIND_FOR_OP: () => REGISTRATION_KIND_FOR_OP,
|
|
23
|
+
getFee: () => getFee,
|
|
24
|
+
getOpFee: () => getOpFee
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(fee_registry_exports);
|
|
27
|
+
var import_viem = require("viem");
|
|
28
|
+
const FEE_REGISTRY_ABI = (0, import_viem.parseAbi)([
|
|
29
|
+
"struct Fee { uint256 amount; address asset; address payee; bool enabled; }",
|
|
30
|
+
"function fees(bytes32 operation) view returns (Fee)",
|
|
31
|
+
"function operationKey(string name) pure returns (bytes32)"
|
|
32
|
+
]);
|
|
33
|
+
const REGISTRATION_KIND_FOR_OP = {
|
|
34
|
+
grant: "grant_registration",
|
|
35
|
+
data: "data_registration",
|
|
36
|
+
server: "server_registration",
|
|
37
|
+
builder: "builder_registration"
|
|
38
|
+
};
|
|
39
|
+
function operationNameFor(kind, opts) {
|
|
40
|
+
switch (kind) {
|
|
41
|
+
case "grant_registration":
|
|
42
|
+
return opts?.grantRegistrationOpName ?? "grant_registration";
|
|
43
|
+
case "data_access":
|
|
44
|
+
return opts?.dataAccessOpName ?? "data_access";
|
|
45
|
+
case "data_registration":
|
|
46
|
+
return opts?.dataRegistrationOpName ?? "data_registration";
|
|
47
|
+
case "server_registration":
|
|
48
|
+
return opts?.serverRegistrationOpName ?? "server_registration";
|
|
49
|
+
case "builder_registration":
|
|
50
|
+
return opts?.builderRegistrationOpName ?? "builder_registration";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
54
|
+
async function getFee(client, config, kind, opts) {
|
|
55
|
+
const address = config.contracts.feeRegistry;
|
|
56
|
+
const opName = operationNameFor(kind, opts);
|
|
57
|
+
const opKey = await client.readContract({
|
|
58
|
+
address,
|
|
59
|
+
abi: FEE_REGISTRY_ABI,
|
|
60
|
+
functionName: "operationKey",
|
|
61
|
+
args: [opName]
|
|
62
|
+
});
|
|
63
|
+
const fee = await client.readContract({
|
|
64
|
+
address,
|
|
65
|
+
abi: FEE_REGISTRY_ABI,
|
|
66
|
+
functionName: "fees",
|
|
67
|
+
args: [opKey]
|
|
68
|
+
});
|
|
69
|
+
if (fee.enabled && fee.payee === ZERO_ADDRESS) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`FeeRegistry: enabled operation "${opName}" has zero-address payee \u2014 contract pre-flight rejects payouts to 0x0`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return fee;
|
|
75
|
+
}
|
|
76
|
+
async function getOpFee(client, config, opType, opts) {
|
|
77
|
+
const registrationKind = REGISTRATION_KIND_FOR_OP[opType];
|
|
78
|
+
if (!registrationKind) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`getOpFee: unknown opType "${opType}" \u2014 supported types are ${Object.keys(REGISTRATION_KIND_FOR_OP).join(", ")}`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const includeDataAccess = opType === "grant";
|
|
84
|
+
const [registration, dataAccess] = await Promise.all([
|
|
85
|
+
getFee(client, config, registrationKind, opts),
|
|
86
|
+
includeDataAccess ? getFee(client, config, "data_access", opts) : Promise.resolve({
|
|
87
|
+
amount: 0n,
|
|
88
|
+
asset: ZERO_ADDRESS,
|
|
89
|
+
payee: ZERO_ADDRESS,
|
|
90
|
+
enabled: false
|
|
91
|
+
})
|
|
92
|
+
]);
|
|
93
|
+
if (registration.enabled && dataAccess.enabled && registration.asset.toLowerCase() !== dataAccess.asset.toLowerCase()) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`FeeRegistry asset mismatch for "${opType}": registration=${registration.asset} vs data_access=${dataAccess.asset}. The gateway requires both kinds to settle in the same asset when both are enabled.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const asset = registration.enabled ? registration.asset : dataAccess.enabled ? dataAccess.asset : ZERO_ADDRESS;
|
|
99
|
+
return {
|
|
100
|
+
asset,
|
|
101
|
+
registrationFee: registration.enabled ? registration.amount : 0n,
|
|
102
|
+
dataAccessFee: dataAccess.enabled ? dataAccess.amount : 0n,
|
|
103
|
+
registrationEnabled: registration.enabled,
|
|
104
|
+
dataAccessEnabled: dataAccess.enabled,
|
|
105
|
+
registrationPayee: registration.enabled ? registration.payee : ZERO_ADDRESS,
|
|
106
|
+
dataAccessPayee: dataAccess.enabled ? dataAccess.payee : ZERO_ADDRESS
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
FEE_REGISTRY_ABI,
|
|
112
|
+
REGISTRATION_KIND_FOR_OP,
|
|
113
|
+
getFee,
|
|
114
|
+
getOpFee
|
|
115
|
+
});
|
|
116
|
+
//# sourceMappingURL=fee-registry.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/protocol/fee-registry.ts"],"sourcesContent":["/**\n * Adapter for the on-chain FeeRegistry contract — the source of truth the\n * gateway re-reads on every /v1/escrow/pay to size the payment amount.\n *\n * The registry stores per-operation `{amount, asset, payee, enabled}`\n * records keyed on `keccak256(name)`. SDK consumers (builders sizing\n * payments) MUST read fees from this contract rather than hardcoding\n * them, or the signed `amount` won't match the gateway's expected total\n * and /v1/escrow/pay returns 400.\n *\n * Five operation kinds are wired up; each kind's name matches the\n * corresponding `payments.kind` column value on the gateway:\n *\n * - 'grant_registration' — one-time fee when a grant is first registered\n * (POST /v1/grants).\n * - 'data_access' — per-access surcharge on grant payments\n * (every accessRecord posted against a grant).\n * - 'data_registration' — one-time fee for registering a data point\n * (POST /v1/data → addDataWithSignature).\n * - 'server_registration' — one-time fee for registering a personal server\n * (POST /v1/servers).\n * - 'builder_registration' — one-time fee for registering a builder\n * (POST /v1/builders; gateway-only, no on-chain\n * submission).\n *\n * Disabled or unregistered fees skip enforcement: the corresponding\n * operation settles WITHOUT requiring a payment from escrow. `getFee`\n * surfaces this via `enabled: false` rather than throwing — the gateway\n * itself treats a disabled fee as a steady state, not a misconfig.\n *\n * Deployers can override the on-chain operation strings via\n * `FEE_REGISTRY_<KIND>_OP` env vars on the gateway side; the matching\n * SDK escape hatch is the `opts.<kind>OpName` arguments.\n *\n * This module is signer/transport-agnostic — callers pass in their own\n * `PublicClient` for the contract reads. No caching here; long-running\n * service consumers should wrap with their own TTL. Mirrors\n * data-gateway/lib/fee-registry.ts byte-for-byte.\n *\n * @category Protocol\n */\nimport { parseAbi, type Address, type Hex, type PublicClient } from \"viem\";\nimport type { DataPortabilityGatewayConfig } from \"./eip712\";\n\nexport const FEE_REGISTRY_ABI = parseAbi([\n \"struct Fee { uint256 amount; address asset; address payee; bool enabled; }\",\n \"function fees(bytes32 operation) view returns (Fee)\",\n \"function operationKey(string name) pure returns (bytes32)\",\n]);\n\nexport type FeeKind =\n | \"grant_registration\"\n | \"data_access\"\n | \"data_registration\"\n | \"server_registration\"\n | \"builder_registration\";\n\n/**\n * Map from a user-facing opType (POST /v1/escrow/pay body field, matches\n * the gateway's `payments.op_type` column) to the FeeKind that gates its\n * one-time registration fee.\n *\n * Data access is a per-call surcharge on grants only — it's not a\n * registration fee for any op, so it lives outside this map.\n */\nexport const REGISTRATION_KIND_FOR_OP: Record<string, FeeKind> = {\n grant: \"grant_registration\",\n data: \"data_registration\",\n server: \"server_registration\",\n builder: \"builder_registration\",\n};\n\nexport interface FeeEntry {\n amount: bigint;\n // Asset the fee is denominated in. 0x0000…0000 = native VANA; anything\n // else is an ERC-20 contract address.\n asset: Address;\n // The recipient the on-chain settle pass routes the fee to. Only\n // meaningful when `enabled` — disabled fees never land as a SettleOp `to`.\n payee: Address;\n enabled: boolean;\n}\n\n/**\n * Compound fee schedule for one op type, mirroring the gateway's\n * lib/op-fees.ts `OpFee`. For ANY op type, `registrationFee` is the\n * one-time fee charged at registration. For `'grant'` only, `dataAccessFee`\n * is the per-access surcharge — for any other op type it's always 0n with\n * `dataAccessEnabled: false`.\n *\n * `xxxEnabled` reflects the on-chain `Fee.enabled` flag. When OFF, the\n * corresponding amount is 0 and the pay handler should NOT require\n * payment for that kind. When both are off (for a grant) or registration\n * is off (for any other op type), the entire payment flow is skipped —\n * the op settles directly via the no-payment path.\n */\nexport interface OpFee {\n // Asset for whichever components are enabled. Falls back to native VANA\n // (0x0) when both components are disabled. The pay handler enforces that\n // the payer's `asset` matches.\n asset: Address;\n registrationFee: bigint;\n dataAccessFee: bigint;\n registrationEnabled: boolean;\n dataAccessEnabled: boolean;\n // Surfaced for the SDK's on-chain log-filter use case; the gateway's\n // OpFee type doesn't include these but the SDK keeps them since callers\n // sizing on-chain assertions need to know where the fee lands. Equal to\n // the zero address when the corresponding kind is disabled.\n registrationPayee: Address;\n dataAccessPayee: Address;\n}\n\nexport interface FeeRegistryOptions {\n grantRegistrationOpName?: string;\n dataAccessOpName?: string;\n dataRegistrationOpName?: string;\n serverRegistrationOpName?: string;\n builderRegistrationOpName?: string;\n}\n\nfunction operationNameFor(\n kind: FeeKind,\n opts: FeeRegistryOptions | undefined,\n): string {\n switch (kind) {\n case \"grant_registration\":\n return opts?.grantRegistrationOpName ?? \"grant_registration\";\n case \"data_access\":\n return opts?.dataAccessOpName ?? \"data_access\";\n case \"data_registration\":\n return opts?.dataRegistrationOpName ?? \"data_registration\";\n case \"server_registration\":\n return opts?.serverRegistrationOpName ?? \"server_registration\";\n case \"builder_registration\":\n return opts?.builderRegistrationOpName ?? \"builder_registration\";\n }\n}\n\nconst ZERO_ADDRESS = \"0x0000000000000000000000000000000000000000\" as Address;\n\n/**\n * Reads one fee kind from the FeeRegistry. Calls the contract's\n * `operationKey(name)` first to derive the bytes32 key — matches the\n * gateway's approach exactly (could compute locally via keccak256, but\n * going through the contract eliminates any chance of encoding drift).\n *\n * Returns `{enabled: false}` entries WITHOUT throwing — disabled is a\n * valid steady state on the gateway. The only validation is the\n * zero-payee check, and that only fires when the fee is enabled\n * (a disabled fee never lands as a SettleOp `to`).\n */\nexport async function getFee(\n client: PublicClient,\n config: DataPortabilityGatewayConfig,\n kind: FeeKind,\n opts?: FeeRegistryOptions,\n): Promise<FeeEntry> {\n const address = config.contracts.feeRegistry as Address;\n const opName = operationNameFor(kind, opts);\n\n const opKey = (await client.readContract({\n address,\n abi: FEE_REGISTRY_ABI,\n functionName: \"operationKey\",\n args: [opName],\n })) as Hex;\n\n const fee = (await client.readContract({\n address,\n abi: FEE_REGISTRY_ABI,\n functionName: \"fees\",\n args: [opKey],\n })) as FeeEntry;\n\n if (fee.enabled && fee.payee === ZERO_ADDRESS) {\n throw new Error(\n `FeeRegistry: enabled operation \"${opName}\" has zero-address payee — contract pre-flight rejects payouts to 0x0`,\n );\n }\n\n return fee;\n}\n\n/**\n * Convenience: combine the FeeRegistry reads for one op type into the\n * compound shape the pay handler validates against.\n *\n * For 'grant' opType the result includes both registration + data_access\n * components; for other op types data_access is always disabled with\n * amount=0. Disabled components contribute 0 to the signed total —\n * callers compute `amount = registrationFee + dataAccessFee` and the pay\n * handler accepts (or short-circuits with 'Payment not required' when\n * both are 0).\n *\n * Throws on asset mismatch ONLY when both components are enabled — a\n * disabled fee never lands as a SettleOp, so its asset is moot.\n */\nexport async function getOpFee(\n client: PublicClient,\n config: DataPortabilityGatewayConfig,\n opType: string,\n opts?: FeeRegistryOptions,\n): Promise<OpFee> {\n const registrationKind = REGISTRATION_KIND_FOR_OP[opType];\n if (!registrationKind) {\n throw new Error(\n `getOpFee: unknown opType \"${opType}\" — supported types are ${Object.keys(REGISTRATION_KIND_FOR_OP).join(\", \")}`,\n );\n }\n\n const includeDataAccess = opType === \"grant\";\n const [registration, dataAccess] = await Promise.all([\n getFee(client, config, registrationKind, opts),\n includeDataAccess\n ? getFee(client, config, \"data_access\", opts)\n : Promise.resolve<FeeEntry>({\n amount: 0n,\n asset: ZERO_ADDRESS,\n payee: ZERO_ADDRESS,\n enabled: false,\n }),\n ]);\n\n if (\n registration.enabled &&\n dataAccess.enabled &&\n registration.asset.toLowerCase() !== dataAccess.asset.toLowerCase()\n ) {\n throw new Error(\n `FeeRegistry asset mismatch for \"${opType}\": registration=${registration.asset} vs data_access=${dataAccess.asset}. The gateway requires both kinds to settle in the same asset when both are enabled.`,\n );\n }\n\n const asset = registration.enabled\n ? registration.asset\n : dataAccess.enabled\n ? dataAccess.asset\n : ZERO_ADDRESS;\n\n return {\n asset,\n registrationFee: registration.enabled ? registration.amount : 0n,\n dataAccessFee: dataAccess.enabled ? dataAccess.amount : 0n,\n registrationEnabled: registration.enabled,\n dataAccessEnabled: dataAccess.enabled,\n registrationPayee: registration.enabled ? registration.payee : ZERO_ADDRESS,\n dataAccessPayee: dataAccess.enabled ? dataAccess.payee : ZERO_ADDRESS,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAyCA,kBAAoE;AAG7D,MAAM,uBAAmB,sBAAS;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAiBM,MAAM,2BAAoD;AAAA,EAC/D,OAAO;AAAA,EACP,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,SAAS;AACX;AAmDA,SAAS,iBACP,MACA,MACQ;AACR,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,aAAO,MAAM,2BAA2B;AAAA,IAC1C,KAAK;AACH,aAAO,MAAM,oBAAoB;AAAA,IACnC,KAAK;AACH,aAAO,MAAM,0BAA0B;AAAA,IACzC,KAAK;AACH,aAAO,MAAM,4BAA4B;AAAA,IAC3C,KAAK;AACH,aAAO,MAAM,6BAA6B;AAAA,EAC9C;AACF;AAEA,MAAM,eAAe;AAarB,eAAsB,OACpB,QACA,QACA,MACA,MACmB;AACnB,QAAM,UAAU,OAAO,UAAU;AACjC,QAAM,SAAS,iBAAiB,MAAM,IAAI;AAE1C,QAAM,QAAS,MAAM,OAAO,aAAa;AAAA,IACvC;AAAA,IACA,KAAK;AAAA,IACL,cAAc;AAAA,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AAA,IAC9B,mBAAmB,aAAa,UAAU,aAAa,QAAQ;AAAA,IAC/D,iBAAiB,WAAW,UAAU,WAAW,QAAQ;AAAA,EAC3D;AACF;","names":[]}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter for the on-chain FeeRegistry contract — the source of truth the
|
|
3
|
+
* gateway re-reads on every /v1/escrow/pay to size the payment amount.
|
|
4
|
+
*
|
|
5
|
+
* The registry stores per-operation `{amount, asset, payee, enabled}`
|
|
6
|
+
* records keyed on `keccak256(name)`. SDK consumers (builders sizing
|
|
7
|
+
* payments) MUST read fees from this contract rather than hardcoding
|
|
8
|
+
* them, or the signed `amount` won't match the gateway's expected total
|
|
9
|
+
* and /v1/escrow/pay returns 400.
|
|
10
|
+
*
|
|
11
|
+
* Five operation kinds are wired up; each kind's name matches the
|
|
12
|
+
* corresponding `payments.kind` column value on the gateway:
|
|
13
|
+
*
|
|
14
|
+
* - 'grant_registration' — one-time fee when a grant is first registered
|
|
15
|
+
* (POST /v1/grants).
|
|
16
|
+
* - 'data_access' — per-access surcharge on grant payments
|
|
17
|
+
* (every accessRecord posted against a grant).
|
|
18
|
+
* - 'data_registration' — one-time fee for registering a data point
|
|
19
|
+
* (POST /v1/data → addDataWithSignature).
|
|
20
|
+
* - 'server_registration' — one-time fee for registering a personal server
|
|
21
|
+
* (POST /v1/servers).
|
|
22
|
+
* - 'builder_registration' — one-time fee for registering a builder
|
|
23
|
+
* (POST /v1/builders; gateway-only, no on-chain
|
|
24
|
+
* submission).
|
|
25
|
+
*
|
|
26
|
+
* Disabled or unregistered fees skip enforcement: the corresponding
|
|
27
|
+
* operation settles WITHOUT requiring a payment from escrow. `getFee`
|
|
28
|
+
* surfaces this via `enabled: false` rather than throwing — the gateway
|
|
29
|
+
* itself treats a disabled fee as a steady state, not a misconfig.
|
|
30
|
+
*
|
|
31
|
+
* Deployers can override the on-chain operation strings via
|
|
32
|
+
* `FEE_REGISTRY_<KIND>_OP` env vars on the gateway side; the matching
|
|
33
|
+
* SDK escape hatch is the `opts.<kind>OpName` arguments.
|
|
34
|
+
*
|
|
35
|
+
* This module is signer/transport-agnostic — callers pass in their own
|
|
36
|
+
* `PublicClient` for the contract reads. No caching here; long-running
|
|
37
|
+
* service consumers should wrap with their own TTL. Mirrors
|
|
38
|
+
* data-gateway/lib/fee-registry.ts byte-for-byte.
|
|
39
|
+
*
|
|
40
|
+
* @category Protocol
|
|
41
|
+
*/
|
|
42
|
+
import { type Address, type PublicClient } from "viem";
|
|
43
|
+
import type { DataPortabilityGatewayConfig } from "./eip712";
|
|
44
|
+
export declare const FEE_REGISTRY_ABI: readonly [{
|
|
45
|
+
readonly name: "fees";
|
|
46
|
+
readonly type: "function";
|
|
47
|
+
readonly stateMutability: "view";
|
|
48
|
+
readonly inputs: readonly [{
|
|
49
|
+
readonly type: "bytes32";
|
|
50
|
+
readonly name: "operation";
|
|
51
|
+
}];
|
|
52
|
+
readonly outputs: readonly [{
|
|
53
|
+
readonly type: "tuple";
|
|
54
|
+
readonly components: readonly [{
|
|
55
|
+
readonly type: "uint256";
|
|
56
|
+
readonly name: "amount";
|
|
57
|
+
}, {
|
|
58
|
+
readonly type: "address";
|
|
59
|
+
readonly name: "asset";
|
|
60
|
+
}, {
|
|
61
|
+
readonly type: "address";
|
|
62
|
+
readonly name: "payee";
|
|
63
|
+
}, {
|
|
64
|
+
readonly type: "bool";
|
|
65
|
+
readonly name: "enabled";
|
|
66
|
+
}];
|
|
67
|
+
}];
|
|
68
|
+
}, {
|
|
69
|
+
readonly name: "operationKey";
|
|
70
|
+
readonly type: "function";
|
|
71
|
+
readonly stateMutability: "pure";
|
|
72
|
+
readonly inputs: readonly [{
|
|
73
|
+
readonly type: "string";
|
|
74
|
+
readonly name: "name";
|
|
75
|
+
}];
|
|
76
|
+
readonly outputs: readonly [{
|
|
77
|
+
readonly type: "bytes32";
|
|
78
|
+
}];
|
|
79
|
+
}];
|
|
80
|
+
export type FeeKind = "grant_registration" | "data_access" | "data_registration" | "server_registration" | "builder_registration";
|
|
81
|
+
/**
|
|
82
|
+
* Map from a user-facing opType (POST /v1/escrow/pay body field, matches
|
|
83
|
+
* the gateway's `payments.op_type` column) to the FeeKind that gates its
|
|
84
|
+
* one-time registration fee.
|
|
85
|
+
*
|
|
86
|
+
* Data access is a per-call surcharge on grants only — it's not a
|
|
87
|
+
* registration fee for any op, so it lives outside this map.
|
|
88
|
+
*/
|
|
89
|
+
export declare const REGISTRATION_KIND_FOR_OP: Record<string, FeeKind>;
|
|
90
|
+
export interface FeeEntry {
|
|
91
|
+
amount: bigint;
|
|
92
|
+
asset: Address;
|
|
93
|
+
payee: Address;
|
|
94
|
+
enabled: boolean;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Compound fee schedule for one op type, mirroring the gateway's
|
|
98
|
+
* lib/op-fees.ts `OpFee`. For ANY op type, `registrationFee` is the
|
|
99
|
+
* one-time fee charged at registration. For `'grant'` only, `dataAccessFee`
|
|
100
|
+
* is the per-access surcharge — for any other op type it's always 0n with
|
|
101
|
+
* `dataAccessEnabled: false`.
|
|
102
|
+
*
|
|
103
|
+
* `xxxEnabled` reflects the on-chain `Fee.enabled` flag. When OFF, the
|
|
104
|
+
* corresponding amount is 0 and the pay handler should NOT require
|
|
105
|
+
* payment for that kind. When both are off (for a grant) or registration
|
|
106
|
+
* is off (for any other op type), the entire payment flow is skipped —
|
|
107
|
+
* the op settles directly via the no-payment path.
|
|
108
|
+
*/
|
|
109
|
+
export interface OpFee {
|
|
110
|
+
asset: Address;
|
|
111
|
+
registrationFee: bigint;
|
|
112
|
+
dataAccessFee: bigint;
|
|
113
|
+
registrationEnabled: boolean;
|
|
114
|
+
dataAccessEnabled: boolean;
|
|
115
|
+
registrationPayee: Address;
|
|
116
|
+
dataAccessPayee: Address;
|
|
117
|
+
}
|
|
118
|
+
export interface FeeRegistryOptions {
|
|
119
|
+
grantRegistrationOpName?: string;
|
|
120
|
+
dataAccessOpName?: string;
|
|
121
|
+
dataRegistrationOpName?: string;
|
|
122
|
+
serverRegistrationOpName?: string;
|
|
123
|
+
builderRegistrationOpName?: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Reads one fee kind from the FeeRegistry. Calls the contract's
|
|
127
|
+
* `operationKey(name)` first to derive the bytes32 key — matches the
|
|
128
|
+
* gateway's approach exactly (could compute locally via keccak256, but
|
|
129
|
+
* going through the contract eliminates any chance of encoding drift).
|
|
130
|
+
*
|
|
131
|
+
* Returns `{enabled: false}` entries WITHOUT throwing — disabled is a
|
|
132
|
+
* valid steady state on the gateway. The only validation is the
|
|
133
|
+
* zero-payee check, and that only fires when the fee is enabled
|
|
134
|
+
* (a disabled fee never lands as a SettleOp `to`).
|
|
135
|
+
*/
|
|
136
|
+
export declare function getFee(client: PublicClient, config: DataPortabilityGatewayConfig, kind: FeeKind, opts?: FeeRegistryOptions): Promise<FeeEntry>;
|
|
137
|
+
/**
|
|
138
|
+
* Convenience: combine the FeeRegistry reads for one op type into the
|
|
139
|
+
* compound shape the pay handler validates against.
|
|
140
|
+
*
|
|
141
|
+
* For 'grant' opType the result includes both registration + data_access
|
|
142
|
+
* components; for other op types data_access is always disabled with
|
|
143
|
+
* amount=0. Disabled components contribute 0 to the signed total —
|
|
144
|
+
* callers compute `amount = registrationFee + dataAccessFee` and the pay
|
|
145
|
+
* handler accepts (or short-circuits with 'Payment not required' when
|
|
146
|
+
* both are 0).
|
|
147
|
+
*
|
|
148
|
+
* Throws on asset mismatch ONLY when both components are enabled — a
|
|
149
|
+
* disabled fee never lands as a SettleOp, so its asset is moot.
|
|
150
|
+
*/
|
|
151
|
+
export declare function getOpFee(client: PublicClient, config: DataPortabilityGatewayConfig, opType: string, opts?: FeeRegistryOptions): Promise<OpFee>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { parseAbi } from "viem";
|
|
2
|
+
const FEE_REGISTRY_ABI = parseAbi([
|
|
3
|
+
"struct Fee { uint256 amount; address asset; address payee; bool enabled; }",
|
|
4
|
+
"function fees(bytes32 operation) view returns (Fee)",
|
|
5
|
+
"function operationKey(string name) pure returns (bytes32)"
|
|
6
|
+
]);
|
|
7
|
+
const REGISTRATION_KIND_FOR_OP = {
|
|
8
|
+
grant: "grant_registration",
|
|
9
|
+
data: "data_registration",
|
|
10
|
+
server: "server_registration",
|
|
11
|
+
builder: "builder_registration"
|
|
12
|
+
};
|
|
13
|
+
function operationNameFor(kind, opts) {
|
|
14
|
+
switch (kind) {
|
|
15
|
+
case "grant_registration":
|
|
16
|
+
return opts?.grantRegistrationOpName ?? "grant_registration";
|
|
17
|
+
case "data_access":
|
|
18
|
+
return opts?.dataAccessOpName ?? "data_access";
|
|
19
|
+
case "data_registration":
|
|
20
|
+
return opts?.dataRegistrationOpName ?? "data_registration";
|
|
21
|
+
case "server_registration":
|
|
22
|
+
return opts?.serverRegistrationOpName ?? "server_registration";
|
|
23
|
+
case "builder_registration":
|
|
24
|
+
return opts?.builderRegistrationOpName ?? "builder_registration";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
28
|
+
async function getFee(client, config, kind, opts) {
|
|
29
|
+
const address = config.contracts.feeRegistry;
|
|
30
|
+
const opName = operationNameFor(kind, opts);
|
|
31
|
+
const opKey = await client.readContract({
|
|
32
|
+
address,
|
|
33
|
+
abi: FEE_REGISTRY_ABI,
|
|
34
|
+
functionName: "operationKey",
|
|
35
|
+
args: [opName]
|
|
36
|
+
});
|
|
37
|
+
const fee = await client.readContract({
|
|
38
|
+
address,
|
|
39
|
+
abi: FEE_REGISTRY_ABI,
|
|
40
|
+
functionName: "fees",
|
|
41
|
+
args: [opKey]
|
|
42
|
+
});
|
|
43
|
+
if (fee.enabled && fee.payee === ZERO_ADDRESS) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`FeeRegistry: enabled operation "${opName}" has zero-address payee \u2014 contract pre-flight rejects payouts to 0x0`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return fee;
|
|
49
|
+
}
|
|
50
|
+
async function getOpFee(client, config, opType, opts) {
|
|
51
|
+
const registrationKind = REGISTRATION_KIND_FOR_OP[opType];
|
|
52
|
+
if (!registrationKind) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`getOpFee: unknown opType "${opType}" \u2014 supported types are ${Object.keys(REGISTRATION_KIND_FOR_OP).join(", ")}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const includeDataAccess = opType === "grant";
|
|
58
|
+
const [registration, dataAccess] = await Promise.all([
|
|
59
|
+
getFee(client, config, registrationKind, opts),
|
|
60
|
+
includeDataAccess ? getFee(client, config, "data_access", opts) : Promise.resolve({
|
|
61
|
+
amount: 0n,
|
|
62
|
+
asset: ZERO_ADDRESS,
|
|
63
|
+
payee: ZERO_ADDRESS,
|
|
64
|
+
enabled: false
|
|
65
|
+
})
|
|
66
|
+
]);
|
|
67
|
+
if (registration.enabled && dataAccess.enabled && registration.asset.toLowerCase() !== dataAccess.asset.toLowerCase()) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`FeeRegistry asset mismatch for "${opType}": registration=${registration.asset} vs data_access=${dataAccess.asset}. The gateway requires both kinds to settle in the same asset when both are enabled.`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const asset = registration.enabled ? registration.asset : dataAccess.enabled ? dataAccess.asset : ZERO_ADDRESS;
|
|
73
|
+
return {
|
|
74
|
+
asset,
|
|
75
|
+
registrationFee: registration.enabled ? registration.amount : 0n,
|
|
76
|
+
dataAccessFee: dataAccess.enabled ? dataAccess.amount : 0n,
|
|
77
|
+
registrationEnabled: registration.enabled,
|
|
78
|
+
dataAccessEnabled: dataAccess.enabled,
|
|
79
|
+
registrationPayee: registration.enabled ? registration.payee : ZERO_ADDRESS,
|
|
80
|
+
dataAccessPayee: dataAccess.enabled ? dataAccess.payee : ZERO_ADDRESS
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
FEE_REGISTRY_ABI,
|
|
85
|
+
REGISTRATION_KIND_FOR_OP,
|
|
86
|
+
getFee,
|
|
87
|
+
getOpFee
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=fee-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/protocol/fee-registry.ts"],"sourcesContent":["/**\n * Adapter for the on-chain FeeRegistry contract — the source of truth the\n * gateway re-reads on every /v1/escrow/pay to size the payment amount.\n *\n * The registry stores per-operation `{amount, asset, payee, enabled}`\n * records keyed on `keccak256(name)`. SDK consumers (builders sizing\n * payments) MUST read fees from this contract rather than hardcoding\n * them, or the signed `amount` won't match the gateway's expected total\n * and /v1/escrow/pay returns 400.\n *\n * Five operation kinds are wired up; each kind's name matches the\n * corresponding `payments.kind` column value on the gateway:\n *\n * - 'grant_registration' — one-time fee when a grant is first registered\n * (POST /v1/grants).\n * - 'data_access' — per-access surcharge on grant payments\n * (every accessRecord posted against a grant).\n * - 'data_registration' — one-time fee for registering a data point\n * (POST /v1/data → addDataWithSignature).\n * - 'server_registration' — one-time fee for registering a personal server\n * (POST /v1/servers).\n * - 'builder_registration' — one-time fee for registering a builder\n * (POST /v1/builders; gateway-only, no on-chain\n * submission).\n *\n * Disabled or unregistered fees skip enforcement: the corresponding\n * operation settles WITHOUT requiring a payment from escrow. `getFee`\n * surfaces this via `enabled: false` rather than throwing — the gateway\n * itself treats a disabled fee as a steady state, not a misconfig.\n *\n * Deployers can override the on-chain operation strings via\n * `FEE_REGISTRY_<KIND>_OP` env vars on the gateway side; the matching\n * SDK escape hatch is the `opts.<kind>OpName` arguments.\n *\n * This module is signer/transport-agnostic — callers pass in their own\n * `PublicClient` for the contract reads. No caching here; long-running\n * service consumers should wrap with their own TTL. Mirrors\n * data-gateway/lib/fee-registry.ts byte-for-byte.\n *\n * @category Protocol\n */\nimport { parseAbi, type Address, type Hex, type PublicClient } from \"viem\";\nimport type { DataPortabilityGatewayConfig } from \"./eip712\";\n\nexport const FEE_REGISTRY_ABI = parseAbi([\n \"struct Fee { uint256 amount; address asset; address payee; bool enabled; }\",\n \"function fees(bytes32 operation) view returns (Fee)\",\n \"function operationKey(string name) pure returns (bytes32)\",\n]);\n\nexport type FeeKind =\n | \"grant_registration\"\n | \"data_access\"\n | \"data_registration\"\n | \"server_registration\"\n | \"builder_registration\";\n\n/**\n * Map from a user-facing opType (POST /v1/escrow/pay body field, matches\n * the gateway's `payments.op_type` column) to the FeeKind that gates its\n * one-time registration fee.\n *\n * Data access is a per-call surcharge on grants only — it's not a\n * registration fee for any op, so it lives outside this map.\n */\nexport const REGISTRATION_KIND_FOR_OP: Record<string, FeeKind> = {\n grant: \"grant_registration\",\n data: \"data_registration\",\n server: \"server_registration\",\n builder: \"builder_registration\",\n};\n\nexport interface FeeEntry {\n amount: bigint;\n // Asset the fee is denominated in. 0x0000…0000 = native VANA; anything\n // else is an ERC-20 contract address.\n asset: Address;\n // The recipient the on-chain settle pass routes the fee to. Only\n // meaningful when `enabled` — disabled fees never land as a SettleOp `to`.\n payee: Address;\n enabled: boolean;\n}\n\n/**\n * Compound fee schedule for one op type, mirroring the gateway's\n * lib/op-fees.ts `OpFee`. For ANY op type, `registrationFee` is the\n * one-time fee charged at registration. For `'grant'` only, `dataAccessFee`\n * is the per-access surcharge — for any other op type it's always 0n with\n * `dataAccessEnabled: false`.\n *\n * `xxxEnabled` reflects the on-chain `Fee.enabled` flag. When OFF, the\n * corresponding amount is 0 and the pay handler should NOT require\n * payment for that kind. When both are off (for a grant) or registration\n * is off (for any other op type), the entire payment flow is skipped —\n * the op settles directly via the no-payment path.\n */\nexport interface OpFee {\n // Asset for whichever components are enabled. Falls back to native VANA\n // (0x0) when both components are disabled. The pay handler enforces that\n // the payer's `asset` matches.\n asset: Address;\n registrationFee: bigint;\n dataAccessFee: bigint;\n registrationEnabled: boolean;\n dataAccessEnabled: boolean;\n // Surfaced for the SDK's on-chain log-filter use case; the gateway's\n // OpFee type doesn't include these but the SDK keeps them since callers\n // sizing on-chain assertions need to know where the fee lands. Equal to\n // the zero address when the corresponding kind is disabled.\n registrationPayee: Address;\n dataAccessPayee: Address;\n}\n\nexport interface FeeRegistryOptions {\n grantRegistrationOpName?: string;\n dataAccessOpName?: string;\n dataRegistrationOpName?: string;\n serverRegistrationOpName?: string;\n builderRegistrationOpName?: string;\n}\n\nfunction operationNameFor(\n kind: FeeKind,\n opts: FeeRegistryOptions | undefined,\n): string {\n switch (kind) {\n case \"grant_registration\":\n return opts?.grantRegistrationOpName ?? \"grant_registration\";\n case \"data_access\":\n return opts?.dataAccessOpName ?? \"data_access\";\n case \"data_registration\":\n return opts?.dataRegistrationOpName ?? \"data_registration\";\n case \"server_registration\":\n return opts?.serverRegistrationOpName ?? \"server_registration\";\n case \"builder_registration\":\n return opts?.builderRegistrationOpName ?? \"builder_registration\";\n }\n}\n\nconst ZERO_ADDRESS = \"0x0000000000000000000000000000000000000000\" as Address;\n\n/**\n * Reads one fee kind from the FeeRegistry. Calls the contract's\n * `operationKey(name)` first to derive the bytes32 key — matches the\n * gateway's approach exactly (could compute locally via keccak256, but\n * going through the contract eliminates any chance of encoding drift).\n *\n * Returns `{enabled: false}` entries WITHOUT throwing — disabled is a\n * valid steady state on the gateway. The only validation is the\n * zero-payee check, and that only fires when the fee is enabled\n * (a disabled fee never lands as a SettleOp `to`).\n */\nexport async function getFee(\n client: PublicClient,\n config: DataPortabilityGatewayConfig,\n kind: FeeKind,\n opts?: FeeRegistryOptions,\n): Promise<FeeEntry> {\n const address = config.contracts.feeRegistry as Address;\n const opName = operationNameFor(kind, opts);\n\n const opKey = (await client.readContract({\n address,\n abi: FEE_REGISTRY_ABI,\n functionName: \"operationKey\",\n args: [opName],\n })) as Hex;\n\n const fee = (await client.readContract({\n address,\n abi: FEE_REGISTRY_ABI,\n functionName: \"fees\",\n args: [opKey],\n })) as FeeEntry;\n\n if (fee.enabled && fee.payee === ZERO_ADDRESS) {\n throw new Error(\n `FeeRegistry: enabled operation \"${opName}\" has zero-address payee — contract pre-flight rejects payouts to 0x0`,\n );\n }\n\n return fee;\n}\n\n/**\n * Convenience: combine the FeeRegistry reads for one op type into the\n * compound shape the pay handler validates against.\n *\n * For 'grant' opType the result includes both registration + data_access\n * components; for other op types data_access is always disabled with\n * amount=0. Disabled components contribute 0 to the signed total —\n * callers compute `amount = registrationFee + dataAccessFee` and the pay\n * handler accepts (or short-circuits with 'Payment not required' when\n * both are 0).\n *\n * Throws on asset mismatch ONLY when both components are enabled — a\n * disabled fee never lands as a SettleOp, so its asset is moot.\n */\nexport async function getOpFee(\n client: PublicClient,\n config: DataPortabilityGatewayConfig,\n opType: string,\n opts?: FeeRegistryOptions,\n): Promise<OpFee> {\n const registrationKind = REGISTRATION_KIND_FOR_OP[opType];\n if (!registrationKind) {\n throw new Error(\n `getOpFee: unknown opType \"${opType}\" — supported types are ${Object.keys(REGISTRATION_KIND_FOR_OP).join(\", \")}`,\n );\n }\n\n const includeDataAccess = opType === \"grant\";\n const [registration, dataAccess] = await Promise.all([\n getFee(client, config, registrationKind, opts),\n includeDataAccess\n ? getFee(client, config, \"data_access\", opts)\n : Promise.resolve<FeeEntry>({\n amount: 0n,\n asset: ZERO_ADDRESS,\n payee: ZERO_ADDRESS,\n enabled: false,\n }),\n ]);\n\n if (\n registration.enabled &&\n dataAccess.enabled &&\n registration.asset.toLowerCase() !== dataAccess.asset.toLowerCase()\n ) {\n throw new Error(\n `FeeRegistry asset mismatch for \"${opType}\": registration=${registration.asset} vs data_access=${dataAccess.asset}. The gateway requires both kinds to settle in the same asset when both are enabled.`,\n );\n }\n\n const asset = registration.enabled\n ? registration.asset\n : dataAccess.enabled\n ? dataAccess.asset\n : ZERO_ADDRESS;\n\n return {\n asset,\n registrationFee: registration.enabled ? registration.amount : 0n,\n dataAccessFee: dataAccess.enabled ? dataAccess.amount : 0n,\n registrationEnabled: registration.enabled,\n dataAccessEnabled: dataAccess.enabled,\n registrationPayee: registration.enabled ? registration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