@integraledger/profile-facilitator 0.4.0 → 0.5.0
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/README.md +642 -456
- package/dist/answers.d.ts +11 -4
- package/dist/answers.js +24 -0
- package/dist/core.d.ts +51 -0
- package/dist/core.js +758 -0
- package/dist/http.d.ts +20 -3
- package/dist/http.js +32 -7
- package/dist/index.d.ts +12 -36
- package/dist/index.js +9 -162
- package/dist/module.d.ts +372 -0
- package/dist/module.js +13 -0
- package/dist/modules/algorand/index.d.ts +22 -0
- package/dist/modules/algorand/index.js +247 -0
- package/dist/modules/algorand/own.d.ts +26 -0
- package/dist/modules/algorand/own.js +231 -0
- package/dist/modules/aptos/index.d.ts +23 -0
- package/dist/modules/aptos/index.js +232 -0
- package/dist/modules/cardano/index.d.ts +18 -0
- package/dist/modules/cardano/index.js +124 -0
- package/dist/modules/casper/index.d.ts +120 -0
- package/dist/modules/casper/index.js +442 -0
- package/dist/modules/concordium/index.d.ts +16 -0
- package/dist/modules/concordium/index.js +77 -0
- package/dist/modules/erc7710/index.d.ts +26 -0
- package/dist/modules/erc7710/index.js +220 -0
- package/dist/modules/evm/forms.d.ts +74 -0
- package/dist/modules/evm/forms.js +182 -0
- package/dist/modules/evm/index.d.ts +40 -0
- package/dist/modules/evm/index.js +148 -0
- package/dist/modules/evm/node.d.ts +29 -0
- package/dist/modules/evm/node.js +222 -0
- package/dist/modules/hedera/executor.d.ts +63 -0
- package/dist/modules/hedera/executor.js +343 -0
- package/dist/modules/hedera/index.d.ts +23 -0
- package/dist/modules/hedera/index.js +136 -0
- package/dist/modules/hedera/mirror.d.ts +32 -0
- package/dist/modules/hedera/mirror.js +111 -0
- package/dist/modules/index.d.ts +23 -0
- package/dist/modules/index.js +24 -0
- package/dist/modules/lnbtc/index.d.ts +10 -0
- package/dist/modules/lnbtc/index.js +111 -0
- package/dist/modules/near/index.d.ts +18 -0
- package/dist/modules/near/index.js +246 -0
- package/dist/modules/polkadot/index.d.ts +12 -0
- package/dist/modules/polkadot/index.js +149 -0
- package/dist/modules/polkadot/rail.d.ts +40 -0
- package/dist/{polkadot.js → modules/polkadot/rail.js} +214 -288
- package/dist/modules/solana/index.d.ts +60 -0
- package/dist/modules/solana/index.js +304 -0
- package/dist/modules/solana/reader.d.ts +25 -0
- package/dist/modules/solana/reader.js +194 -0
- package/dist/modules/solana/state.d.ts +6 -0
- package/dist/modules/solana/state.js +103 -0
- package/dist/modules/starknet/index.d.ts +35 -0
- package/dist/modules/starknet/index.js +541 -0
- package/dist/modules/starknet/node.d.ts +78 -0
- package/dist/modules/starknet/node.js +339 -0
- package/dist/modules/stellar/index.d.ts +44 -0
- package/dist/modules/stellar/index.js +481 -0
- package/dist/modules/stellar/node.d.ts +57 -0
- package/dist/modules/stellar/node.js +151 -0
- package/dist/modules/sui/index.d.ts +16 -0
- package/dist/modules/sui/index.js +309 -0
- package/dist/modules/sui/node.d.ts +49 -0
- package/dist/modules/sui/node.js +197 -0
- package/dist/modules/ton/index.d.ts +16 -0
- package/dist/modules/ton/index.js +73 -0
- package/dist/modules/tron/index.d.ts +12 -0
- package/dist/modules/tron/index.js +120 -0
- package/dist/modules/tron/rail.d.ts +32 -0
- package/dist/{tron.js → modules/tron/rail.js} +206 -195
- package/dist/modules/xrpl/index.d.ts +28 -0
- package/dist/modules/xrpl/index.js +299 -0
- package/dist/modules/xrpl/node.d.ts +52 -0
- package/dist/modules/xrpl/node.js +231 -0
- package/dist/modules/xrpl/upto.d.ts +5 -0
- package/dist/modules/xrpl/upto.js +269 -0
- package/dist/record.d.ts +131 -0
- package/dist/record.js +330 -0
- package/dist/unexpected.d.ts +3 -2
- package/dist/unexpected.js +2 -1
- package/dist/wrapper.d.ts +68 -0
- package/dist/wrapper.js +126 -0
- package/package.json +47 -12
- package/dist/polkadot.d.ts +0 -21
- package/dist/store.d.ts +0 -59
- package/dist/store.js +0 -103
- package/dist/tron.d.ts +0 -12
- /package/dist/{base58check.d.ts → modules/tron/base58check.d.ts} +0 -0
- /package/dist/{base58check.js → modules/tron/base58check.js} +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { AuthCaptureEvmScheme } from "@x402/evm/auth-capture/facilitator";
|
|
2
|
+
import { BatchSettlementEvmScheme } from "@x402/evm/batch-settlement/facilitator";
|
|
3
|
+
import { ExactEvmScheme } from "@x402/evm/exact/facilitator";
|
|
4
|
+
import { UptoEvmScheme } from "@x402/evm/upto/facilitator";
|
|
5
|
+
import { NODE_TIMEOUT_MS } from "../../http.js";
|
|
6
|
+
import { admit, instrumentOf, PAIRINGS, readOf } from "./forms.js";
|
|
7
|
+
import { chargedFrom, evmReader, feeShown } from "./node.js";
|
|
8
|
+
const SCHEMES = ["exact", "upto", "auth-capture", "batch-settlement"];
|
|
9
|
+
/**
|
|
10
|
+
* The gas key as the classes take it, each transaction it sends (`writeContract`, `sendTransaction`) given to the
|
|
11
|
+
* network's fee meter once sent and again once a class's wait for its receipt ends; every other member unchanged.
|
|
12
|
+
*/
|
|
13
|
+
function counted(signer, meter, rpc) {
|
|
14
|
+
const count = (transaction) => meter.sent(transaction, () => feeShown(rpc, transaction, performance.now() + NODE_TIMEOUT_MS));
|
|
15
|
+
return new Proxy(signer, {
|
|
16
|
+
get(inner, member) {
|
|
17
|
+
const value = Reflect.get(inner, member);
|
|
18
|
+
if (typeof value !== "function")
|
|
19
|
+
return value;
|
|
20
|
+
if (member === "writeContract" || member === "sendTransaction") {
|
|
21
|
+
return async (args) => {
|
|
22
|
+
const transaction = (await value.call(inner, args));
|
|
23
|
+
count(transaction);
|
|
24
|
+
return transaction;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (member === "waitForTransactionReceipt") {
|
|
28
|
+
return async (args) => {
|
|
29
|
+
try {
|
|
30
|
+
return await value.call(inner, args);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
count(args.hash);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return value.bind(inner);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** x402's class for `scheme`, given the module's key and configuration, unchanged. */
|
|
42
|
+
function classOf(scheme, c, signer, pendingSettlementStore) {
|
|
43
|
+
const factories = [...(c.eip6492AllowedFactories ?? [])];
|
|
44
|
+
const pending = pendingSettlementStore === undefined ? {} : { pendingSettlementStore };
|
|
45
|
+
switch (scheme) {
|
|
46
|
+
case "exact":
|
|
47
|
+
return new ExactEvmScheme(signer, { eip6492AllowedFactories: factories, ...pending });
|
|
48
|
+
case "upto":
|
|
49
|
+
return new UptoEvmScheme(signer, pending);
|
|
50
|
+
case "auth-capture":
|
|
51
|
+
return new AuthCaptureEvmScheme(signer, c.authCapture);
|
|
52
|
+
case "batch-settlement":
|
|
53
|
+
return new BatchSettlementEvmScheme(signer, undefined, { eip6492AllowedFactories: factories, ...pending });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The payer a payload names, where it names one, for a refusal's answer. */
|
|
57
|
+
function payerOf(payload) {
|
|
58
|
+
const body = payload.payload;
|
|
59
|
+
for (const k of ["authorization", "permit2Authorization"]) {
|
|
60
|
+
const a = body?.[k];
|
|
61
|
+
if (typeof a?.["from"] === "string")
|
|
62
|
+
return a["from"];
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The class behind the profile's pure checks: a payment they refuse is answered with their reason and never reaches
|
|
68
|
+
* the class; every other call is the class's own, its `getExtra` and `getSigners` included.
|
|
69
|
+
*/
|
|
70
|
+
function checked(scheme, inner, pairings) {
|
|
71
|
+
return {
|
|
72
|
+
scheme: inner.scheme,
|
|
73
|
+
caipFamily: inner.caipFamily,
|
|
74
|
+
getExtra: (network) => inner.getExtra(network),
|
|
75
|
+
getSigners: (network) => inner.getSigners(network),
|
|
76
|
+
async verify(payload, requirements, context) {
|
|
77
|
+
const a = await admit(scheme, { payload, requirements }, pairings);
|
|
78
|
+
if ("refused" in a) {
|
|
79
|
+
const payer = payerOf(payload);
|
|
80
|
+
return payer === undefined ? { isValid: false, invalidReason: a.refused } : { isValid: false, invalidReason: a.refused, payer };
|
|
81
|
+
}
|
|
82
|
+
return inner.verify(payload, requirements, context);
|
|
83
|
+
},
|
|
84
|
+
async settle(payload, requirements, context) {
|
|
85
|
+
const a = await admit(scheme, { payload, requirements }, pairings);
|
|
86
|
+
if ("refused" in a)
|
|
87
|
+
return { success: false, errorReason: a.refused, transaction: "", network: requirements.network };
|
|
88
|
+
return inner.settle(payload, requirements, context);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The `evm` module's schemes on one network, each x402's class with what the core needs beside it, the gas key counted
|
|
94
|
+
* by `meter`, the network's fee meter. `pending` is where `exact`, `upto` and `batch-settlement` keep a settlement they
|
|
95
|
+
* sent and the chain does not yet show, so that a retried settle reconciles against it; each class keeps its own in
|
|
96
|
+
* memory where absent.
|
|
97
|
+
*/
|
|
98
|
+
export function evmModules(c, meter, pending) {
|
|
99
|
+
const network = c.network;
|
|
100
|
+
const signer = counted(c.signer, meter, c.rpc);
|
|
101
|
+
return (c.schemes ?? SCHEMES).map((scheme) => {
|
|
102
|
+
const pairings = PAIRINGS[scheme];
|
|
103
|
+
const record = {
|
|
104
|
+
async instrument(p) {
|
|
105
|
+
const a = await admit(scheme, p, pairings);
|
|
106
|
+
return "refused" in a ? a : instrumentOf(network, p, a);
|
|
107
|
+
},
|
|
108
|
+
async read(p, at) {
|
|
109
|
+
const a = await admit(scheme, p, pairings);
|
|
110
|
+
if ("refused" in a)
|
|
111
|
+
return { outcome: "live", answer: { state: "unreadable", reason: "unparseable", foreign: [] } };
|
|
112
|
+
return readOf(evmReader(c.network, c.rpc, at.deadline), a, at);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
descriptor: {
|
|
117
|
+
name: "evm",
|
|
118
|
+
key: "relayer",
|
|
119
|
+
pairings,
|
|
120
|
+
// Permit2 takes no zero (its proxies refuse an amount of 0), and every other EVM class form but EIP-3009's
|
|
121
|
+
// `exact` is unestablished at zero; a kind takes zero only where each of its pairings does.
|
|
122
|
+
acceptsZero: false,
|
|
123
|
+
},
|
|
124
|
+
network,
|
|
125
|
+
facilitator: checked(scheme, classOf(scheme, c, signer, pending), pairings),
|
|
126
|
+
record,
|
|
127
|
+
fees: {
|
|
128
|
+
paysFee: () => true,
|
|
129
|
+
payerBroadcast: () => false,
|
|
130
|
+
charged: chargedFrom(c.rpc),
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* How the core starts the module: the configuration's `evm` member lists the networks, one entry each, and each
|
|
137
|
+
* configured scheme on a network is one registration of x402's class, whose pending settlements are kept in the
|
|
138
|
+
* settler's database. The `erc7710` module offers itself to the `exact` registration made here.
|
|
139
|
+
*/
|
|
140
|
+
export const starter = {
|
|
141
|
+
name: "evm",
|
|
142
|
+
async start(networks, core) {
|
|
143
|
+
const pending = core.state.pendingSettlements();
|
|
144
|
+
return {
|
|
145
|
+
registrations: networks.flatMap((c) => evmModules(c, core.meter(c.network), pending).map((module) => ({ network: module.network, module, own: [] }))),
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seller's EVM node, read over JSON-RPC: lcp's `EvmReader` for the pairings' reads, and the fee of a transaction
|
|
3
|
+
* the module's key sent. Every call is bounded by the node call's own bounds and by the deadline the caller gives.
|
|
4
|
+
*/
|
|
5
|
+
import { type EvmReader, type Hex } from "@integraledger/lcp/evm";
|
|
6
|
+
import type { Eip155 } from "@integraledger/lcp/evm";
|
|
7
|
+
import type { Charged, FeeNeeds } from "../../module.js";
|
|
8
|
+
/** One JSON-RPC call to `rpc` that ends by `deadline`, a `performance.now()` time. */
|
|
9
|
+
export declare function rpcCall(rpc: string, method: string, params: readonly unknown[], deadline: number): Promise<unknown>;
|
|
10
|
+
/** lcp's reader of `network` over the JSON-RPC endpoint `rpc`, each call ending by `deadline`. */
|
|
11
|
+
export declare function evmReader(network: Eip155, rpc: string, deadline: number): EvmReader;
|
|
12
|
+
/**
|
|
13
|
+
* What a transaction the module's key sent costs it, in wei: once the node shows its receipt, the fee it paid, the gas
|
|
14
|
+
* used times the effective gas price, and on an OP Stack chain the L1 data fee the receipt shows (`l1Fee`), which the
|
|
15
|
+
* key pays beside it; until then the most it can pay, its gas limit times its fee cap (or its gas price, for a
|
|
16
|
+
* transaction of a type that has no cap), and the L1 data fee it would pay now. Undefined where the node holds neither.
|
|
17
|
+
*/
|
|
18
|
+
export declare function transactionFee(rpc: string, transaction: Hex, deadline: number): Promise<{
|
|
19
|
+
readonly included: bigint;
|
|
20
|
+
} | {
|
|
21
|
+
readonly bound: bigint;
|
|
22
|
+
} | undefined>;
|
|
23
|
+
/** `transactionFee` as the fee count takes it: what was paid once included, the bound until then. */
|
|
24
|
+
export declare function feeShown(rpc: string, transaction: string, deadline: number): Promise<Charged | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* The fee budget's read of a transaction a class sent, from the module's node: the fee the chain shows once included,
|
|
27
|
+
* its own bound until then. A transaction the node holds neither as sent nor as included rejects.
|
|
28
|
+
*/
|
|
29
|
+
export declare function chargedFrom(rpc: string): NonNullable<FeeNeeds["charged"]>;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seller's EVM node, read over JSON-RPC: lcp's `EvmReader` for the pairings' reads, and the fee of a transaction
|
|
3
|
+
* the module's key sent. Every call is bounded by the node call's own bounds and by the deadline the caller gives.
|
|
4
|
+
*/
|
|
5
|
+
import { ReaderError } from "@integraledger/lcp/evm";
|
|
6
|
+
import { encodeFunctionData, serializeTransaction } from "viem";
|
|
7
|
+
import { NodeError, postJson } from "../../http.js";
|
|
8
|
+
const HEX_QUANTITY = /^0x[0-9a-fA-F]{1,64}$/;
|
|
9
|
+
const HEX_DATA = /^0x([0-9a-fA-F]{2})*$/;
|
|
10
|
+
const HASH = /^0x[0-9a-fA-F]{64}$/;
|
|
11
|
+
const ADDRESS = /^0x[0-9a-fA-F]{40}$/;
|
|
12
|
+
function isObject(v) {
|
|
13
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
14
|
+
}
|
|
15
|
+
function malformed(what) {
|
|
16
|
+
throw new ReaderError("malformed", `the node's ${what} is not one JSON-RPC defines`);
|
|
17
|
+
}
|
|
18
|
+
function quantity(v, what) {
|
|
19
|
+
if (typeof v !== "string" || !HEX_QUANTITY.test(v))
|
|
20
|
+
malformed(what);
|
|
21
|
+
return BigInt(v);
|
|
22
|
+
}
|
|
23
|
+
function data(v, what) {
|
|
24
|
+
if (typeof v !== "string" || !HEX_DATA.test(v))
|
|
25
|
+
malformed(what);
|
|
26
|
+
return v.toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
function hex(v) {
|
|
29
|
+
return `0x${v.toString(16)}`;
|
|
30
|
+
}
|
|
31
|
+
/** A JSON-RPC error the node answered: the range of a log query it will not serve, history it does not hold, or other. */
|
|
32
|
+
function nodeRefused(e) {
|
|
33
|
+
const code = isObject(e) ? e["code"] : undefined;
|
|
34
|
+
const message = isObject(e) && typeof e["message"] === "string" ? e["message"] : "";
|
|
35
|
+
if (code === -32005 || /range|too many|limit|exceed/i.test(message))
|
|
36
|
+
return new ReaderError("too-large", message);
|
|
37
|
+
if (/missing trie node|pruned|history|not available|header not found/i.test(message))
|
|
38
|
+
return new ReaderError("history", message);
|
|
39
|
+
return new ReaderError("transport", message || `JSON-RPC error ${String(code)}`);
|
|
40
|
+
}
|
|
41
|
+
/** One JSON-RPC call to `rpc` that ends by `deadline`, a `performance.now()` time. */
|
|
42
|
+
export async function rpcCall(rpc, method, params, deadline) {
|
|
43
|
+
let answer;
|
|
44
|
+
try {
|
|
45
|
+
answer = await postJson(rpc, { jsonrpc: "2.0", id: 1, method, params }, deadline - performance.now());
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
if (e instanceof NodeError && (e.code === "malformed" || e.code === "too-large")) {
|
|
49
|
+
throw new ReaderError(e.code === "too-large" ? "too-large" : "malformed", e.message);
|
|
50
|
+
}
|
|
51
|
+
throw new ReaderError(e instanceof NodeError && e.code === "timeout" ? "timeout" : "transport", String(e));
|
|
52
|
+
}
|
|
53
|
+
if (!isObject(answer))
|
|
54
|
+
malformed(method);
|
|
55
|
+
if (answer["error"] !== undefined)
|
|
56
|
+
throw nodeRefused(answer["error"]);
|
|
57
|
+
if (!("result" in answer))
|
|
58
|
+
malformed(method);
|
|
59
|
+
return answer["result"];
|
|
60
|
+
}
|
|
61
|
+
function logOf(l, what) {
|
|
62
|
+
if (!isObject(l) || typeof l["address"] !== "string" || !ADDRESS.test(l["address"]) || !Array.isArray(l["topics"])) {
|
|
63
|
+
malformed(what);
|
|
64
|
+
}
|
|
65
|
+
const topics = l["topics"].map((t) => {
|
|
66
|
+
if (typeof t !== "string" || !HASH.test(t))
|
|
67
|
+
malformed(what);
|
|
68
|
+
return t.toLowerCase();
|
|
69
|
+
});
|
|
70
|
+
return { address: l["address"].toLowerCase(), topics, data: data(l["data"], what) };
|
|
71
|
+
}
|
|
72
|
+
/** The block tag of a read: a mark, or a number. */
|
|
73
|
+
function tag(at) {
|
|
74
|
+
return typeof at === "bigint" ? hex(at) : at;
|
|
75
|
+
}
|
|
76
|
+
/** lcp's reader of `network` over the JSON-RPC endpoint `rpc`, each call ending by `deadline`. */
|
|
77
|
+
export function evmReader(network, rpc, deadline) {
|
|
78
|
+
const call = (method, params) => rpcCall(rpc, method, params, deadline);
|
|
79
|
+
const block = async (at) => {
|
|
80
|
+
const b = await call("eth_getBlockByNumber", [tag(at), false]);
|
|
81
|
+
if (b === null)
|
|
82
|
+
throw new ReaderError("history", `no block ${tag(at)}`);
|
|
83
|
+
if (!isObject(b))
|
|
84
|
+
malformed("block");
|
|
85
|
+
return { number: quantity(b["number"], "block number"), timestamp: quantity(b["timestamp"], "block timestamp") };
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
network,
|
|
89
|
+
async receipt(tx) {
|
|
90
|
+
const r = await call("eth_getTransactionReceipt", [tx]);
|
|
91
|
+
if (r === null)
|
|
92
|
+
return null;
|
|
93
|
+
if (!isObject(r) || !Array.isArray(r["logs"]))
|
|
94
|
+
malformed("receipt");
|
|
95
|
+
const status = quantity(r["status"], "receipt status");
|
|
96
|
+
if (status !== 0n && status !== 1n)
|
|
97
|
+
malformed("receipt status");
|
|
98
|
+
return {
|
|
99
|
+
status: status === 1n ? 1 : 0,
|
|
100
|
+
blockNumber: quantity(r["blockNumber"], "receipt block"),
|
|
101
|
+
logs: r["logs"].map((l) => logOf(l, "receipt log")),
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
async blockNumber(mark) {
|
|
105
|
+
return (await block(mark)).number;
|
|
106
|
+
},
|
|
107
|
+
async transaction(tx) {
|
|
108
|
+
const t = await call("eth_getTransactionByHash", [tx]);
|
|
109
|
+
if (t === null)
|
|
110
|
+
return null;
|
|
111
|
+
if (!isObject(t) || typeof t["from"] !== "string" || !ADDRESS.test(t["from"]))
|
|
112
|
+
malformed("transaction");
|
|
113
|
+
const to = t["to"];
|
|
114
|
+
if (to !== null && (typeof to !== "string" || !ADDRESS.test(to)))
|
|
115
|
+
malformed("transaction");
|
|
116
|
+
return {
|
|
117
|
+
from: t["from"].toLowerCase(),
|
|
118
|
+
to: to === null ? null : to.toLowerCase(),
|
|
119
|
+
input: data(t["input"], "transaction input"),
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
async call(to, input, at) {
|
|
123
|
+
return data(await call("eth_call", [{ to, data: input }, hex(at)]), "call result");
|
|
124
|
+
},
|
|
125
|
+
async logs(filter) {
|
|
126
|
+
const params = {
|
|
127
|
+
topics: filter.topics,
|
|
128
|
+
fromBlock: hex(filter.fromBlock),
|
|
129
|
+
toBlock: hex(filter.toBlock),
|
|
130
|
+
};
|
|
131
|
+
if (filter.address !== undefined)
|
|
132
|
+
params["address"] = filter.address;
|
|
133
|
+
const logs = await call("eth_getLogs", [params]);
|
|
134
|
+
if (!Array.isArray(logs))
|
|
135
|
+
malformed("logs");
|
|
136
|
+
return logs.map((l) => {
|
|
137
|
+
if (!isObject(l) || typeof l["transactionHash"] !== "string" || !HASH.test(l["transactionHash"]))
|
|
138
|
+
malformed("log");
|
|
139
|
+
return {
|
|
140
|
+
...logOf(l, "log"),
|
|
141
|
+
transactionHash: l["transactionHash"].toLowerCase(),
|
|
142
|
+
blockNumber: quantity(l["blockNumber"], "log block"),
|
|
143
|
+
logIndex: Number(quantity(l["logIndex"], "log index")),
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
block,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** The OP Stack's GasPriceOracle predeploy, as viem's `op-stack` contracts name it. */
|
|
151
|
+
const GAS_PRICE_ORACLE = "0x420000000000000000000000000000000000000F";
|
|
152
|
+
const GET_L1_FEE = [
|
|
153
|
+
{ type: "function", name: "getL1Fee", stateMutability: "view", inputs: [{ name: "_data", type: "bytes" }], outputs: [{ type: "uint256" }] },
|
|
154
|
+
];
|
|
155
|
+
/**
|
|
156
|
+
* The L1 data fee the transaction `t`, as the node answers it unmined, would pay now on an OP Stack chain: the
|
|
157
|
+
* GasPriceOracle's `getL1Fee` over its serialized form, as viem's `estimateL1Fee` asks it. 0 where the call answers no
|
|
158
|
+
* one word, as on a chain with no such predeploy, or the node refuses it.
|
|
159
|
+
*/
|
|
160
|
+
async function l1FeeNow(rpc, t, deadline) {
|
|
161
|
+
try {
|
|
162
|
+
const optional = (v, what) => (v === undefined || v === null ? undefined : quantity(v, what));
|
|
163
|
+
const serialized = serializeTransaction({
|
|
164
|
+
type: "eip1559",
|
|
165
|
+
chainId: Number(quantity(t["chainId"], "chain id")),
|
|
166
|
+
nonce: Number(quantity(t["nonce"], "nonce")),
|
|
167
|
+
gas: quantity(t["gas"], "gas limit"),
|
|
168
|
+
maxFeePerGas: quantity(t["maxFeePerGas"] ?? t["gasPrice"], "fee cap"),
|
|
169
|
+
maxPriorityFeePerGas: optional(t["maxPriorityFeePerGas"], "priority fee") ?? 0n,
|
|
170
|
+
...(typeof t["to"] === "string" ? { to: t["to"] } : {}),
|
|
171
|
+
value: optional(t["value"], "value") ?? 0n,
|
|
172
|
+
data: data(t["input"] ?? "0x", "input"),
|
|
173
|
+
});
|
|
174
|
+
const call = encodeFunctionData({ abi: GET_L1_FEE, functionName: "getL1Fee", args: [serialized] });
|
|
175
|
+
const word = await rpcCall(rpc, "eth_call", [{ to: GAS_PRICE_ORACLE, data: call }, "latest"], deadline);
|
|
176
|
+
return typeof word === "string" && HASH.test(word) ? BigInt(word) : 0n;
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return 0n;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* What a transaction the module's key sent costs it, in wei: once the node shows its receipt, the fee it paid, the gas
|
|
184
|
+
* used times the effective gas price, and on an OP Stack chain the L1 data fee the receipt shows (`l1Fee`), which the
|
|
185
|
+
* key pays beside it; until then the most it can pay, its gas limit times its fee cap (or its gas price, for a
|
|
186
|
+
* transaction of a type that has no cap), and the L1 data fee it would pay now. Undefined where the node holds neither.
|
|
187
|
+
*/
|
|
188
|
+
export async function transactionFee(rpc, transaction, deadline) {
|
|
189
|
+
const r = await rpcCall(rpc, "eth_getTransactionReceipt", [transaction], deadline);
|
|
190
|
+
if (r !== null) {
|
|
191
|
+
if (!isObject(r))
|
|
192
|
+
malformed("receipt");
|
|
193
|
+
const l1 = r["l1Fee"] === undefined || r["l1Fee"] === null ? 0n : quantity(r["l1Fee"], "L1 fee");
|
|
194
|
+
return { included: quantity(r["gasUsed"], "gas used") * quantity(r["effectiveGasPrice"], "effective gas price") + l1 };
|
|
195
|
+
}
|
|
196
|
+
const t = await rpcCall(rpc, "eth_getTransactionByHash", [transaction], deadline);
|
|
197
|
+
if (t === null)
|
|
198
|
+
return undefined;
|
|
199
|
+
if (!isObject(t))
|
|
200
|
+
malformed("transaction");
|
|
201
|
+
const cap = t["maxFeePerGas"] ?? t["gasPrice"];
|
|
202
|
+
return { bound: quantity(t["gas"], "gas limit") * quantity(cap, "fee cap") + (await l1FeeNow(rpc, t, deadline)) };
|
|
203
|
+
}
|
|
204
|
+
/** `transactionFee` as the fee count takes it: what was paid once included, the bound until then. */
|
|
205
|
+
export async function feeShown(rpc, transaction, deadline) {
|
|
206
|
+
const fee = await transactionFee(rpc, transaction, deadline);
|
|
207
|
+
if (fee === undefined)
|
|
208
|
+
return undefined;
|
|
209
|
+
return "included" in fee ? { fee: fee.included, included: true } : { fee: fee.bound, included: false };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The fee budget's read of a transaction a class sent, from the module's node: the fee the chain shows once included,
|
|
213
|
+
* its own bound until then. A transaction the node holds neither as sent nor as included rejects.
|
|
214
|
+
*/
|
|
215
|
+
export function chargedFrom(rpc) {
|
|
216
|
+
return async (_payment, sent) => {
|
|
217
|
+
const shown = await feeShown(rpc, sent.transaction, sent.deadline);
|
|
218
|
+
if (shown === undefined)
|
|
219
|
+
throw new Error(`the node holds no transaction ${sent.transaction}`);
|
|
220
|
+
return shown;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ContractExecuteTransaction } from "@hiero-ledger/sdk";
|
|
2
|
+
import type { Network } from "@x402/core/types";
|
|
3
|
+
import { type FeeMeter, type Module } from "../../module.js";
|
|
4
|
+
import { type HederaConfig } from "./index.js";
|
|
5
|
+
/** `executeTransfer(address,address,address,uint256,bytes)`, as x402's specification states its selector. */
|
|
6
|
+
export declare const EXECUTE_TRANSFER = "ea8f19fd";
|
|
7
|
+
/** The fee payer's account, and the act of its key: sign the transaction the module built, submit it, await its receipt. */
|
|
8
|
+
export interface ExecutorSigner {
|
|
9
|
+
/** The fee-payer account, `shard.realm.num`, which the transaction id names. */
|
|
10
|
+
readonly accountId: string;
|
|
11
|
+
/**
|
|
12
|
+
* Freezes `transaction` for the network's nodes, signs it as the fee payer, submits it and waits for its receipt;
|
|
13
|
+
* answers its transaction id as the SDK writes it, whatever the receipt's status.
|
|
14
|
+
*/
|
|
15
|
+
execute(transaction: ContractExecuteTransaction, network: Network): Promise<{
|
|
16
|
+
readonly transactionId: string;
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
/** What the module is given beside the Hedera module's configuration. */
|
|
20
|
+
export interface ExecutorOffering {
|
|
21
|
+
/** The executor contracts the module admits, `shard.realm.num`: every other is refused. */
|
|
22
|
+
readonly executors: readonly string[];
|
|
23
|
+
readonly signer: ExecutorSigner;
|
|
24
|
+
/** The gas limit of the call, the same in the simulation and in the transaction. */
|
|
25
|
+
readonly gas: number;
|
|
26
|
+
/** The transaction's maximum fee, in tinybars: what the fee count holds until the record shows the fee. */
|
|
27
|
+
readonly maxFee: bigint;
|
|
28
|
+
/** The fee meter of the network, which the module asks before it submits. */
|
|
29
|
+
readonly meter: FeeMeter;
|
|
30
|
+
/** How long `settle` waits for the record, in milliseconds. */
|
|
31
|
+
readonly settleWaitMs: number;
|
|
32
|
+
}
|
|
33
|
+
/** An entity's long-zero EVM address: shard in 4 bytes, realm and number in 8 each, as Hedera addresses a contract. */
|
|
34
|
+
export declare function longZero(entity: string): string;
|
|
35
|
+
/** The call data of `executeTransfer(from, asset, to, amount, authorization)`, ABI-encoded. */
|
|
36
|
+
export declare function executeTransferData(a: {
|
|
37
|
+
from: string;
|
|
38
|
+
asset: string;
|
|
39
|
+
to: string;
|
|
40
|
+
amount: bigint;
|
|
41
|
+
authorization: string;
|
|
42
|
+
}): string;
|
|
43
|
+
/**
|
|
44
|
+
* Phase 4, rule 3, over the record merged with its child records: the payee credited exactly `amount` in the asset; the
|
|
45
|
+
* payer debited `amount`, the fee payer the fee, and no other account; nothing else moved but the fee's distribution
|
|
46
|
+
* in HBAR; no NFT changed owner; and no log records an ERC-20 transfer out of the fee payer's address. A staking reward
|
|
47
|
+
* the record lists (HIP-406) is taken out of the account it credits and out of the reward account's debit. The reason
|
|
48
|
+
* of the first rule that fails, or the fee the record charged.
|
|
49
|
+
*/
|
|
50
|
+
export declare function judgeRecord(entries: readonly Record<string, unknown>[], logs: readonly Record<string, unknown>[], t: {
|
|
51
|
+
asset: string;
|
|
52
|
+
payTo: string;
|
|
53
|
+
amount: bigint;
|
|
54
|
+
payer: string;
|
|
55
|
+
feePayer: string;
|
|
56
|
+
feePayerEvm: string;
|
|
57
|
+
}): {
|
|
58
|
+
fee: bigint;
|
|
59
|
+
} | {
|
|
60
|
+
reason: string;
|
|
61
|
+
};
|
|
62
|
+
/** The transfer-executor module on the Hedera module's network, admitting `offering.executors` alone. */
|
|
63
|
+
export declare function transferExecutorModule(c: HederaConfig, offering: ExecutorOffering): Module;
|