@payai/x402-evm 2.4.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/batch-settlement/client/file-storage.d.ts +47 -0
- package/dist/cjs/batch-settlement/client/file-storage.js +116 -0
- package/dist/cjs/batch-settlement/client/file-storage.js.map +1 -0
- package/dist/cjs/batch-settlement/client/index.d.ts +111 -0
- package/dist/cjs/batch-settlement/client/index.js +1565 -0
- package/dist/cjs/batch-settlement/client/index.js.map +1 -0
- package/dist/cjs/batch-settlement/facilitator/index.d.ts +71 -0
- package/dist/cjs/batch-settlement/facilitator/index.js +2032 -0
- package/dist/cjs/batch-settlement/facilitator/index.js.map +1 -0
- package/dist/cjs/batch-settlement/server/file-storage.d.ts +53 -0
- package/dist/cjs/batch-settlement/server/file-storage.js +181 -0
- package/dist/cjs/batch-settlement/server/file-storage.js.map +1 -0
- package/dist/cjs/batch-settlement/server/index.d.ts +491 -0
- package/dist/cjs/batch-settlement/server/index.js +1960 -0
- package/dist/cjs/batch-settlement/server/index.js.map +1 -0
- package/dist/cjs/batch-settlement/server/redis-storage.d.ts +87 -0
- package/dist/cjs/batch-settlement/server/redis-storage.js +181 -0
- package/dist/cjs/batch-settlement/server/redis-storage.js.map +1 -0
- package/dist/cjs/exact/client/index.d.ts +6 -4
- package/dist/cjs/exact/client/index.js +7 -5
- package/dist/cjs/exact/client/index.js.map +1 -1
- package/dist/cjs/exact/facilitator/index.d.ts +16 -9
- package/dist/cjs/exact/facilitator/index.js +35 -7
- package/dist/cjs/exact/facilitator/index.js.map +1 -1
- package/dist/cjs/exact/server/index.js +40 -1
- package/dist/cjs/exact/server/index.js.map +1 -1
- package/dist/cjs/exact/v1/client/index.d.ts +2 -1
- package/dist/cjs/exact/v1/client/index.js.map +1 -1
- package/dist/cjs/exact/v1/facilitator/index.d.ts +11 -5
- package/dist/cjs/exact/v1/facilitator/index.js +16 -2
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -1
- package/dist/cjs/index.d.ts +113 -7
- package/dist/cjs/index.js +1353 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/{esm/permit2-CyZxwngN.d.mts → cjs/permit2-DhJRUcgY.d.ts} +1 -13
- package/dist/cjs/rpc-DULZzRne.d.ts +13 -0
- package/dist/cjs/scheme-CvkPJXBD.d.ts +307 -0
- package/dist/{esm/scheme-DCR7hsa3.d.mts → cjs/scheme-DTQFE9xp.d.ts} +2 -2
- package/dist/{esm/signer-D912R4mq.d.mts → cjs/signer-tYS6Y46X.d.ts} +3 -0
- package/dist/cjs/storage-6W5MO46W.d.ts +50 -0
- package/dist/cjs/storage-Bl6aD0Xg.d.ts +81 -0
- package/dist/cjs/types-CF8P2-NM.d.ts +180 -0
- package/dist/cjs/upto/client/index.d.ts +5 -3
- package/dist/cjs/upto/client/index.js +7 -5
- package/dist/cjs/upto/client/index.js.map +1 -1
- package/dist/cjs/upto/facilitator/index.d.ts +2 -1
- package/dist/cjs/upto/facilitator/index.js +2 -1
- package/dist/cjs/upto/facilitator/index.js.map +1 -1
- package/dist/cjs/upto/server/index.js +40 -1
- package/dist/cjs/upto/server/index.js.map +1 -1
- package/dist/cjs/v1/index.d.ts +2 -1
- package/dist/cjs/v1/index.js.map +1 -1
- package/dist/esm/batch-settlement/client/file-storage.d.mts +47 -0
- package/dist/esm/batch-settlement/client/file-storage.mjs +63 -0
- package/dist/esm/batch-settlement/client/file-storage.mjs.map +1 -0
- package/dist/esm/batch-settlement/client/index.d.mts +111 -0
- package/dist/esm/batch-settlement/client/index.mjs +59 -0
- package/dist/esm/batch-settlement/client/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/facilitator/index.d.mts +71 -0
- package/dist/esm/batch-settlement/facilitator/index.mjs +1235 -0
- package/dist/esm/batch-settlement/facilitator/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/file-storage.d.mts +53 -0
- package/dist/esm/batch-settlement/server/file-storage.mjs +128 -0
- package/dist/esm/batch-settlement/server/file-storage.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/index.d.mts +491 -0
- package/dist/esm/batch-settlement/server/index.mjs +1645 -0
- package/dist/esm/batch-settlement/server/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/redis-storage.d.mts +87 -0
- package/dist/esm/batch-settlement/server/redis-storage.mjs +156 -0
- package/dist/esm/batch-settlement/server/redis-storage.mjs.map +1 -0
- package/dist/esm/chunk-2EUQTNJO.mjs +38 -0
- package/dist/esm/chunk-2EUQTNJO.mjs.map +1 -0
- package/dist/esm/chunk-53USC5VE.mjs +47 -0
- package/dist/esm/chunk-53USC5VE.mjs.map +1 -0
- package/dist/esm/{chunk-GJ57SZGI.mjs → chunk-6WQOGWBE.mjs} +7 -5
- package/dist/esm/{chunk-GJ57SZGI.mjs.map → chunk-6WQOGWBE.mjs.map} +1 -1
- package/dist/esm/{chunk-F3OOHBAW.mjs → chunk-BTYNCDNS.mjs} +42 -2
- package/dist/esm/{chunk-F3OOHBAW.mjs.map → chunk-BTYNCDNS.mjs.map} +1 -1
- package/dist/esm/{chunk-ERK2ZPOY.mjs → chunk-CSQS7ZON.mjs} +27 -7
- package/dist/esm/chunk-CSQS7ZON.mjs.map +1 -0
- package/dist/esm/chunk-GD4MKCN7.mjs +57 -0
- package/dist/esm/chunk-GD4MKCN7.mjs.map +1 -0
- package/dist/esm/chunk-HYABYUBD.mjs +432 -0
- package/dist/esm/chunk-HYABYUBD.mjs.map +1 -0
- package/dist/esm/chunk-IN5YIT5C.mjs +159 -0
- package/dist/esm/chunk-IN5YIT5C.mjs.map +1 -0
- package/dist/esm/{chunk-JII456TS.mjs → chunk-JK7SLLF7.mjs} +1 -1
- package/dist/esm/chunk-JK7SLLF7.mjs.map +1 -0
- package/dist/esm/{chunk-C4ZQMS77.mjs → chunk-MACPBXCT.mjs} +2 -216
- package/dist/esm/chunk-MACPBXCT.mjs.map +1 -0
- package/dist/esm/chunk-NKYVYGRA.mjs +911 -0
- package/dist/esm/chunk-NKYVYGRA.mjs.map +1 -0
- package/dist/esm/{chunk-FQJR4RCF.mjs → chunk-R7I3RZFF.mjs} +10 -6
- package/dist/esm/{chunk-FQJR4RCF.mjs.map → chunk-R7I3RZFF.mjs.map} +1 -1
- package/dist/esm/{chunk-CRT6YNY5.mjs → chunk-RWLVVO3B.mjs} +21 -61
- package/dist/esm/chunk-RWLVVO3B.mjs.map +1 -0
- package/dist/esm/chunk-TGFAVNUD.mjs +111 -0
- package/dist/esm/chunk-TGFAVNUD.mjs.map +1 -0
- package/dist/esm/chunk-TW7Z65AO.mjs +34 -0
- package/dist/esm/chunk-TW7Z65AO.mjs.map +1 -0
- package/dist/esm/chunk-U4HCGTLU.mjs +35 -0
- package/dist/esm/chunk-U4HCGTLU.mjs.map +1 -0
- package/dist/esm/chunk-VS3RYAYE.mjs +80 -0
- package/dist/esm/chunk-VS3RYAYE.mjs.map +1 -0
- package/dist/esm/chunk-W6ON4LG2.mjs +39 -0
- package/dist/esm/chunk-W6ON4LG2.mjs.map +1 -0
- package/dist/esm/{chunk-WKBC5YMI.mjs → chunk-YMQCTKDU.mjs} +23 -55
- package/dist/esm/chunk-YMQCTKDU.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +6 -4
- package/dist/esm/exact/client/index.mjs +10 -5
- package/dist/esm/exact/facilitator/index.d.mts +16 -9
- package/dist/esm/exact/facilitator/index.mjs +36 -14
- package/dist/esm/exact/facilitator/index.mjs.map +1 -1
- package/dist/esm/exact/server/index.mjs +1 -1
- package/dist/esm/exact/v1/client/index.d.mts +2 -1
- package/dist/esm/exact/v1/client/index.mjs +5 -2
- package/dist/esm/exact/v1/facilitator/index.d.mts +11 -5
- package/dist/esm/exact/v1/facilitator/index.mjs +5 -2
- package/dist/esm/index.d.mts +113 -7
- package/dist/esm/index.mjs +53 -7
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/permit2-DhJRUcgY.d.mts +729 -0
- package/dist/esm/rpc-DULZzRne.d.mts +13 -0
- package/dist/esm/scheme-DtbSS4Fk.d.mts +307 -0
- package/dist/esm/scheme-gtqAIYPJ.d.mts +47 -0
- package/dist/esm/signer-tYS6Y46X.d.mts +170 -0
- package/dist/esm/storage-6W5MO46W.d.mts +50 -0
- package/dist/esm/storage-sZ1CDS4P.d.mts +81 -0
- package/dist/esm/types-CF8P2-NM.d.mts +180 -0
- package/dist/esm/upto/client/index.d.mts +5 -3
- package/dist/esm/upto/client/index.mjs +9 -4
- package/dist/esm/upto/facilitator/index.d.mts +2 -1
- package/dist/esm/upto/facilitator/index.mjs +17 -9
- package/dist/esm/upto/facilitator/index.mjs.map +1 -1
- package/dist/esm/upto/server/index.mjs +1 -1
- package/dist/esm/v1/index.d.mts +2 -1
- package/dist/esm/v1/index.mjs +5 -2
- package/package.json +5 -5
- package/dist/esm/chunk-C4ZQMS77.mjs.map +0 -1
- package/dist/esm/chunk-CRT6YNY5.mjs.map +0 -1
- package/dist/esm/chunk-ERK2ZPOY.mjs.map +0 -1
- package/dist/esm/chunk-JII456TS.mjs.map +0 -1
- package/dist/esm/chunk-WKBC5YMI.mjs.map +0 -1
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
import { Network, SchemePaymentRequiredContext, SchemeNetworkServer, SchemeServerHooks, PaymentRequirements, DeepReadonly, PaymentPayload, MoneyParser, Price, AssetAmount } from '@payai/x402/types';
|
|
2
|
+
import { FacilitatorClient, SettleContext, SettleResultContext } from '@payai/x402/server';
|
|
3
|
+
import { h as BatchSettlementVoucherClaim, A as AuthorizerSigner } from '../../types-CF8P2-NM.mjs';
|
|
4
|
+
import { a as Channel, C as ChannelStorage } from '../../storage-sZ1CDS4P.mjs';
|
|
5
|
+
export { b as ChannelUpdateResult, I as InMemoryChannelStorage, P as PendingRequest } from '../../storage-sZ1CDS4P.mjs';
|
|
6
|
+
import 'viem';
|
|
7
|
+
|
|
8
|
+
interface ChannelManagerConfig {
|
|
9
|
+
scheme: BatchSettlementEvmScheme;
|
|
10
|
+
facilitator: FacilitatorClient;
|
|
11
|
+
receiver: `0x${string}`;
|
|
12
|
+
token: `0x${string}`;
|
|
13
|
+
network: Network;
|
|
14
|
+
}
|
|
15
|
+
type ClaimChannelSelector = (channels: Channel[], context: AutoSettlementContext) => Channel[] | Promise<Channel[]>;
|
|
16
|
+
interface ClaimOptions {
|
|
17
|
+
maxClaimsPerBatch?: number;
|
|
18
|
+
idleSecs?: number;
|
|
19
|
+
selectClaimChannels?: ClaimChannelSelector;
|
|
20
|
+
}
|
|
21
|
+
interface AutoSettlementConfig {
|
|
22
|
+
claimIntervalSecs?: number;
|
|
23
|
+
settleIntervalSecs?: number;
|
|
24
|
+
refundIntervalSecs?: number;
|
|
25
|
+
maxClaimsPerBatch?: number;
|
|
26
|
+
selectClaimChannels?: ClaimChannelSelector;
|
|
27
|
+
shouldSettle?: (context: AutoSettlementContext) => boolean | Promise<boolean>;
|
|
28
|
+
selectRefundChannels?: (channels: Channel[], context: AutoSettlementContext) => Channel[] | Promise<Channel[]>;
|
|
29
|
+
onClaim?: (result: ClaimResult) => void;
|
|
30
|
+
onSettle?: (result: SettleResult) => void;
|
|
31
|
+
onRefund?: (result: RefundResult) => void;
|
|
32
|
+
onError?: (error: unknown) => void;
|
|
33
|
+
}
|
|
34
|
+
interface AutoSettlementContext {
|
|
35
|
+
now: number;
|
|
36
|
+
lastClaimTime: number;
|
|
37
|
+
lastSettleTime: number;
|
|
38
|
+
pendingSettle: boolean;
|
|
39
|
+
}
|
|
40
|
+
interface ClaimResult {
|
|
41
|
+
vouchers: number;
|
|
42
|
+
transaction: string;
|
|
43
|
+
}
|
|
44
|
+
interface SettleResult {
|
|
45
|
+
transaction: string;
|
|
46
|
+
}
|
|
47
|
+
interface RefundResult {
|
|
48
|
+
channel: string;
|
|
49
|
+
transaction: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Manages the server-side channel lifecycle for the `batch-settlement` scheme:
|
|
53
|
+
* batch claiming of vouchers, settlement of claimed funds, and cooperative refund.
|
|
54
|
+
*
|
|
55
|
+
* Provides one-shot operations (`claim()`, `settle()`, `claimAndSettle()`,
|
|
56
|
+
* `refundIdleChannels()`) and an optional interval runner.
|
|
57
|
+
*/
|
|
58
|
+
declare class BatchSettlementChannelManager {
|
|
59
|
+
private readonly scheme;
|
|
60
|
+
private readonly facilitator;
|
|
61
|
+
private readonly receiver;
|
|
62
|
+
private readonly token;
|
|
63
|
+
private readonly network;
|
|
64
|
+
private timers;
|
|
65
|
+
private lastClaimTime;
|
|
66
|
+
private lastSettleTime;
|
|
67
|
+
private pendingSettle;
|
|
68
|
+
private running;
|
|
69
|
+
private pendingJobs;
|
|
70
|
+
private drainingJobs;
|
|
71
|
+
private autoSettleConfig;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new channel manager.
|
|
74
|
+
*
|
|
75
|
+
* @param config - Manager configuration: scheme, facilitator, receiver, token, network.
|
|
76
|
+
*/
|
|
77
|
+
constructor(config: ChannelManagerConfig);
|
|
78
|
+
/**
|
|
79
|
+
* Collects claimable vouchers and submits them in batches to the facilitator via `claim()`.
|
|
80
|
+
*
|
|
81
|
+
* @param opts - Optional claim execution and target selection options.
|
|
82
|
+
* @param opts.maxClaimsPerBatch - Max vouchers per facilitator `claim` batch.
|
|
83
|
+
* @param opts.idleSecs - When set, only include channels idle for at least this many seconds.
|
|
84
|
+
* @param opts.selectClaimChannels - Optional selector for choosing channels before claimability checks.
|
|
85
|
+
* @returns Array of claim results (one per batch).
|
|
86
|
+
*/
|
|
87
|
+
claim(opts?: ClaimOptions): Promise<ClaimResult[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Transfers claimed (but unsettled) funds to the receiver by calling `settle(receiver, token)`.
|
|
90
|
+
*
|
|
91
|
+
* @returns Settle result with the transaction hash.
|
|
92
|
+
*/
|
|
93
|
+
settle(): Promise<SettleResult>;
|
|
94
|
+
/**
|
|
95
|
+
* Convenience: claims all eligible vouchers then settles in one call.
|
|
96
|
+
*
|
|
97
|
+
* @param opts - Optional claim execution and target selection options.
|
|
98
|
+
* @param opts.maxClaimsPerBatch - Max vouchers per claim batch before settling.
|
|
99
|
+
* @param opts.idleSecs - When set, only include channels idle for at least this many seconds.
|
|
100
|
+
* @param opts.selectClaimChannels - Optional selector for choosing channels before claimability checks.
|
|
101
|
+
* @returns Combined claim and settle results.
|
|
102
|
+
*/
|
|
103
|
+
claimAndSettle(opts?: ClaimOptions): Promise<{
|
|
104
|
+
claims: ClaimResult[];
|
|
105
|
+
settle?: SettleResult;
|
|
106
|
+
}>;
|
|
107
|
+
/**
|
|
108
|
+
* Initiates cooperative refunds for one or more channels.
|
|
109
|
+
*
|
|
110
|
+
* @param channelIds - Specific channels to refund; defaults to all sessions.
|
|
111
|
+
* @returns One result per successfully refunded channel.
|
|
112
|
+
*/
|
|
113
|
+
refund(channelIds?: string[]): Promise<RefundResult[]>;
|
|
114
|
+
/**
|
|
115
|
+
* Refunds idle channels with non-zero balances.
|
|
116
|
+
*
|
|
117
|
+
* @param opts - Idle refund options.
|
|
118
|
+
* @param opts.idleSecs - Minimum seconds since the last request.
|
|
119
|
+
* @returns One result per successfully refunded channel.
|
|
120
|
+
*/
|
|
121
|
+
refundIdleChannels(opts: {
|
|
122
|
+
idleSecs: number;
|
|
123
|
+
}): Promise<RefundResult[]>;
|
|
124
|
+
/**
|
|
125
|
+
* Collects vouchers that are eligible for onchain claiming.
|
|
126
|
+
*
|
|
127
|
+
* A voucher is claimable when its `chargedCumulativeAmount` exceeds what has already
|
|
128
|
+
* been claimed onchain. An optional idle filter skips sessions that received a
|
|
129
|
+
* request within the last `idleSecs` seconds.
|
|
130
|
+
*
|
|
131
|
+
* @param opts - Optional filtering: `idleSecs` to only return idle channels.
|
|
132
|
+
* @param opts.idleSecs - Minimum seconds since last request for a channel to be included.
|
|
133
|
+
* @returns Array of {@link BatchSettlementVoucherClaim} entries for batch submission.
|
|
134
|
+
*/
|
|
135
|
+
getClaimableVouchers(opts?: {
|
|
136
|
+
idleSecs?: number;
|
|
137
|
+
}): Promise<BatchSettlementVoucherClaim[]>;
|
|
138
|
+
/**
|
|
139
|
+
* Returns channels that have a pending payer-initiated withdrawal.
|
|
140
|
+
*
|
|
141
|
+
* @returns All stored channel records with `withdrawRequestedAt` set.
|
|
142
|
+
*/
|
|
143
|
+
getWithdrawalPendingSessions(): Promise<Channel[]>;
|
|
144
|
+
/**
|
|
145
|
+
* Starts auto-settlement jobs for configured claim, settle, and refund intervals.
|
|
146
|
+
*
|
|
147
|
+
* @param config - Auto-settlement policy configuration.
|
|
148
|
+
*/
|
|
149
|
+
start(config?: AutoSettlementConfig): void;
|
|
150
|
+
/**
|
|
151
|
+
* Stops the auto-settlement loop.
|
|
152
|
+
*
|
|
153
|
+
* @param opts - Stop options.
|
|
154
|
+
* @param opts.flush - When true, run `claimAndSettle` before stopping.
|
|
155
|
+
* @returns Resolves when the loop is stopped (and flush work completes, if requested).
|
|
156
|
+
*/
|
|
157
|
+
stop(opts?: {
|
|
158
|
+
flush?: boolean;
|
|
159
|
+
}): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* Refunds a single channel and removes it from storage after success.
|
|
162
|
+
*
|
|
163
|
+
* @param target - Channel to refund.
|
|
164
|
+
* @returns Successful refund transaction.
|
|
165
|
+
*/
|
|
166
|
+
private refundChannel;
|
|
167
|
+
/**
|
|
168
|
+
* Starts a recurring timer for one auto job.
|
|
169
|
+
*
|
|
170
|
+
* @param job - Job to enqueue when the interval fires.
|
|
171
|
+
* @param intervalSecs - Timer interval in seconds.
|
|
172
|
+
*/
|
|
173
|
+
private startAutoTimer;
|
|
174
|
+
/**
|
|
175
|
+
* Adds an auto job to the coalescing queue.
|
|
176
|
+
*
|
|
177
|
+
* @param job - Job to run.
|
|
178
|
+
*/
|
|
179
|
+
private enqueueJob;
|
|
180
|
+
/**
|
|
181
|
+
* Drains queued auto jobs in priority order.
|
|
182
|
+
*/
|
|
183
|
+
private drainJobs;
|
|
184
|
+
/**
|
|
185
|
+
* Returns the highest-priority queued auto job.
|
|
186
|
+
*
|
|
187
|
+
* @returns Next job to run.
|
|
188
|
+
*/
|
|
189
|
+
private nextPendingJob;
|
|
190
|
+
/**
|
|
191
|
+
* Runs one auto job.
|
|
192
|
+
*
|
|
193
|
+
* @param job - Job to run.
|
|
194
|
+
*/
|
|
195
|
+
private runAutoJob;
|
|
196
|
+
/**
|
|
197
|
+
* Runs the claim auto job.
|
|
198
|
+
*/
|
|
199
|
+
private runClaimJob;
|
|
200
|
+
/**
|
|
201
|
+
* Runs the settlement auto job.
|
|
202
|
+
*/
|
|
203
|
+
private runSettleJob;
|
|
204
|
+
/**
|
|
205
|
+
* Runs the refund auto job.
|
|
206
|
+
*/
|
|
207
|
+
private runRefundJob;
|
|
208
|
+
/**
|
|
209
|
+
* Claims vouchers from a provided channel snapshot.
|
|
210
|
+
*
|
|
211
|
+
* @param channels - Channels to inspect for claimable vouchers.
|
|
212
|
+
* @param opts - Claim batching and filtering options.
|
|
213
|
+
* @param opts.maxClaimsPerBatch - Max vouchers per facilitator claim transaction.
|
|
214
|
+
* @param opts.idleSecs - Optional idle filter.
|
|
215
|
+
* @returns Claim results, one per submitted batch.
|
|
216
|
+
*/
|
|
217
|
+
private claimFromChannels;
|
|
218
|
+
/**
|
|
219
|
+
* Loads stored channels and applies the configured claim selector, if any.
|
|
220
|
+
*
|
|
221
|
+
* @param opts - Claim options containing an optional target selector.
|
|
222
|
+
* @returns The channel snapshot that should be inspected for claimable vouchers.
|
|
223
|
+
*/
|
|
224
|
+
private selectClaimTargets;
|
|
225
|
+
/**
|
|
226
|
+
* Refunds each eligible channel independently.
|
|
227
|
+
*
|
|
228
|
+
* @param channels - Channels to refund.
|
|
229
|
+
* @returns Successful refund results.
|
|
230
|
+
*/
|
|
231
|
+
private refundChannels;
|
|
232
|
+
/**
|
|
233
|
+
* Builds an outstanding voucher claim for a refund payload.
|
|
234
|
+
*
|
|
235
|
+
* @param channel - Channel being refunded.
|
|
236
|
+
* @returns Claim payloads needed before refunding unclaimed balance.
|
|
237
|
+
*/
|
|
238
|
+
private buildRefundClaims;
|
|
239
|
+
/**
|
|
240
|
+
* Builds the policy context passed to interval hooks.
|
|
241
|
+
*
|
|
242
|
+
* @param now - Current wall-clock time in milliseconds.
|
|
243
|
+
* @returns Auto-settlement policy context.
|
|
244
|
+
*/
|
|
245
|
+
private buildAutoSettlementContext;
|
|
246
|
+
/**
|
|
247
|
+
* Collects claimable vouchers from a provided channel snapshot.
|
|
248
|
+
*
|
|
249
|
+
* @param channels - Channels to inspect.
|
|
250
|
+
* @param opts - Optional idle filter.
|
|
251
|
+
* @param opts.idleSecs - Minimum seconds since last request.
|
|
252
|
+
* @returns Claimable voucher payloads.
|
|
253
|
+
*/
|
|
254
|
+
private getClaimableVouchersFromChannels;
|
|
255
|
+
/**
|
|
256
|
+
* Filters idle channels that can be cooperatively refunded.
|
|
257
|
+
*
|
|
258
|
+
* @param channels - Channels to inspect.
|
|
259
|
+
* @param idleSecs - Minimum seconds since the last request.
|
|
260
|
+
* @returns Idle refundable channels.
|
|
261
|
+
*/
|
|
262
|
+
private getIdleChannelsForRefundFromChannels;
|
|
263
|
+
/**
|
|
264
|
+
* Returns channels that have been idle longer than `idleSecs` and still have
|
|
265
|
+
* a non-zero balance (candidates for cooperative refund).
|
|
266
|
+
*
|
|
267
|
+
* @param idleSecs - Minimum seconds since last request for a session to count as idle.
|
|
268
|
+
* @returns Channels meeting the idle and balance criteria.
|
|
269
|
+
*/
|
|
270
|
+
private getIdleChannelsForRefund;
|
|
271
|
+
/**
|
|
272
|
+
* Submits a batch of voucher claims to the facilitator.
|
|
273
|
+
*
|
|
274
|
+
* @param claims - Voucher claims to send in one `type: "claim"` payload.
|
|
275
|
+
* @returns Per-batch claim summary (count and transaction hash).
|
|
276
|
+
*/
|
|
277
|
+
private submitClaim;
|
|
278
|
+
/**
|
|
279
|
+
* Builds a settlement payment payload for `settle(receiver, token)`.
|
|
280
|
+
*
|
|
281
|
+
* @returns Payload with `type: "settle"` and receiver/token fields.
|
|
282
|
+
*/
|
|
283
|
+
private buildSettlePaymentPayload;
|
|
284
|
+
/**
|
|
285
|
+
* Builds a minimal {@link PaymentRequirements} for channel manager operations.
|
|
286
|
+
*
|
|
287
|
+
* @returns Requirements describing batched operations for this manager.
|
|
288
|
+
*/
|
|
289
|
+
private buildPaymentRequirements;
|
|
290
|
+
/**
|
|
291
|
+
* Updates session records after a successful claim submission so that
|
|
292
|
+
* `getClaimableVouchers` no longer returns already-claimed vouchers.
|
|
293
|
+
*
|
|
294
|
+
* @param claims - Voucher claims that were included in the submitted settlement transaction.
|
|
295
|
+
*/
|
|
296
|
+
private updateClaimedSessions;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Adds server channel state to corrective 402 responses for cumulative mismatches.
|
|
301
|
+
*
|
|
302
|
+
* @param scheme - Owning `BatchSettlementEvmScheme` instance for storage access.
|
|
303
|
+
* @param ctx - Payment-required response context.
|
|
304
|
+
*/
|
|
305
|
+
declare function handleEnrichPaymentRequiredResponse(scheme: BatchSettlementEvmScheme, ctx: SchemePaymentRequiredContext): Promise<void>;
|
|
306
|
+
|
|
307
|
+
interface BatchSettlementEvmSchemeServerConfig {
|
|
308
|
+
storage?: ChannelStorage;
|
|
309
|
+
receiverAuthorizerSigner?: AuthorizerSigner;
|
|
310
|
+
withdrawDelay?: number;
|
|
311
|
+
onchainStateTtlMs?: number;
|
|
312
|
+
}
|
|
313
|
+
interface BatchSettlementRequestContext {
|
|
314
|
+
channelId?: string;
|
|
315
|
+
pendingId?: string;
|
|
316
|
+
channelSnapshot?: Channel;
|
|
317
|
+
localVerify?: boolean;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Server-side implementation of the `batch-settlement` scheme for EVM networks.
|
|
321
|
+
*/
|
|
322
|
+
declare class BatchSettlementEvmScheme implements SchemeNetworkServer {
|
|
323
|
+
readonly scheme: "batch-settlement";
|
|
324
|
+
readonly schemeHooks: SchemeServerHooks;
|
|
325
|
+
private readonly requestContexts;
|
|
326
|
+
private moneyParsers;
|
|
327
|
+
private readonly storage;
|
|
328
|
+
private readonly receiverAuthorizerSigner;
|
|
329
|
+
private readonly receiverAddress;
|
|
330
|
+
private readonly withdrawDelay;
|
|
331
|
+
private readonly onchainStateTtlMs;
|
|
332
|
+
/**
|
|
333
|
+
* Constructs a batched server scheme.
|
|
334
|
+
*
|
|
335
|
+
* @param receiverAddress - The server's receiver address (payTo).
|
|
336
|
+
* @param config - Optional configuration for storage, receiver-authorizer signer, and withdraw delay.
|
|
337
|
+
*/
|
|
338
|
+
constructor(receiverAddress: `0x${string}`, config?: BatchSettlementEvmSchemeServerConfig);
|
|
339
|
+
/**
|
|
340
|
+
* Adds server-owned settlement fields before facilitator settlement.
|
|
341
|
+
*
|
|
342
|
+
* @param ctx - Settlement context for the current payment.
|
|
343
|
+
* @returns Additive payload fields, or nothing when no enrichment is needed.
|
|
344
|
+
*/
|
|
345
|
+
enrichSettlementPayload: (ctx: SettleContext) => Promise<Record<string, unknown> | void>;
|
|
346
|
+
/**
|
|
347
|
+
* Adds corrective channel state to payment-required responses when available.
|
|
348
|
+
*
|
|
349
|
+
* @param ctx - Payment-required response context for the current request.
|
|
350
|
+
* @returns Updated payment requirements, or nothing when no enrichment is needed.
|
|
351
|
+
*/
|
|
352
|
+
enrichPaymentRequiredResponse: (ctx: Parameters<typeof handleEnrichPaymentRequiredResponse>[1]) => Promise<PaymentRequirements[] | void>;
|
|
353
|
+
/**
|
|
354
|
+
* Adds server-owned extra fields after facilitator settlement.
|
|
355
|
+
*
|
|
356
|
+
* @param ctx - Settlement result context for the current payment.
|
|
357
|
+
* @returns Additive response extra fields, or nothing when no enrichment is needed.
|
|
358
|
+
*/
|
|
359
|
+
enrichSettlementResponse: (ctx: SettleResultContext) => Promise<Record<string, unknown> | void>;
|
|
360
|
+
/**
|
|
361
|
+
* Merges batch-settlement state into the current request context.
|
|
362
|
+
*
|
|
363
|
+
* @param payload - Request-scoped payment payload object.
|
|
364
|
+
* @param context - Partial context fields to merge.
|
|
365
|
+
*/
|
|
366
|
+
mergeRequestContext(payload: DeepReadonly<PaymentPayload>, context: BatchSettlementRequestContext): void;
|
|
367
|
+
/**
|
|
368
|
+
* Reads batch-settlement state for the current request without clearing it.
|
|
369
|
+
*
|
|
370
|
+
* @param payload - Request-scoped payment payload object.
|
|
371
|
+
* @returns Request context, if one was recorded.
|
|
372
|
+
*/
|
|
373
|
+
readRequestContext(payload: DeepReadonly<PaymentPayload>): BatchSettlementRequestContext | undefined;
|
|
374
|
+
/**
|
|
375
|
+
* Reads and clears batch-settlement state for the current request.
|
|
376
|
+
*
|
|
377
|
+
* @param payload - Request-scoped payment payload object.
|
|
378
|
+
* @returns Request context, if one was recorded.
|
|
379
|
+
*/
|
|
380
|
+
takeRequestContext(payload: DeepReadonly<PaymentPayload>): BatchSettlementRequestContext | undefined;
|
|
381
|
+
/**
|
|
382
|
+
* Stores a channel snapshot for the current settlement request.
|
|
383
|
+
*
|
|
384
|
+
* @param payload - Request-scoped payment payload object.
|
|
385
|
+
* @param channel - Channel state to use during response enrichment.
|
|
386
|
+
*/
|
|
387
|
+
rememberChannelSnapshot(payload: DeepReadonly<PaymentPayload>, channel: Channel): void;
|
|
388
|
+
/**
|
|
389
|
+
* Reads and clears a channel snapshot for the current settlement request.
|
|
390
|
+
*
|
|
391
|
+
* @param payload - Request-scoped payment payload object.
|
|
392
|
+
* @returns Stored channel state, if one was recorded.
|
|
393
|
+
*/
|
|
394
|
+
takeChannelSnapshot(payload: DeepReadonly<PaymentPayload>): Channel | undefined;
|
|
395
|
+
/**
|
|
396
|
+
* Clears this request's pending reservation without touching newer reservations.
|
|
397
|
+
*
|
|
398
|
+
* @param payload - Request-scoped payment payload object.
|
|
399
|
+
*/
|
|
400
|
+
clearPendingRequest(payload: DeepReadonly<PaymentPayload>): Promise<void>;
|
|
401
|
+
/**
|
|
402
|
+
* Registers a custom money parser for converting price strings to token amounts.
|
|
403
|
+
*
|
|
404
|
+
* @param parser - A parser function to try before the default USD→token conversion.
|
|
405
|
+
* @returns `this` for chaining.
|
|
406
|
+
*/
|
|
407
|
+
registerMoneyParser(parser: MoneyParser): BatchSettlementEvmScheme;
|
|
408
|
+
/**
|
|
409
|
+
* Resolves a human-readable price (e.g. `"$0.01"`) into an onchain token amount.
|
|
410
|
+
*
|
|
411
|
+
* @param price - A price string, number, or explicit {@link AssetAmount}.
|
|
412
|
+
* @param network - CAIP-2 network identifier for looking up the default asset.
|
|
413
|
+
* @returns Token amount with asset address and metadata.
|
|
414
|
+
*/
|
|
415
|
+
parsePrice(price: Price, network: Network): Promise<AssetAmount>;
|
|
416
|
+
/**
|
|
417
|
+
* Injects batched-specific fields into the payment requirements returned to
|
|
418
|
+
* the client (receiverAuthorizer, withdrawDelay, EIP-712 domain info).
|
|
419
|
+
*
|
|
420
|
+
* @param paymentRequirements - Base payment requirements from the middleware.
|
|
421
|
+
* @param supportedKind - Matched scheme/network kind (extra may contain overrides).
|
|
422
|
+
* @param supportedKind.x402Version - Protocol version from the matched kind.
|
|
423
|
+
* @param supportedKind.scheme - Scheme name from the matched kind.
|
|
424
|
+
* @param supportedKind.network - Network identifier from the matched kind.
|
|
425
|
+
* @param supportedKind.extra - Optional extra fields on the matched kind.
|
|
426
|
+
* @param _extensionKeys - Extension keys (unused).
|
|
427
|
+
* @returns Enhanced payment requirements with batched fields in `extra`.
|
|
428
|
+
*/
|
|
429
|
+
enhancePaymentRequirements(paymentRequirements: PaymentRequirements, supportedKind: {
|
|
430
|
+
x402Version: number;
|
|
431
|
+
scheme: string;
|
|
432
|
+
network: Network;
|
|
433
|
+
extra?: Record<string, unknown>;
|
|
434
|
+
}, _extensionKeys: string[]): Promise<PaymentRequirements>;
|
|
435
|
+
/**
|
|
436
|
+
* Returns the underlying channel storage instance.
|
|
437
|
+
*
|
|
438
|
+
* @returns The configured {@link ChannelStorage} backend.
|
|
439
|
+
*/
|
|
440
|
+
getStorage(): ChannelStorage;
|
|
441
|
+
/**
|
|
442
|
+
* Returns the server's receiver address.
|
|
443
|
+
*
|
|
444
|
+
* @returns Receiver wallet address for the payment channel.
|
|
445
|
+
*/
|
|
446
|
+
getReceiverAddress(): `0x${string}`;
|
|
447
|
+
/**
|
|
448
|
+
* Returns the configured withdraw delay (seconds).
|
|
449
|
+
*
|
|
450
|
+
* @returns Withdraw delay in seconds before uncooperative withdrawal is allowed.
|
|
451
|
+
*/
|
|
452
|
+
getWithdrawDelay(): number;
|
|
453
|
+
/**
|
|
454
|
+
* Returns how long mirrored onchain channel state is trusted for local voucher verification.
|
|
455
|
+
*
|
|
456
|
+
* @returns Freshness window in milliseconds.
|
|
457
|
+
*/
|
|
458
|
+
getOnchainStateTtlMs(): number;
|
|
459
|
+
/**
|
|
460
|
+
* Returns the receiver-authorizer signer, if configured.
|
|
461
|
+
*
|
|
462
|
+
* @returns Receiver-authorizer signer, or `undefined` when not set.
|
|
463
|
+
*/
|
|
464
|
+
getReceiverAuthorizerSigner(): AuthorizerSigner | undefined;
|
|
465
|
+
/**
|
|
466
|
+
* Creates a {@link BatchSettlementChannelManager} pre-configured with this scheme's
|
|
467
|
+
* receiver, default token for the given network, and the provided facilitator.
|
|
468
|
+
*
|
|
469
|
+
* @param facilitator - Facilitator client for submitting onchain claims/settlements.
|
|
470
|
+
* @param network - CAIP-2 network identifier (e.g. `"eip155:84532"`).
|
|
471
|
+
* @returns A ready-to-use channel manager.
|
|
472
|
+
*/
|
|
473
|
+
createChannelManager(facilitator: FacilitatorClient, network: Network): BatchSettlementChannelManager;
|
|
474
|
+
/**
|
|
475
|
+
* Parses a human-readable money string (e.g. `"$1.50"`) into a decimal number.
|
|
476
|
+
*
|
|
477
|
+
* @param money - Money string (may include `$`) or numeric amount.
|
|
478
|
+
* @returns Parsed finite number.
|
|
479
|
+
*/
|
|
480
|
+
private parseMoneyToDecimal;
|
|
481
|
+
/**
|
|
482
|
+
* Converts a decimal dollar amount to the network's default token amount.
|
|
483
|
+
*
|
|
484
|
+
* @param amount - Decimal amount in display units.
|
|
485
|
+
* @param network - Target chain/network for default asset resolution.
|
|
486
|
+
* @returns {@link AssetAmount} with integer token amount, contract address, and metadata.
|
|
487
|
+
*/
|
|
488
|
+
private defaultMoneyConversion;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
export { AuthorizerSigner, type AutoSettlementConfig, type AutoSettlementContext, BatchSettlementChannelManager, BatchSettlementEvmScheme, type BatchSettlementEvmSchemeServerConfig, type BatchSettlementRequestContext, Channel, type ChannelManagerConfig, ChannelStorage, type ClaimChannelSelector, type ClaimOptions, type ClaimResult, type RefundResult, type SettleResult };
|