@integraledger/profile-facilitator 0.5.1 → 0.6.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/CHANGELOG.md +449 -0
- package/README.md +551 -47
- package/dist/core.d.ts +1 -1
- package/dist/core.js +683 -66
- package/dist/http.d.ts +19 -4
- package/dist/http.js +16 -6
- package/dist/module.d.ts +108 -21
- package/dist/module.js +39 -0
- package/dist/modules/casper/index.js +14 -2
- package/dist/modules/erc7710/index.d.ts +8 -5
- package/dist/modules/erc7710/index.js +1110 -33
- package/dist/modules/evm/escrow.d.ts +125 -0
- package/dist/modules/evm/escrow.js +619 -0
- package/dist/modules/evm/forms.d.ts +37 -9
- package/dist/modules/evm/forms.js +165 -21
- package/dist/modules/evm/index.d.ts +41 -6
- package/dist/modules/evm/index.js +1127 -18
- package/dist/modules/evm/node.d.ts +16 -1
- package/dist/modules/evm/node.js +181 -26
- package/dist/modules/evm/nonce-proof.d.ts +186 -0
- package/dist/modules/evm/nonce-proof.js +779 -0
- package/dist/modules/solana/index.d.ts +3 -0
- package/dist/modules/solana/index.js +94 -7
- package/dist/modules/solana/reader.d.ts +2 -1
- package/dist/modules/solana/reader.js +42 -4
- package/dist/modules/solana/upto-claim.d.ts +58 -0
- package/dist/modules/solana/upto-claim.js +269 -0
- package/dist/modules/stellar/index.d.ts +5 -0
- package/dist/modules/stellar/index.js +17 -7
- package/dist/modules/stellar/node.d.ts +7 -1
- package/dist/modules/stellar/node.js +51 -7
- package/dist/modules/sui/index.js +63 -18
- package/dist/modules/sui/node.d.ts +13 -0
- package/dist/modules/sui/node.js +60 -0
- package/dist/modules/ton/index.js +5 -1
- package/dist/modules/xrpl/index.js +60 -11
- package/dist/modules/xrpl/node.d.ts +31 -2
- package/dist/modules/xrpl/node.js +88 -17
- package/dist/modules/xrpl/upto.js +105 -19
- package/dist/record.d.ts +52 -8
- package/dist/record.js +39 -5
- package/dist/wrapper.d.ts +3 -1
- package/dist/wrapper.js +4 -2
- package/package.json +3 -3
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The read of an auth-capture escrow's later message: a capture, a void or a refund that x402's lifecycle manager sends
|
|
3
|
+
* on a payment an earlier hold opened in the commerce-payments escrow. lcp does not decode these messages, so the
|
|
4
|
+
* module reads them itself, from the escrow's own state and events, by the payment's hash as the escrow computes it.
|
|
5
|
+
*
|
|
6
|
+
* The escrow keeps, per payment, a capturable and a refundable amount. A capture moves its amount from the first to the
|
|
7
|
+
* second, a refund takes its amount from the second, a void and a reclaim empty the first. The capturable amount never
|
|
8
|
+
* rises again once a hold is made, and a capture or refund of zero reverts, so the pair the receiver authorizer signs as
|
|
9
|
+
* the message's expected amounts, once left, never returns. A capture or refund whose expected amounts still stand has
|
|
10
|
+
* not landed, and the escrow takes it as signed; one whose expected amounts no longer stand can never be taken as signed
|
|
11
|
+
* again. A void is in the same position while the capturable amount is above zero. The escrow itself does not compare
|
|
12
|
+
* a call with the expected amounts: the same capture or refund call sent twice executes twice, so it is the module that
|
|
13
|
+
* sends a message's call at most once (`unexecutedAt`).
|
|
14
|
+
*/
|
|
15
|
+
import { ReaderError } from "@integraledger/lcp/evm";
|
|
16
|
+
import { resolveAuthCaptureDeployment } from "@x402/evm";
|
|
17
|
+
import { decodeFunctionResult, encodeFunctionData, keccak256, parseAbi, toFunctionSelector, toHex } from "viem";
|
|
18
|
+
import { outcomeOf } from "../../module.js";
|
|
19
|
+
import { rpcCall } from "./node.js";
|
|
20
|
+
import { finalizedReceipt, nonceProof, passedAtFinalized } from "./nonce-proof.js";
|
|
21
|
+
const INFO = "(address operator,address payer,address receiver,address token,uint120 maxAmount,uint48 preApprovalExpiry,uint48 authorizationExpiry,uint48 refundExpiry,uint16 minFeeBps,uint16 maxFeeBps,address feeReceiver,uint256 salt)";
|
|
22
|
+
const ESCROW = parseAbi([
|
|
23
|
+
`function getHash(${INFO} paymentInfo) view returns (bytes32)`,
|
|
24
|
+
"function paymentState(bytes32) view returns (bool hasCollectedPayment, uint120 capturableAmount, uint120 refundableAmount)",
|
|
25
|
+
]);
|
|
26
|
+
/** The escrow's `void`, as its call data starts. */
|
|
27
|
+
const VOID_CALL = toFunctionSelector(parseAbi([`function void(${INFO} paymentInfo)`])[0]).toLowerCase();
|
|
28
|
+
/** Whether call data is the escrow's `void`. */
|
|
29
|
+
export function isVoidCall(data) {
|
|
30
|
+
return data.toLowerCase().startsWith(VOID_CALL);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The escrow's events that change a payment's amounts, by their topic: the hash of the event's name and its parameters'
|
|
34
|
+
* types alone, a tuple's as its members' types in brackets, with no member's name.
|
|
35
|
+
*/
|
|
36
|
+
const topic = (signature) => keccak256(toHex(signature)).toLowerCase();
|
|
37
|
+
/** The payment's tuple as an event's signature names it: its members' types, in order. */
|
|
38
|
+
const INFO_TYPES = "(address,address,address,address,uint120,uint48,uint48,uint48,uint16,uint16,address,uint256)";
|
|
39
|
+
const AUTHORIZED = topic(`PaymentAuthorized(bytes32,${INFO_TYPES},uint256,address)`);
|
|
40
|
+
const CHARGED_V10 = topic(`PaymentCharged(bytes32,${INFO_TYPES},uint256,address,uint16,address)`);
|
|
41
|
+
const CHARGED_V11 = topic(`PaymentCharged(bytes32,${INFO_TYPES},uint256,address,uint256,address)`);
|
|
42
|
+
/** v1.0's capture names its fee in basis points, v1.1's as an amount. */
|
|
43
|
+
const CAPTURED_V10 = topic("PaymentCaptured(bytes32,uint256,uint16,address)");
|
|
44
|
+
const CAPTURED_V11 = topic("PaymentCaptured(bytes32,uint256,uint256,address)");
|
|
45
|
+
const VOIDED = topic("PaymentVoided(bytes32,uint256)");
|
|
46
|
+
const RECLAIMED = topic("PaymentReclaimed(bytes32,uint256)");
|
|
47
|
+
const REFUNDED = topic("PaymentRefunded(bytes32,uint256,address)");
|
|
48
|
+
function isObject(v) {
|
|
49
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
50
|
+
}
|
|
51
|
+
/** The message a later payload carries, or undefined where it is not one the escrow could take. */
|
|
52
|
+
function messageOf(p) {
|
|
53
|
+
const body = p.payload.payload;
|
|
54
|
+
const extra = p.requirements.extra;
|
|
55
|
+
const deployment = resolveAuthCaptureDeployment(typeof extra?.["authCaptureEscrow"] === "string" ? extra["authCaptureEscrow"] : undefined);
|
|
56
|
+
if (!isObject(body) || deployment === undefined)
|
|
57
|
+
return undefined;
|
|
58
|
+
const type = body["type"];
|
|
59
|
+
const i = body["paymentInfo"];
|
|
60
|
+
if ((type !== "capture" && type !== "void" && type !== "refund") || !isObject(i))
|
|
61
|
+
return undefined;
|
|
62
|
+
try {
|
|
63
|
+
const info = {
|
|
64
|
+
operator: String(i["operator"]),
|
|
65
|
+
payer: String(i["payer"]),
|
|
66
|
+
receiver: String(i["receiver"]),
|
|
67
|
+
token: String(i["token"]),
|
|
68
|
+
maxAmount: BigInt(String(i["maxAmount"])),
|
|
69
|
+
preApprovalExpiry: Number(BigInt(String(i["preApprovalExpiry"]))),
|
|
70
|
+
authorizationExpiry: Number(BigInt(String(i["authorizationExpiry"]))),
|
|
71
|
+
refundExpiry: Number(BigInt(String(i["refundExpiry"]))),
|
|
72
|
+
minFeeBps: Number(BigInt(String(i["minFeeBps"]))),
|
|
73
|
+
maxFeeBps: Number(BigInt(String(i["maxFeeBps"]))),
|
|
74
|
+
feeReceiver: String(i["feeReceiver"]),
|
|
75
|
+
salt: BigInt(String(i["salt"])),
|
|
76
|
+
};
|
|
77
|
+
const base = { type: type, escrow: deployment.escrow.toLowerCase(), info };
|
|
78
|
+
if (type === "void")
|
|
79
|
+
return base;
|
|
80
|
+
const expected = { capturable: BigInt(String(body["expectedCapturableAmount"])), refundable: BigInt(String(body["expectedRefundableAmount"])) };
|
|
81
|
+
const amount = BigInt(String(body["amount"]));
|
|
82
|
+
if (type === "refund")
|
|
83
|
+
return { ...base, amount, expected, collector: deployment.operatorRefundCollector.toLowerCase() };
|
|
84
|
+
const receiver = String(body["feeReceiver"]).toLowerCase();
|
|
85
|
+
const fee = body["feeAmount"] !== undefined
|
|
86
|
+
? { amount: BigInt(String(body["feeAmount"])), receiver }
|
|
87
|
+
: { bps: BigInt(String(body["feeBps"])), receiver };
|
|
88
|
+
return { ...base, amount, expected, fee, ...(carriesVoid(body) ? { voids: true } : {}) };
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** The 32-byte words of a log's data, as unsigned integers. */
|
|
95
|
+
function words(data) {
|
|
96
|
+
const out = [];
|
|
97
|
+
for (let i = 2; i + 64 <= data.length; i += 64)
|
|
98
|
+
out.push(BigInt(`0x${data.slice(i, i + 64)}`));
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
/** The amount an event of the escrow moves: a hold's or charge's after the payment's twelve words, else its first. */
|
|
102
|
+
function movedBy(log) {
|
|
103
|
+
const t0 = log.topics[0];
|
|
104
|
+
return (t0 === AUTHORIZED || t0 === CHARGED_V10 || t0 === CHARGED_V11 ? chargedAmount(log) : words(log.data)[0]) ?? 0n;
|
|
105
|
+
}
|
|
106
|
+
/** The payment's amounts after `log`, from its amounts before it. */
|
|
107
|
+
function after(s, log) {
|
|
108
|
+
const t0 = log.topics[0];
|
|
109
|
+
const a = movedBy(log);
|
|
110
|
+
if (t0 === AUTHORIZED)
|
|
111
|
+
return { collected: true, capturable: s.capturable + a, refundable: s.refundable };
|
|
112
|
+
if (t0 === CHARGED_V10 || t0 === CHARGED_V11)
|
|
113
|
+
return { collected: true, capturable: s.capturable, refundable: s.refundable + a };
|
|
114
|
+
if (t0 === CAPTURED_V10 || t0 === CAPTURED_V11)
|
|
115
|
+
return { ...s, capturable: s.capturable - a, refundable: s.refundable + a };
|
|
116
|
+
if (t0 === VOIDED || t0 === RECLAIMED)
|
|
117
|
+
return { ...s, capturable: 0n };
|
|
118
|
+
if (t0 === REFUNDED)
|
|
119
|
+
return { ...s, refundable: s.refundable - a };
|
|
120
|
+
return s;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The payment's amounts before `log`, from its amounts after it: `after` undone. A void's and a reclaim's event name
|
|
124
|
+
* the capturable amount they emptied; a hold or charge is the payment's first event, before which nothing was collected.
|
|
125
|
+
*/
|
|
126
|
+
function before(s, log) {
|
|
127
|
+
const t0 = log.topics[0];
|
|
128
|
+
const a = movedBy(log);
|
|
129
|
+
if (t0 === AUTHORIZED)
|
|
130
|
+
return { collected: false, capturable: s.capturable - a, refundable: s.refundable };
|
|
131
|
+
if (t0 === CHARGED_V10 || t0 === CHARGED_V11)
|
|
132
|
+
return { collected: false, capturable: s.capturable, refundable: s.refundable - a };
|
|
133
|
+
if (t0 === CAPTURED_V10 || t0 === CAPTURED_V11)
|
|
134
|
+
return { ...s, capturable: s.capturable + a, refundable: s.refundable - a };
|
|
135
|
+
if (t0 === VOIDED || t0 === RECLAIMED)
|
|
136
|
+
return { ...s, capturable: s.capturable + a };
|
|
137
|
+
if (t0 === REFUNDED)
|
|
138
|
+
return { ...s, refundable: s.refundable + a };
|
|
139
|
+
return s;
|
|
140
|
+
}
|
|
141
|
+
/** The amount of a hold's or charge's event: the word after the payment's twelve. */
|
|
142
|
+
function chargedAmount(log) {
|
|
143
|
+
return words(log.data)[12];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Whether `log` is the escrow's event of the message itself: its kind, its amount, for a capture its fee terms, and for
|
|
147
|
+
* a refund its token collector.
|
|
148
|
+
*/
|
|
149
|
+
function isOf(m, log) {
|
|
150
|
+
const t0 = log.topics[0];
|
|
151
|
+
const w = words(log.data);
|
|
152
|
+
switch (m.type) {
|
|
153
|
+
case "void":
|
|
154
|
+
return t0 === VOIDED;
|
|
155
|
+
case "refund":
|
|
156
|
+
return t0 === REFUNDED && w[0] === m.amount && w[1] !== undefined && `0x${w[1].toString(16).padStart(40, "0")}` === m.collector;
|
|
157
|
+
case "capture": {
|
|
158
|
+
const receiver = w[2] === undefined ? undefined : `0x${w[2].toString(16).padStart(40, "0")}`;
|
|
159
|
+
if (receiver !== m.fee?.receiver)
|
|
160
|
+
return false;
|
|
161
|
+
if (t0 === CAPTURED_V11)
|
|
162
|
+
return w[0] === m.amount && m.fee?.amount !== undefined && w[1] === m.fee.amount;
|
|
163
|
+
if (t0 === CAPTURED_V10)
|
|
164
|
+
return w[0] === m.amount && m.fee?.bps !== undefined && w[1] === m.fee.bps;
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Whether the payment's amounts `s` are those under which the escrow takes the message as signed. */
|
|
170
|
+
function stands(m, s) {
|
|
171
|
+
if (m.type === "void")
|
|
172
|
+
return s.capturable > 0n;
|
|
173
|
+
return s.capturable === m.expected.capturable && s.refundable === m.expected.refundable;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Whether the payment's amounts `s` show that the message can never be taken as signed again. Once a hold is made its
|
|
177
|
+
* capturable amount never rises, and while it stays the same the refundable amount only falls (a refund; a capture
|
|
178
|
+
* moves both): so a capture or refund is past once the capturable amount is below the one it expects, or equal with the
|
|
179
|
+
* refundable amount below. A void is past once a collected payment has nothing capturable.
|
|
180
|
+
*/
|
|
181
|
+
function past(m, s) {
|
|
182
|
+
if (!s.collected)
|
|
183
|
+
return false;
|
|
184
|
+
if (m.type === "void")
|
|
185
|
+
return s.capturable === 0n;
|
|
186
|
+
const e = m.expected;
|
|
187
|
+
return s.capturable < e.capturable || (s.capturable === e.capturable && s.refundable < e.refundable);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Whether the payment's amounts `s` show that no call of the message can execute any more, whatever transaction carries
|
|
191
|
+
* it: the escrow reverts a capture above the capturable amount, which never rises once a hold is made; a refund above
|
|
192
|
+
* the refundable amount, which rises only by what is captured, so never past the two amounts together; and a void of a
|
|
193
|
+
* payment with nothing capturable.
|
|
194
|
+
*/
|
|
195
|
+
function never(m, s) {
|
|
196
|
+
if (!s.collected)
|
|
197
|
+
return false;
|
|
198
|
+
if (m.type === "void")
|
|
199
|
+
return s.capturable === 0n;
|
|
200
|
+
if (m.type === "capture")
|
|
201
|
+
return s.capturable < m.amount;
|
|
202
|
+
return s.capturable + s.refundable < m.amount;
|
|
203
|
+
}
|
|
204
|
+
/** The most blocks one `eth_getLogs` call of the read asks for: nodes that bound a call's range take this many. */
|
|
205
|
+
export const LOG_SPAN = 2000n;
|
|
206
|
+
const unreadable = (reason) => outcomeOf({ state: "unreadable", reason, foreign: [] });
|
|
207
|
+
function answer(a) {
|
|
208
|
+
return outcomeOf(a);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The read of an escrow's later message, at the mark `finalized`. `at.transaction` is the transaction the record names
|
|
212
|
+
* for the claim, the last one the module named before sending it; `at.from` the block the module read just before the
|
|
213
|
+
* claim (`escrowPosition`), after which anything the settler sent lands. In this order:
|
|
214
|
+
*
|
|
215
|
+
* - The transaction the record names, where the node shows its receipt: reverted, `failed` (`transaction-failed`),
|
|
216
|
+
* naming it; executed with the escrow's event of this message, `settled`, naming it. Either in a block above the
|
|
217
|
+
* finalized one is `pending` (`below-mark`). Where the node holds it unmined (it shows it with no block), `pending`
|
|
218
|
+
* (`below-mark`) naming it. Where the node shows it in a block and no receipt of it, it is not held: where the node
|
|
219
|
+
* shows it, or another signed at its nonce, in a block, `settled` where the finalized chain shows one of them
|
|
220
|
+
* executed with the message's event (`own`, below), naming that one; else the read goes on.
|
|
221
|
+
* - The `pending` transaction count of each of the gas key's own addresses (`keys`, the accounts that sign and send the
|
|
222
|
+
* escrow's calls, whatever contract `PaymentInfo.operator` names), then one block fixed (the latest) for the escrow's
|
|
223
|
+
* amounts for the payment and each address's mined count. Where the amounts are still those the message expects (for
|
|
224
|
+
* a void, a capturable amount above zero), the message has not landed: `pending` (`instrument-live`), so a held claim
|
|
225
|
+
* may send the identical message again, unless an address's pending count is above its mined count, a transaction
|
|
226
|
+
* the node held unmined, which may be this message: `pending` (`below-mark`), and nothing is sent. A send the node held when the
|
|
227
|
+
* pending count was read is either mined by the fixed block, its effect in the amounts, or counted unmined. An
|
|
228
|
+
* endpoint whose reads do not show a transaction it accepted cannot be told from one that holds none.
|
|
229
|
+
* - Otherwise the escrow's events for the payment after `from`, to the fixed block, asked for `LOG_SPAN` blocks at a
|
|
230
|
+
* time, are replayed from the amounts at `from`, which are those at the fixed block with the events since undone (no
|
|
231
|
+
* state older than the fixed block is read). Where the amounts at `from` show the message can never be taken as
|
|
232
|
+
* signed (`past`), `failed` (`before-position`) naming nothing, once `from` is at or below the finalized block, and
|
|
233
|
+
* `pending` (`below-mark`) before. The event of this message applied where its expected
|
|
234
|
+
* amounts stood is the message landing, `settled` naming its transaction; else the first event after which the
|
|
235
|
+
* message can never be taken as signed, `failed` (`instrument-consumed`) naming it; either above the finalized block
|
|
236
|
+
* is `pending` (`below-mark`). Where neither is found, or without `from`, `pending` (`search-incomplete`).
|
|
237
|
+
*
|
|
238
|
+
* A capture that also carries the void of the payment's remainder (`carriesVoid`) is `settled` only once
|
|
239
|
+
* that void is done: where its capture landed at or below the finalized block, the capturable amount at the finalized
|
|
240
|
+
* block is above zero, and no void the module sent for it was reverted (the transaction named last), it is `pending`:
|
|
241
|
+
* `below-mark` where the capturable amount at the latest block is zero or a transaction of the gas key is unmined, else
|
|
242
|
+
* `instrument-live`, so that a held claim sends the void (`voidOf`, `captureLanded`). A void the escrow reverted leaves
|
|
243
|
+
* the capture's own answer, `settled`. `voids` reads a capture as carrying that void where its row's claim carried it,
|
|
244
|
+
* whatever body is presented now.
|
|
245
|
+
*
|
|
246
|
+
* `own` is every transaction the module signed for the message's own call that may have been given to a node
|
|
247
|
+
* (`SignedCall`). The escrow takes a call whatever amounts the message expects, so one of them that executed is the
|
|
248
|
+
* message landing wherever the amounts stood, and no answer is `failed` for good while one of them may have executed
|
|
249
|
+
* the message or may still: `before-position` and `instrument-consumed` above are given only where, at one
|
|
250
|
+
* finalized block named by its hash, every one of them is shown unable to. Its nonce is not yet taken there (the key's
|
|
251
|
+
* count) and the amounts there let no call of the message execute any more (`never`); or its nonce is taken, and the
|
|
252
|
+
* finalized chain shows what took it: a receipt of one signed at that nonce that reverted, or another transaction of
|
|
253
|
+
* the key (`nonceProof`). Where the finalized chain shows one of them executed with the message's event (its receipt,
|
|
254
|
+
* or the events of the block that holds it, read by that block's hash, where the node shows no receipt), `settled`
|
|
255
|
+
* naming it. Otherwise `pending` (`below-mark`) naming the row's transaction: a lookup that finds nothing decides
|
|
256
|
+
* nothing.
|
|
257
|
+
*
|
|
258
|
+
* A node call that fails is `unreadable`, and nothing is sent on it.
|
|
259
|
+
*/
|
|
260
|
+
export async function escrowRead(reader, rpc, keys, p, at, voids = false, own = []) {
|
|
261
|
+
const m = withVoid(messageOf(p), voids);
|
|
262
|
+
if (m === undefined)
|
|
263
|
+
return unreadable("unparseable");
|
|
264
|
+
try {
|
|
265
|
+
const hash = await hashOf(reader, rpc, m, at.deadline);
|
|
266
|
+
const amountsAt = async (block) => {
|
|
267
|
+
const out = await reader.call(m.escrow, encodeFunctionData({ abi: ESCROW, functionName: "paymentState", args: [hash] }), block);
|
|
268
|
+
const [collected, capturable, refundable] = decodeFunctionResult({ abi: ESCROW, functionName: "paymentState", data: out });
|
|
269
|
+
return { collected, capturable, refundable };
|
|
270
|
+
};
|
|
271
|
+
const finalized = await reader.blockNumber("finalized");
|
|
272
|
+
const ofMessage = (log) => log.address === m.escrow && log.topics[1] === hash && isOf(m, log);
|
|
273
|
+
// Whether the transaction named last is the void of the payment's remainder, sent once the capture landed, and the
|
|
274
|
+
// escrow reverted it: the capture's answer is then its own, and the void is not sent again.
|
|
275
|
+
let voidReverted = false;
|
|
276
|
+
// The capture landed by `tx`, at or below the finalized block: settled, unless it voids the payment's remainder and
|
|
277
|
+
// that void is still to land. While the payment's capturable amount at the finalized block is above zero and no void
|
|
278
|
+
// of it the module sent was reverted, the void is due: `pending` (`below-mark`) where the capturable amount at the
|
|
279
|
+
// latest block is zero (a void above the finalized block) or a transaction of the key is unmined, which may be the
|
|
280
|
+
// void; else `pending` (`instrument-live`), so a held claim sends the void (`voidDue`).
|
|
281
|
+
const landed = async (tx) => {
|
|
282
|
+
const settled = answer({ state: "settled", transaction: tx, finality: "finalized", presented: "yes", foreign: [] });
|
|
283
|
+
if (m.voids !== true || voidReverted || (await amountsAt(finalized)).capturable === 0n)
|
|
284
|
+
return settled;
|
|
285
|
+
const unmined = await Promise.all(keys.map((k) => nonce(rpc, k, "pending", at.deadline)));
|
|
286
|
+
const head = await latest(rpc, at.deadline);
|
|
287
|
+
const [now, mined] = await Promise.all([amountsAt(head), Promise.all(keys.map((k) => nonce(rpc, k, head, at.deadline)))]);
|
|
288
|
+
const held = now.capturable === 0n || unmined.some((n, i) => n > mined[i]);
|
|
289
|
+
return answer({ state: "pending", reason: held ? "below-mark" : "instrument-live", foreign: [] });
|
|
290
|
+
};
|
|
291
|
+
// What the finalized chain shows of the transactions signed for the message's call at one nonce of one account:
|
|
292
|
+
// `executed`, one of them with the message's event; `dead`, none executed the message and none ever can; `open`,
|
|
293
|
+
// the nonce is not taken at the finalized block `f`; `unknown`, nothing the chain as read shows.
|
|
294
|
+
const fateOf = async (group, f) => {
|
|
295
|
+
const { from, nonce } = group[0];
|
|
296
|
+
// The key's count at `f`, by its hash; where the node serves no state there, the count at its latest block, by
|
|
297
|
+
// its hash, at or below the nonce, with `f` shown an ancestor of that block (`passedAtFinalized`). Where neither
|
|
298
|
+
// shows the count, the nonce is not shown open, and the finalized chain's other facts below decide.
|
|
299
|
+
if (from !== undefined && nonce !== undefined && (await passedAtFinalized(rpc, from, nonce, at.deadline, f).catch(() => true)) === false)
|
|
300
|
+
return "open";
|
|
301
|
+
for (const t of group) {
|
|
302
|
+
const tx = t.hash.toLowerCase();
|
|
303
|
+
const r = await finalizedReceipt(rpc, tx, at.deadline);
|
|
304
|
+
if (r === undefined)
|
|
305
|
+
continue;
|
|
306
|
+
if (r.status === 0)
|
|
307
|
+
return "dead";
|
|
308
|
+
const shown = await reader.receipt(tx);
|
|
309
|
+
return shown !== null && shown.status === 1 && shown.logs.some(ofMessage) ? { executed: tx } : "unknown";
|
|
310
|
+
}
|
|
311
|
+
if (from === undefined || nonce === undefined)
|
|
312
|
+
return "unknown";
|
|
313
|
+
const since = at.from === undefined ? {} : { from: BigInt(String(at.from)) };
|
|
314
|
+
const proof = await nonceProof(rpc, from, nonce, group.map((t) => t.hash), { deadline: at.deadline, ...since });
|
|
315
|
+
if (proof.answer === "taken-by-another")
|
|
316
|
+
return "dead";
|
|
317
|
+
if (proof.answer !== "ours")
|
|
318
|
+
return "unknown";
|
|
319
|
+
// One of them is in a block of the finalized chain, and the node shows no receipt of it: that block's own events
|
|
320
|
+
// of the payment, read by the block's hash, which has one answer on every node that holds it.
|
|
321
|
+
const inBlock = await rpcCall(rpc, "eth_getLogs", [{ blockHash: proof.block, address: m.escrow, topics: [null, hash] }], at.deadline);
|
|
322
|
+
if (!Array.isArray(inBlock))
|
|
323
|
+
throw new ReaderError("malformed", "the node's logs are not a list");
|
|
324
|
+
const executed = inBlock.some((l) => {
|
|
325
|
+
if (!isObject(l) || typeof l["address"] !== "string" || typeof l["data"] !== "string" || !Array.isArray(l["topics"]))
|
|
326
|
+
return false;
|
|
327
|
+
if (typeof l["transactionHash"] !== "string" || l["transactionHash"].toLowerCase() !== proof.transaction.toLowerCase())
|
|
328
|
+
return false;
|
|
329
|
+
if (typeof l["blockHash"] !== "string" || l["blockHash"].toLowerCase() !== proof.block.toLowerCase())
|
|
330
|
+
return false;
|
|
331
|
+
return ofMessage({ address: l["address"].toLowerCase(), topics: l["topics"].map((x) => String(x).toLowerCase()), data: l["data"] });
|
|
332
|
+
});
|
|
333
|
+
return executed ? { executed: proof.transaction.toLowerCase() } : "unknown";
|
|
334
|
+
};
|
|
335
|
+
// The transactions signed for the message's call, by the account and nonce each was signed at; one whose signed
|
|
336
|
+
// bytes are no longer kept alone.
|
|
337
|
+
const groups = () => {
|
|
338
|
+
const by = new Map();
|
|
339
|
+
for (const t of own) {
|
|
340
|
+
const k = t.from === undefined || t.nonce === undefined ? `hash:${t.hash.toLowerCase()}` : `${t.from.toLowerCase()}:${t.nonce}`;
|
|
341
|
+
by.set(k, [...(by.get(k) ?? []), t]);
|
|
342
|
+
}
|
|
343
|
+
return [...by.values()];
|
|
344
|
+
};
|
|
345
|
+
const ofPayment = (log) => log.address === m.escrow && log.topics[1] === hash;
|
|
346
|
+
// Whether the amounts at the finalized block `f` let no call of the message execute any more (`never`): the amounts
|
|
347
|
+
// there, by its hash; where the node serves no state there, for a capture of an amount or a void, the payment's
|
|
348
|
+
// void or reclaim (`drains`) in a block at or below `f` that the finalized chain shows executed, after which
|
|
349
|
+
// nothing is capturable for good. Neither shown, it is not.
|
|
350
|
+
const neverAt = async (f, drains) => {
|
|
351
|
+
const amounts = await amountsAtHash(rpc, m.escrow, hash, f, at.deadline).catch(() => undefined);
|
|
352
|
+
if (amounts !== undefined)
|
|
353
|
+
return never(m, amounts);
|
|
354
|
+
if (m.type === "refund" || (m.type === "capture" && !(m.amount > 0n)))
|
|
355
|
+
return false;
|
|
356
|
+
for (const log of drains) {
|
|
357
|
+
if (log.blockNumber <= f.number && (log.topics[0] === VOIDED || log.topics[0] === RECLAIMED) && ofPayment(log) && (await executedIn(rpc, log, at.deadline)))
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
};
|
|
362
|
+
// A `failed` answer, given only where the finalized chain shows that no transaction signed for the message's call
|
|
363
|
+
// executed it or ever can; `settled` where it shows one that did; else `pending` (`below-mark`), naming the row's.
|
|
364
|
+
const unlessOwn = async (failed, drains) => {
|
|
365
|
+
if (own.length === 0)
|
|
366
|
+
return failed;
|
|
367
|
+
const f = await finalizedBlock(rpc, at.deadline);
|
|
368
|
+
let final;
|
|
369
|
+
for (const group of groups()) {
|
|
370
|
+
const fate = await fateOf(group, f);
|
|
371
|
+
if (fate === "dead")
|
|
372
|
+
continue;
|
|
373
|
+
if (typeof fate === "object")
|
|
374
|
+
return landed(fate.executed);
|
|
375
|
+
if (fate === "open" && (final ??= await neverAt(f, drains)))
|
|
376
|
+
continue;
|
|
377
|
+
return answer({ state: "pending", reason: "below-mark", transaction: (at.transaction ?? group.at(-1).hash).toLowerCase(), foreign: [] });
|
|
378
|
+
}
|
|
379
|
+
return failed;
|
|
380
|
+
};
|
|
381
|
+
if (at.transaction !== undefined) {
|
|
382
|
+
const tx = at.transaction.toLowerCase();
|
|
383
|
+
const r = await reader.receipt(tx);
|
|
384
|
+
if (r !== null) {
|
|
385
|
+
if (r.blockNumber > finalized)
|
|
386
|
+
return answer({ state: "pending", reason: "below-mark", transaction: tx, foreign: [] });
|
|
387
|
+
// Final only on the finalized chain: the receipt's block, by its hash, shown below the finalized block's own hash.
|
|
388
|
+
const onChain = await finalizedReceipt(rpc, tx, at.deadline);
|
|
389
|
+
if (onChain === undefined || onChain.status !== r.status)
|
|
390
|
+
return answer({ state: "pending", reason: "below-mark", transaction: tx, foreign: [] });
|
|
391
|
+
if (r.status === 0 && m.voids === true && (await reader.transaction(tx))?.input.toLowerCase().startsWith(VOID_CALL))
|
|
392
|
+
voidReverted = true;
|
|
393
|
+
else if (r.status === 0)
|
|
394
|
+
return answer({ state: "failed", reason: "transaction-failed", transaction: tx, finality: "finalized", foreign: [] });
|
|
395
|
+
else if (r.logs.some(ofMessage))
|
|
396
|
+
return landed(tx);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
// No receipt shown. Held only where the node shows the transaction with no block: one it shows in a block, with
|
|
400
|
+
// no receipt (a node that dropped old receipts, or answers the receipt from another node), is not held, and
|
|
401
|
+
// the finalized chain's facts decide it.
|
|
402
|
+
const shown = await shownAs(rpc, tx, at.deadline);
|
|
403
|
+
if (shown === "held")
|
|
404
|
+
return answer({ state: "pending", reason: "below-mark", transaction: tx, foreign: [] });
|
|
405
|
+
// Where the node shows it in a block, or another signed at its nonce (one it replaced, or that replaced it),
|
|
406
|
+
// whichever of them the finalized chain shows executed with the message's event.
|
|
407
|
+
const group = groups().find((g) => g.some((t) => t.hash.toLowerCase() === tx));
|
|
408
|
+
let mined = shown === "mined";
|
|
409
|
+
for (const t of group ?? [])
|
|
410
|
+
mined ||= t.hash.toLowerCase() !== tx && (await shownAs(rpc, t.hash.toLowerCase(), at.deadline)) === "mined";
|
|
411
|
+
if (group !== undefined && mined) {
|
|
412
|
+
const fate = await fateOf(group, await finalizedBlock(rpc, at.deadline));
|
|
413
|
+
if (typeof fate === "object")
|
|
414
|
+
return landed(fate.executed);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
// The key's pending counts first, then one block for the amounts and the mined counts: a send the node held when
|
|
419
|
+
// the pending count was read is either mined by that block, and its effect is in the amounts, or counted unmined.
|
|
420
|
+
const unmined = await Promise.all(keys.map((k) => nonce(rpc, k, "pending", at.deadline)));
|
|
421
|
+
const head = await latest(rpc, at.deadline);
|
|
422
|
+
const [now, mined] = await Promise.all([amountsAt(head), Promise.all(keys.map((k) => nonce(rpc, k, head, at.deadline)))]);
|
|
423
|
+
const held = unmined.some((n, i) => n > mined[i]);
|
|
424
|
+
if (stands(m, now))
|
|
425
|
+
return answer({ state: "pending", reason: held ? "below-mark" : "instrument-live", foreign: [] });
|
|
426
|
+
if (at.from === undefined)
|
|
427
|
+
return answer({ state: "pending", reason: "search-incomplete", foreign: [] });
|
|
428
|
+
const from = BigInt(String(at.from));
|
|
429
|
+
const logs = [];
|
|
430
|
+
for (let lo = from + 1n; lo <= head; lo += LOG_SPAN) {
|
|
431
|
+
const hi = lo + LOG_SPAN - 1n < head ? lo + LOG_SPAN - 1n : head;
|
|
432
|
+
logs.push(...(await reader.logs({ address: m.escrow, topics: [null, hash], fromBlock: lo, toBlock: hi })));
|
|
433
|
+
}
|
|
434
|
+
logs.sort(order);
|
|
435
|
+
// The amounts at `from`, from those at the head with the events since undone: no state older than the head is read.
|
|
436
|
+
let s = logs.reduceRight(before, now);
|
|
437
|
+
if (past(m, s)) {
|
|
438
|
+
// Final only once `from` is finalized: above it, a reorganisation can still change what the amounts there were.
|
|
439
|
+
return from > finalized
|
|
440
|
+
? answer({ state: "pending", reason: "below-mark", foreign: [] })
|
|
441
|
+
: unlessOwn(answer({ state: "failed", reason: "before-position", finality: "finalized", foreign: [] }), logs);
|
|
442
|
+
}
|
|
443
|
+
let consumed;
|
|
444
|
+
for (const log of logs) {
|
|
445
|
+
const prior = s;
|
|
446
|
+
s = after(s, log);
|
|
447
|
+
if (stands(m, prior) && ofMessage(log)) {
|
|
448
|
+
return log.blockNumber > finalized || !(await executedIn(rpc, log, at.deadline))
|
|
449
|
+
? answer({ state: "pending", reason: "below-mark", transaction: log.transactionHash, foreign: [] })
|
|
450
|
+
: landed(log.transactionHash);
|
|
451
|
+
}
|
|
452
|
+
if (consumed === undefined && past(m, s))
|
|
453
|
+
consumed = log;
|
|
454
|
+
}
|
|
455
|
+
if (consumed === undefined)
|
|
456
|
+
return answer({ state: "pending", reason: "search-incomplete", foreign: [] });
|
|
457
|
+
return consumed.blockNumber > finalized || !(await executedIn(rpc, consumed, at.deadline))
|
|
458
|
+
? answer({ state: "pending", reason: "below-mark", transaction: consumed.transactionHash, foreign: [] })
|
|
459
|
+
: unlessOwn(answer({ state: "failed", reason: "instrument-consumed", transaction: consumed.transactionHash, finality: "finalized", foreign: [] }), logs);
|
|
460
|
+
}
|
|
461
|
+
catch (e) {
|
|
462
|
+
if (e instanceof ReaderError && e.kind === "history")
|
|
463
|
+
return unreadable("history-gap");
|
|
464
|
+
if (e instanceof ReaderError && e.kind === "malformed")
|
|
465
|
+
return unreadable("unparseable");
|
|
466
|
+
return unreadable("transport");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* A capture read as also voiding the payment's remainder where `voids` says its row's claim carries that void (the
|
|
471
|
+
* module records it before the send), whether or not the body presented now carries it.
|
|
472
|
+
*/
|
|
473
|
+
function withVoid(m, voids) {
|
|
474
|
+
return m !== undefined && voids && m.type === "capture" ? { ...m, voids: true } : m;
|
|
475
|
+
}
|
|
476
|
+
/** The payment's hash as the escrow's `getHash` gives it, at the latest block. */
|
|
477
|
+
async function hashOf(reader, rpc, m, deadline) {
|
|
478
|
+
const word = await reader.call(m.escrow, encodeFunctionData({ abi: ESCROW, functionName: "getHash", args: [m.info] }), await latest(rpc, deadline));
|
|
479
|
+
return decodeFunctionResult({ abi: ESCROW, functionName: "getHash", data: word }).toLowerCase();
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* For a capture whose receiver authorizer also signed the void of the payment's remainder: the one of `named`, the
|
|
483
|
+
* transactions earlier settles of its claim named, whose receipt shows the escrow's event of this capture, where one
|
|
484
|
+
* does; undefined otherwise, and for any other message.
|
|
485
|
+
*/
|
|
486
|
+
export async function captureLanded(reader, rpc, p, named, deadline, voids = false) {
|
|
487
|
+
const m = withVoid(messageOf(p), voids);
|
|
488
|
+
if (m?.voids !== true || named.length === 0)
|
|
489
|
+
return undefined;
|
|
490
|
+
const hash = await hashOf(reader, rpc, m, deadline);
|
|
491
|
+
for (const t of [...named].reverse()) {
|
|
492
|
+
const r = await reader.receipt(t.toLowerCase());
|
|
493
|
+
if (r !== null && r.status === 1 && r.logs.some((l) => l.address === m.escrow && l.topics[1] === hash && isOf(m, l)))
|
|
494
|
+
return t.toLowerCase();
|
|
495
|
+
}
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Whether a capture's payload also voids the payment's remainder, as x402's `AuthCaptureEvmScheme` of `@x402/evm`
|
|
500
|
+
* 2.31.0 reads it: a `voidAuthorizerSignature` that is not empty (the class tests it for truth), or, for a delegated
|
|
501
|
+
* receiver authorizer, `voidRemainder` true, which the class signs as a void beside the capture.
|
|
502
|
+
*/
|
|
503
|
+
export function carriesVoid(body) {
|
|
504
|
+
const signature = body["voidAuthorizerSignature"];
|
|
505
|
+
return (typeof signature === "string" && signature !== "") || body["voidRemainder"] === true;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* The void of the payment's remainder a capture carries (`carriesVoid`), as the lifecycle manager sends a void: its
|
|
509
|
+
* type, the payment, the salt nonce, and the receiver authorizer's signature of the void where the capture carries one;
|
|
510
|
+
* undefined for any other payload.
|
|
511
|
+
*/
|
|
512
|
+
export function voidOf(p) {
|
|
513
|
+
const body = p.payload.payload;
|
|
514
|
+
if (body?.["type"] !== "capture" || !carriesVoid(body))
|
|
515
|
+
return undefined;
|
|
516
|
+
const signature = body["voidAuthorizerSignature"];
|
|
517
|
+
// Where the class signs for a delegated receiver authorizer, the capture carries `voidRemainder` and no signature:
|
|
518
|
+
// its void carries none either, and the class signs it as it signs every delegated void.
|
|
519
|
+
const payload = {
|
|
520
|
+
type: "void",
|
|
521
|
+
paymentInfo: body["paymentInfo"],
|
|
522
|
+
saltNonce: body["saltNonce"],
|
|
523
|
+
...(typeof signature === "string" && signature !== "" ? { authorizerSignature: signature } : {}),
|
|
524
|
+
};
|
|
525
|
+
return { payload: { ...p.payload, payload }, requirements: p.requirements };
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Whether the finalized chain shows the log's transaction executed in the log's own block: its receipt on the finalized
|
|
529
|
+
* chain (`finalizedReceipt`), status 1, in the block at the log's number. A log answered from a node on another fork
|
|
530
|
+
* whose transaction the finalized chain holds elsewhere, or reverted, is not.
|
|
531
|
+
*/
|
|
532
|
+
async function executedIn(rpc, log, deadline) {
|
|
533
|
+
const r = await finalizedReceipt(rpc, log.transactionHash, deadline);
|
|
534
|
+
return r !== undefined && r.status === 1 && r.blockNumber === log.blockNumber;
|
|
535
|
+
}
|
|
536
|
+
function order(a, b) {
|
|
537
|
+
return a.blockNumber === b.blockNumber ? a.logIndex - b.logIndex : a.blockNumber < b.blockNumber ? -1 : 1;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* What the node shows of a transaction by its hash: `mined` where it names the block that holds it, `held` where it
|
|
541
|
+
* shows it with no block, `unshown` where it answers nothing for it, which is no fact about the chain.
|
|
542
|
+
*/
|
|
543
|
+
async function shownAs(rpc, transaction, deadline) {
|
|
544
|
+
const t = await rpcCall(rpc, "eth_getTransactionByHash", [transaction], deadline);
|
|
545
|
+
if (t === null)
|
|
546
|
+
return "unshown";
|
|
547
|
+
if (!isObject(t))
|
|
548
|
+
throw new ReaderError("malformed", "the node's transaction is not one JSON-RPC defines");
|
|
549
|
+
return t["blockNumber"] === null || t["blockNumber"] === undefined ? "held" : "mined";
|
|
550
|
+
}
|
|
551
|
+
/** The amounts of the payment `hash` in `escrow` at the block `at`, named by its hash (EIP-1898), which must be canonical. */
|
|
552
|
+
async function amountsAtHash(rpc, escrow, hash, at, deadline) {
|
|
553
|
+
const block = { blockHash: at.hash, requireCanonical: true };
|
|
554
|
+
const out = await rpcCall(rpc, "eth_call", [{ to: escrow, data: encodeFunctionData({ abi: ESCROW, functionName: "paymentState", args: [hash] }) }, block], deadline);
|
|
555
|
+
if (typeof out !== "string" || !/^0x([0-9a-fA-F]{2})*$/.test(out))
|
|
556
|
+
throw new ReaderError("malformed", "the escrow's payment state is not data");
|
|
557
|
+
const [collected, capturable, refundable] = decodeFunctionResult({ abi: ESCROW, functionName: "paymentState", data: out });
|
|
558
|
+
return { collected, capturable, refundable };
|
|
559
|
+
}
|
|
560
|
+
async function latest(rpc, deadline) {
|
|
561
|
+
const n = await rpcCall(rpc, "eth_blockNumber", [], deadline);
|
|
562
|
+
if (typeof n !== "string" || !/^0x[0-9a-fA-F]{1,64}$/.test(n))
|
|
563
|
+
throw new ReaderError("malformed", "the node's block number is not a quantity");
|
|
564
|
+
return BigInt(n);
|
|
565
|
+
}
|
|
566
|
+
async function nonce(rpc, account, at, deadline) {
|
|
567
|
+
const n = await rpcCall(rpc, "eth_getTransactionCount", [account, typeof at === "bigint" ? toHex(at) : at], deadline);
|
|
568
|
+
if (typeof n !== "string" || !/^0x[0-9a-fA-F]{1,64}$/.test(n))
|
|
569
|
+
throw new ReaderError("malformed", "the node's transaction count is not a quantity");
|
|
570
|
+
return BigInt(n);
|
|
571
|
+
}
|
|
572
|
+
/** The block the node names `finalized`, by its number and hash. */
|
|
573
|
+
export async function finalizedBlock(rpc, deadline) {
|
|
574
|
+
const b = await rpcCall(rpc, "eth_getBlockByNumber", ["finalized", false], deadline);
|
|
575
|
+
if (b === null || typeof b !== "object")
|
|
576
|
+
throw new ReaderError("history", "the node names no finalized block");
|
|
577
|
+
const { number, hash } = b;
|
|
578
|
+
if (typeof number !== "string" || !/^0x[0-9a-fA-F]{1,64}$/.test(number) || typeof hash !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(hash)) {
|
|
579
|
+
throw new ReaderError("malformed", "the node's finalized block names no number and hash");
|
|
580
|
+
}
|
|
581
|
+
return { number: BigInt(number), hash: hash.toLowerCase() };
|
|
582
|
+
}
|
|
583
|
+
/** `account`'s transaction count at the block `at`, named by its hash (EIP-1898), which must be canonical. */
|
|
584
|
+
export async function countAt(rpc, account, at, deadline) {
|
|
585
|
+
const n = await rpcCall(rpc, "eth_getTransactionCount", [account, { blockHash: at.hash, requireCanonical: true }], deadline);
|
|
586
|
+
if (typeof n !== "string" || !/^0x[0-9a-fA-F]{1,64}$/.test(n))
|
|
587
|
+
throw new ReaderError("malformed", "the node's transaction count is not a quantity");
|
|
588
|
+
return BigInt(n);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Whether the escrow's own state at the block `at`, named by its hash (EIP-1898), shows the message `p` unexecuted:
|
|
592
|
+
* the payment's pair (`capturableAmount`, `refundableAmount`) is the one the message expects, and for a void the
|
|
593
|
+
* capturable amount is above zero. The escrow's pair falls strictly (ordered by the capturable amount, then the
|
|
594
|
+
* refundable) with every call that executes, and a call that reverts leaves it: so a pair, once left, never returns,
|
|
595
|
+
* and the pair a message expects standing at a block shows that no execution of that message (of any copy of it) is
|
|
596
|
+
* in that block or before it. False where the payload is no capture, void or refund the escrow takes.
|
|
597
|
+
*/
|
|
598
|
+
export async function unexecutedAt(rpc, p, at, deadline) {
|
|
599
|
+
const m = messageOf(p);
|
|
600
|
+
if (m === undefined)
|
|
601
|
+
return false;
|
|
602
|
+
const block = { blockHash: at.hash, requireCanonical: true };
|
|
603
|
+
const word = await rpcCall(rpc, "eth_call", [{ to: m.escrow, data: encodeFunctionData({ abi: ESCROW, functionName: "getHash", args: [m.info] }) }, block], deadline);
|
|
604
|
+
if (typeof word !== "string" || !/^0x([0-9a-fA-F]{2})*$/.test(word))
|
|
605
|
+
throw new ReaderError("malformed", "the escrow's payment hash is not data");
|
|
606
|
+
const hash = decodeFunctionResult({ abi: ESCROW, functionName: "getHash", data: word });
|
|
607
|
+
const out = await rpcCall(rpc, "eth_call", [{ to: m.escrow, data: encodeFunctionData({ abi: ESCROW, functionName: "paymentState", args: [hash] }) }, block], deadline);
|
|
608
|
+
if (typeof out !== "string" || !/^0x([0-9a-fA-F]{2})*$/.test(out))
|
|
609
|
+
throw new ReaderError("malformed", "the escrow's payment state is not data");
|
|
610
|
+
const [collected, capturable, refundable] = decodeFunctionResult({ abi: ESCROW, functionName: "paymentState", data: out });
|
|
611
|
+
return collected && stands(m, { collected, capturable, refundable });
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* The block an escrow's later message is read from: the latest block, read just before the settler claims the message,
|
|
615
|
+
* as a decimal string, so that anything the settler sends for it lands after it.
|
|
616
|
+
*/
|
|
617
|
+
export async function escrowPosition(rpc, deadline) {
|
|
618
|
+
return (await latest(rpc, deadline)).toString(10);
|
|
619
|
+
}
|