@pod-network/trade-sdk 0.2.0 → 0.4.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 +94 -2
- package/dist/client.d.ts +19 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +25 -13
- package/dist/client.js.map +1 -1
- package/dist/codec/bridge.d.ts +82 -0
- package/dist/codec/bridge.d.ts.map +1 -0
- package/dist/codec/bridge.js +114 -0
- package/dist/codec/bridge.js.map +1 -0
- package/dist/codec/decode.d.ts +15 -2
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +46 -6
- package/dist/codec/decode.js.map +1 -1
- package/dist/codec/direction.d.ts +10 -0
- package/dist/codec/direction.d.ts.map +1 -0
- package/dist/codec/direction.js +46 -0
- package/dist/codec/direction.js.map +1 -0
- package/dist/codec/orders-v2.d.ts +19 -0
- package/dist/codec/orders-v2.d.ts.map +1 -0
- package/dist/codec/orders-v2.js +200 -0
- package/dist/codec/orders-v2.js.map +1 -0
- package/dist/codec/units.d.ts +10 -0
- package/dist/codec/units.d.ts.map +1 -1
- package/dist/codec/units.js +25 -1
- package/dist/codec/units.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/sync/orders.d.ts +71 -16
- package/dist/sync/orders.d.ts.map +1 -1
- package/dist/sync/orders.js +202 -83
- package/dist/sync/orders.js.map +1 -1
- package/dist/sync/sources.d.ts +5 -1
- package/dist/sync/sources.d.ts.map +1 -1
- package/dist/sync/sources.js +16 -0
- package/dist/sync/sources.js.map +1 -1
- package/dist/sync/withdrawals.d.ts +5 -0
- package/dist/sync/withdrawals.d.ts.map +1 -0
- package/dist/sync/withdrawals.js +172 -0
- package/dist/sync/withdrawals.js.map +1 -0
- package/dist/transport/claim.d.ts +160 -0
- package/dist/transport/claim.d.ts.map +1 -0
- package/dist/transport/claim.js +341 -0
- package/dist/transport/claim.js.map +1 -0
- package/dist/transport/jsonrpc.d.ts +16 -0
- package/dist/transport/jsonrpc.d.ts.map +1 -0
- package/dist/transport/jsonrpc.js +42 -0
- package/dist/transport/jsonrpc.js.map +1 -0
- package/dist/transport/rest.d.ts +15 -1
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/transport/rest.js +25 -1
- package/dist/transport/rest.js.map +1 -1
- package/dist/transport/ws.d.ts +22 -2
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js +14 -2
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +212 -9
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +147 -41
- package/dist/types/wire.d.ts.map +1 -1
- package/dist/write/delegation.d.ts.map +1 -1
- package/dist/write/delegation.js +2 -12
- package/dist/write/delegation.js.map +1 -1
- package/dist/write/index.d.ts +26 -1
- package/dist/write/index.d.ts.map +1 -1
- package/dist/write/index.js +21 -14
- package/dist/write/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +33 -18
- package/src/codec/bridge.ts +138 -0
- package/src/codec/decode.ts +54 -10
- package/src/codec/direction.ts +36 -0
- package/src/codec/orders-v2.ts +222 -0
- package/src/codec/units.ts +24 -1
- package/src/index.ts +16 -0
- package/src/sync/orders.ts +203 -83
- package/src/sync/sources.ts +16 -1
- package/src/sync/withdrawals.ts +177 -0
- package/src/transport/claim.ts +434 -0
- package/src/transport/jsonrpc.ts +54 -0
- package/src/transport/rest.ts +35 -7
- package/src/transport/ws.ts +30 -4
- package/src/types/public.ts +233 -7
- package/src/types/wire.ts +161 -29
- package/src/write/delegation.ts +2 -12
- package/src/write/index.ts +59 -13
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Terminal withdrawal outcomes for one account (ADR 0033 §6): seeded and
|
|
2
|
+
// gap-filled over `GET /v1/bridge/withdrawals/{account}`, kept live by the
|
|
3
|
+
// `pod_withdrawals` subscription.
|
|
4
|
+
//
|
|
5
|
+
// The two surfaces serve the identical shape, so a backfilled outcome is
|
|
6
|
+
// indistinguishable from one that arrived live — which is what lets a reconnect
|
|
7
|
+
// be a plain "fetch the gap" rather than a separate code path.
|
|
8
|
+
//
|
|
9
|
+
// Outcomes are terminal and immutable: an id appears once and never changes, so
|
|
10
|
+
// merging is last-write-wins on the id with no reconciliation.
|
|
11
|
+
import { decodeWithdrawal } from "../codec/decode.js";
|
|
12
|
+
import { seedNowAndOnReconnect } from "./sources.js";
|
|
13
|
+
/** Page size for the backfill. The server caps at 1000 and defaults to 500. */
|
|
14
|
+
const PAGE_LIMIT = 500;
|
|
15
|
+
/** Pages to walk in one backfill before giving up, so a bad cursor can't spin
|
|
16
|
+
* forever. 50 pages × 500 covers 25k outcomes — far past any real account. */
|
|
17
|
+
const MAX_PAGES = 50;
|
|
18
|
+
/** Resubscribe attempts after a rejection before leaving the channel alone.
|
|
19
|
+
* Reached only when the rejection is permanent — see the `onError` comment. */
|
|
20
|
+
const MAX_SUB_RETRIES = 5;
|
|
21
|
+
export function withdrawalsSource({ rest, ws }, account) {
|
|
22
|
+
return (h) => {
|
|
23
|
+
let alive = true;
|
|
24
|
+
const byId = new Map();
|
|
25
|
+
// Cursor into the outcome log: the newest tick fully absorbed, plus the last
|
|
26
|
+
// id seen inside it. Both are needed because one tick can span a page — the
|
|
27
|
+
// deadline alone can only re-serve the tick's earlier rows or skip its later
|
|
28
|
+
// ones.
|
|
29
|
+
let sinceUs = 0;
|
|
30
|
+
let sinceId;
|
|
31
|
+
let retries = 0;
|
|
32
|
+
let retryTimer;
|
|
33
|
+
const publish = () => {
|
|
34
|
+
// Newest first: a user reads their most recent withdrawal at the top, and
|
|
35
|
+
// the id tiebreak keeps a same-tick pair from reordering between renders.
|
|
36
|
+
h.set([...byId.values()].sort((a, b) => b.timeUs - a.timeUs || b.id.localeCompare(a.id)));
|
|
37
|
+
};
|
|
38
|
+
const absorb = (items) => {
|
|
39
|
+
for (const w of items) {
|
|
40
|
+
byId.set(w.id, w);
|
|
41
|
+
// Never move the cursor backwards: an out-of-order arrival would
|
|
42
|
+
// otherwise re-serve ticks we already hold.
|
|
43
|
+
if (w.timeUs >= sinceUs) {
|
|
44
|
+
sinceUs = w.timeUs;
|
|
45
|
+
sinceId = w.id;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Walk forward from the cursor until the log is exhausted.
|
|
51
|
+
*
|
|
52
|
+
* Serialised on `inflight` because two callers race by construction: the
|
|
53
|
+
* immediate seed and the socket's `open` overlap on a cold start, and a
|
|
54
|
+
* reconnect can fire while a retry's backfill is still paging. Both share
|
|
55
|
+
* the cursor, so concurrent loops would re-request each other's pages.
|
|
56
|
+
*/
|
|
57
|
+
let inflight;
|
|
58
|
+
const backfill = () => (inflight ??= run().finally(() => { inflight = undefined; }));
|
|
59
|
+
const run = async () => {
|
|
60
|
+
try {
|
|
61
|
+
// Page against a cursor local to this run. The shared one is also moved
|
|
62
|
+
// by live pushes, and a push landing mid-backfill would jump the next
|
|
63
|
+
// page past the rest of an older tick — rows the forward-only cursor
|
|
64
|
+
// could then never come back for.
|
|
65
|
+
let pageSince = sinceUs;
|
|
66
|
+
let pageSinceId = sinceId;
|
|
67
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
68
|
+
const rows = await rest.bridgeWithdrawals(account, {
|
|
69
|
+
since: pageSince,
|
|
70
|
+
sinceId: pageSinceId,
|
|
71
|
+
limit: PAGE_LIMIT,
|
|
72
|
+
});
|
|
73
|
+
if (!alive)
|
|
74
|
+
return;
|
|
75
|
+
if (rows.length === 0)
|
|
76
|
+
break;
|
|
77
|
+
absorb(rows);
|
|
78
|
+
// Rows come back ascending, so the last one is this page's high-water
|
|
79
|
+
// mark — regardless of what the stream did to the shared cursor.
|
|
80
|
+
// Non-empty by the check above.
|
|
81
|
+
const last = rows[rows.length - 1];
|
|
82
|
+
pageSince = last.timeUs;
|
|
83
|
+
pageSinceId = last.id;
|
|
84
|
+
if (rows.length < PAGE_LIMIT)
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
// Once, after paging: publishing per page re-copies and re-sorts
|
|
88
|
+
// everything accumulated so far for a list the user sees for
|
|
89
|
+
// milliseconds — O(pages²) on exactly the accounts paging exists for.
|
|
90
|
+
//
|
|
91
|
+
// Unconditional, including when nothing was found. An account with no
|
|
92
|
+
// withdrawals must still reach a defined `[]`, or every consumer stays
|
|
93
|
+
// on "not loaded yet" and treats the first live outcome as its initial
|
|
94
|
+
// snapshot — silently swallowing a user's first-ever withdrawal.
|
|
95
|
+
publish();
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
// Publish on the failure path too, for exactly the reason the success
|
|
99
|
+
// path publishes unconditionally: a consumer left on "not loaded yet"
|
|
100
|
+
// treats the first live outcome as its initial snapshot and swallows it.
|
|
101
|
+
// A failed backfill is a reason to have no history — not a reason to
|
|
102
|
+
// misread the next thing that happens. `fail` records the error beside
|
|
103
|
+
// the value rather than replacing it, so a caller can read both.
|
|
104
|
+
publish();
|
|
105
|
+
// Only surface the failure when there is nothing to show; otherwise the
|
|
106
|
+
// stream is still feeding a usable list and a toast would be noise.
|
|
107
|
+
if (byId.size === 0)
|
|
108
|
+
h.fail(e);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const sub = ws.subscribe("pod_withdrawals",
|
|
112
|
+
// Live-only until a cursor exists. This is a replay channel, so the server
|
|
113
|
+
// accepts `since` only when its buffer still reaches back that far — and
|
|
114
|
+
// `since: 0` never does, so sending it would fail the subscribe every time
|
|
115
|
+
// and burn the retry budget below on a self-inflicted rejection. (The
|
|
116
|
+
// `since: 0` idiom belongs to the *state* channels, which answer it with a
|
|
117
|
+
// snapshot.) REST backfill covers history either way.
|
|
118
|
+
{ account }, (result) => {
|
|
119
|
+
retries = 0;
|
|
120
|
+
// A frame means the subscription is healthy, so a retry armed by an
|
|
121
|
+
// earlier rejection has nothing left to fix. Letting it fire would call
|
|
122
|
+
// `resubscribe()` on a live subscription, which drops the server's id
|
|
123
|
+
// without an `eth_unsubscribe` and leaks one subscription per occurrence.
|
|
124
|
+
if (retryTimer) {
|
|
125
|
+
clearTimeout(retryTimer);
|
|
126
|
+
retryTimer = undefined;
|
|
127
|
+
}
|
|
128
|
+
// One message per tick carrying that tick's outcomes for this account.
|
|
129
|
+
absorb(result.map(decodeWithdrawal));
|
|
130
|
+
publish();
|
|
131
|
+
sub.update({ since: sinceUs }); // keep the resume point fresh for a reconnect
|
|
132
|
+
}, () => {
|
|
133
|
+
// Rejected — usually a `since` older than the replay buffer. Back off,
|
|
134
|
+
// re-seed over REST (which has no such horizon), and resubscribe; after
|
|
135
|
+
// a couple of failures drop `since` and settle for live-only.
|
|
136
|
+
//
|
|
137
|
+
// Bounded, because the other reason for a rejection is a node that
|
|
138
|
+
// doesn't serve this channel at all — an older build, since the app
|
|
139
|
+
// ships ahead of the fleet. Retrying that forever is a request every
|
|
140
|
+
// 30s for the lifetime of the tab, so give up and leave the REST-seeded
|
|
141
|
+
// list in place rather than polling a node that will never answer.
|
|
142
|
+
if (!alive || retries >= MAX_SUB_RETRIES)
|
|
143
|
+
return;
|
|
144
|
+
retries++;
|
|
145
|
+
if (retryTimer)
|
|
146
|
+
clearTimeout(retryTimer);
|
|
147
|
+
retryTimer = setTimeout(() => {
|
|
148
|
+
void backfill().then(() => {
|
|
149
|
+
if (!alive)
|
|
150
|
+
return;
|
|
151
|
+
// Live-only until a real cursor exists (see the subscribe above), and
|
|
152
|
+
// dropped again after a couple of failures in case the replay buffer
|
|
153
|
+
// has outrun it.
|
|
154
|
+
sub.update({ since: retries > 2 || sinceUs === 0 ? undefined : sinceUs });
|
|
155
|
+
sub.resubscribe();
|
|
156
|
+
});
|
|
157
|
+
}, Math.min(30_000, 500 * 2 ** (retries - 1)));
|
|
158
|
+
});
|
|
159
|
+
// Seed immediately — REST needs no socket, so history paints without waiting
|
|
160
|
+
// on the handshake — and fill the gap again on every reconnect.
|
|
161
|
+
const offOpen = seedNowAndOnReconnect(ws, () => { if (alive)
|
|
162
|
+
void backfill(); });
|
|
163
|
+
return () => {
|
|
164
|
+
alive = false;
|
|
165
|
+
offOpen();
|
|
166
|
+
if (retryTimer)
|
|
167
|
+
clearTimeout(retryTimer);
|
|
168
|
+
sub.unsubscribe();
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=withdrawals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdrawals.js","sourceRoot":"","sources":["../../src/sync/withdrawals.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,2EAA2E;AAC3E,kCAAkC;AAClC,EAAE;AACF,yEAAyE;AACzE,gFAAgF;AAChF,+DAA+D;AAC/D,EAAE;AACF,gFAAgF;AAChF,+DAA+D;AAI/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,qBAAqB,EAAoB,MAAM,cAAc,CAAC;AAEvE,+EAA+E;AAC/E,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB;8EAC8E;AAC9E,MAAM,SAAS,GAAG,EAAE,CAAC;AAErB;+EAC+E;AAC/E,MAAM,eAAe,GAAG,CAAC,CAAC;AAE1B,MAAM,UAAU,iBAAiB,CAC/B,EAAE,IAAI,EAAE,EAAE,EAAe,EACzB,OAAgB;IAEhB,OAAO,CAAC,CAAC,EAAE,EAAE;QACX,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAsB,CAAC;QAC3C,6EAA6E;QAC7E,4EAA4E;QAC5E,6EAA6E;QAC7E,QAAQ;QACR,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,IAAI,OAAqC,CAAC;QAC1C,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,IAAI,UAAqD,CAAC;QAE1D,MAAM,OAAO,GAAG,GAAG,EAAE;YACnB,0EAA0E;YAC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,MAAM,OAAO,GAAG,qBAAqB,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,KAAK;YAAE,KAAK,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAEjF,OAAO,GAAG,EAAE;YACV,KAAK,GAAG,KAAK,CAAC;YACd,OAAO,EAAE,CAAC;YACV,IAAI,UAAU;gBAAE,YAAY,CAAC,UAAU,CAAC,CAAC;YACzC,GAAG,CAAC,WAAW,EAAE,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { Address, Hash } from "../types/public.js";
|
|
2
|
+
import { type RpcOptions } from "./jsonrpc.js";
|
|
3
|
+
/**
|
|
4
|
+
* topic0 of `Claim(bytes32 indexed txHash, address token, address mirrorToken,
|
|
5
|
+
* uint256 amount, address indexed to)` — protocol/src/Bridge.sol.
|
|
6
|
+
*
|
|
7
|
+
* A literal rather than a derivation so the read path doesn't pull in an ABI
|
|
8
|
+
* encoder; `claim.test.ts` pins it against viem's `toEventSelector`, so it
|
|
9
|
+
* cannot drift from the signature above without failing a test.
|
|
10
|
+
*/
|
|
11
|
+
export declare const CLAIM_TOPIC: Hash;
|
|
12
|
+
/** Where a withdrawal has got to on its way to the claim chain. */
|
|
13
|
+
export type ClaimStatus =
|
|
14
|
+
/** The certificate is still assembling (below the signature threshold). */
|
|
15
|
+
{
|
|
16
|
+
state: "pending";
|
|
17
|
+
}
|
|
18
|
+
/** Claimable. `claimHash` is the `Claim` event's indexed `txHash`. */
|
|
19
|
+
| {
|
|
20
|
+
state: "claimable";
|
|
21
|
+
claimHash: Hash;
|
|
22
|
+
}
|
|
23
|
+
/** Refused at execution: nothing was debited, so no `Claim` event can ever
|
|
24
|
+
* fire and the user can resubmit under a new id. Terminal. */
|
|
25
|
+
| {
|
|
26
|
+
state: "refused";
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Resolve one withdrawal's claim state from the pod node.
|
|
30
|
+
*
|
|
31
|
+
* **The precedence is deliberate and is the forward-compatibility contract — do
|
|
32
|
+
* not "simplify" it into a switch on `status`:**
|
|
33
|
+
*
|
|
34
|
+
* 1. A present `proof.claim_hash` wins, whatever `status` says. The money is in
|
|
35
|
+
* the certificate, not in the word beside it, and a status this build has
|
|
36
|
+
* never heard of must not veto one sitting in the same response.
|
|
37
|
+
* 2. Only then may `status` be terminal, and only `refused` is — meaning the
|
|
38
|
+
* withdrawal was rejected at execution with nothing debited.
|
|
39
|
+
* 3. Everything else is `pending`, i.e. "ask again": an unfamiliar status, a
|
|
40
|
+
* 404 (this node has no outcome row yet, normal right after submitting), any
|
|
41
|
+
* other HTTP failure, and a transport error. None of them is information
|
|
42
|
+
* about the withdrawal, and guessing `refused` would tell a user their funds
|
|
43
|
+
* were never taken when they may well have been.
|
|
44
|
+
*
|
|
45
|
+
* This mirrors the two properties the node's own `WithdrawalDetail` documents
|
|
46
|
+
* for consumers (node/src/rpc/bridge_rest.rs), which its producer type
|
|
47
|
+
* deliberately does not provide.
|
|
48
|
+
*/
|
|
49
|
+
export declare function fetchClaimStatus(restUrl: string, withdrawalId: Hash, opts?: RpcOptions): Promise<ClaimStatus>;
|
|
50
|
+
export interface WaitForClaimOptions extends RpcOptions {
|
|
51
|
+
/** pod REST base, as `PodTradeClient` takes it — `""` for a same-origin
|
|
52
|
+
* browser deployment. Stage 1 lives entirely on this surface; no JSON-RPC. */
|
|
53
|
+
restUrl: string;
|
|
54
|
+
/** Claim-chain JSON-RPC — where the `Claim` event is emitted. */
|
|
55
|
+
claimRpcUrl: string;
|
|
56
|
+
withdrawalId: Hash;
|
|
57
|
+
/** The bridge on the claim chain (`BridgeConfig.sourceContract`). Narrows the
|
|
58
|
+
* log query to one contract; omit only if it isn't known. */
|
|
59
|
+
bridge?: Address;
|
|
60
|
+
/** Give up after this long. Default 10 minutes. */
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
/** Default 5s. */
|
|
63
|
+
pollMs?: number;
|
|
64
|
+
/** How far before the current block the log scan starts, covering a relayer
|
|
65
|
+
* that claimed before the watch began. Default 5000.
|
|
66
|
+
*
|
|
67
|
+
* Worth lowering for a watch started as the withdrawal resolves: the claim
|
|
68
|
+
* cannot predate the outcome, so the window is known-empty, and this is both
|
|
69
|
+
* the largest query of the watch and the one public providers cap by range. */
|
|
70
|
+
lookbackBlocks?: number;
|
|
71
|
+
}
|
|
72
|
+
export type ClaimOutcome =
|
|
73
|
+
/** The funds landed. */
|
|
74
|
+
{
|
|
75
|
+
state: "claimed";
|
|
76
|
+
claimHash: Hash;
|
|
77
|
+
transactionHash: Hash;
|
|
78
|
+
}
|
|
79
|
+
/** Refused at execution; nothing was debited and nothing will arrive. */
|
|
80
|
+
| {
|
|
81
|
+
state: "refused";
|
|
82
|
+
}
|
|
83
|
+
/** Still outstanding when the timeout or the abort signal hit. Carries the
|
|
84
|
+
* claim hash if stage 1 got that far, so a caller can resume the watch. */
|
|
85
|
+
| {
|
|
86
|
+
state: "pending";
|
|
87
|
+
claimHash?: Hash;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Follow one withdrawal to the claim chain: resolve its claim hash, then watch
|
|
91
|
+
* for the `Claim` event carrying it.
|
|
92
|
+
*
|
|
93
|
+
* Matching on the hash rather than the recipient is what makes this exact —
|
|
94
|
+
* `Claim` indexes `to` as well, but that identifies the *user*, so two
|
|
95
|
+
* withdrawals of the same amount to the same address would be indistinguishable.
|
|
96
|
+
*
|
|
97
|
+
* Returns rather than throws on every expected ending, so a caller renders an
|
|
98
|
+
* outcome instead of catching. Pass `signal` to stop the polling when whatever
|
|
99
|
+
* asked for it goes away.
|
|
100
|
+
*/
|
|
101
|
+
export declare function waitForClaim(opts: WaitForClaimOptions): Promise<ClaimOutcome>;
|
|
102
|
+
export interface WatchClaimsOptions extends RpcOptions {
|
|
103
|
+
/** pod REST base, as `PodTradeClient` takes it. */
|
|
104
|
+
restUrl: string;
|
|
105
|
+
/** Claim-chain JSON-RPC — where the `Claim` event is emitted. */
|
|
106
|
+
claimRpcUrl: string;
|
|
107
|
+
/** The bridge on the claim chain (`BridgeConfig.sourceContract`). */
|
|
108
|
+
bridge?: Address;
|
|
109
|
+
/** Default 5s. */
|
|
110
|
+
pollMs?: number;
|
|
111
|
+
/** Called once per withdrawal, when it reaches a terminal state. Never called
|
|
112
|
+
* for one still outstanding when the watcher stops. */
|
|
113
|
+
onSettled: (withdrawalId: Hash, outcome: ClaimOutcome) => void;
|
|
114
|
+
}
|
|
115
|
+
export interface ClaimWatcher {
|
|
116
|
+
/**
|
|
117
|
+
* Follow one withdrawal. Ignored if it is already followed, so a caller may
|
|
118
|
+
* re-offer the same id freely. It is NOT ignored for an id that has already
|
|
119
|
+
* settled, because the watcher forgets those — deduplicating across
|
|
120
|
+
* settlement belongs to the caller, which is the only side that knows whether
|
|
121
|
+
* it acted on the outcome.
|
|
122
|
+
*
|
|
123
|
+
* `lookbackBlocks` covers a claim emitted before this id joined. It sets this
|
|
124
|
+
* id's OWN scan floor, and the shared query starts at the oldest floor among
|
|
125
|
+
* outstanding ids — so a range already scanned for others is re-scanned for
|
|
126
|
+
* this one, and no id's window is ever skipped on another's behalf.
|
|
127
|
+
*/
|
|
128
|
+
add(withdrawalId: Hash, opts?: {
|
|
129
|
+
lookbackBlocks?: number;
|
|
130
|
+
}): void;
|
|
131
|
+
/** How many withdrawals are still outstanding. */
|
|
132
|
+
readonly size: number;
|
|
133
|
+
stop(): void;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Follow many withdrawals on one pair of loops instead of one pair each.
|
|
137
|
+
*
|
|
138
|
+
* The saving is on the claim chain, which is the half that costs: a `Claim`
|
|
139
|
+
* topic filter is an OR set at `topics[1]`, so a single `eth_getLogs` covers
|
|
140
|
+
* every outstanding hash and that query's count stops scaling with the number of
|
|
141
|
+
* withdrawals. Stage 1 still costs one REST read per unresolved id, issued
|
|
142
|
+
* concurrently rather than in series — those go to pod's own node, not to a
|
|
143
|
+
* metered provider, so the thing worth collapsing is the log scan.
|
|
144
|
+
*
|
|
145
|
+
* **Each id carries its own scan floor, and the shared query starts at the
|
|
146
|
+
* oldest of them.** This is the part that is easy to get wrong: a withdrawal
|
|
147
|
+
* still waiting for its certificate has no hash to search for yet, so it
|
|
148
|
+
* contributes nothing to the topic set — but the blocks passing meanwhile are
|
|
149
|
+
* exactly where its `Claim` may land, because a relayer can claim from a
|
|
150
|
+
* certificate this node has not finished assembling. Advancing the floor on the
|
|
151
|
+
* strength of the ids that *do* have hashes would step over that window for
|
|
152
|
+
* good. So a floor is only raised for an id whose hash was actually in the query
|
|
153
|
+
* that covered it.
|
|
154
|
+
*
|
|
155
|
+
* Settlement arrives through `onSettled` rather than a promise per id, so a
|
|
156
|
+
* caller hears about each outcome when it happens rather than when the slowest
|
|
157
|
+
* one does.
|
|
158
|
+
*/
|
|
159
|
+
export declare function watchClaims(opts: WatchClaimsOptions): ClaimWatcher;
|
|
160
|
+
//# sourceMappingURL=claim.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claim.d.ts","sourceRoot":"","sources":["../../src/transport/claim.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAU,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAGvD;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,EAAE,IAC4C,CAAC;AAEvE,mEAAmE;AACnE,MAAM,MAAM,WAAW;AACrB,2EAA2E;AACzE;IAAE,KAAK,EAAE,SAAS,CAAA;CAAE;AACtB,sEAAsE;GACpE;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE;AACzC;8DAC8D;GAC5D;IAAE,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAEzB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,gBAAgB,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