@circle-fin/x402-batching 2.0.3
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 +123 -0
- package/dist/client/index.d.mts +653 -0
- package/dist/client/index.d.ts +653 -0
- package/dist/client/index.js +1313 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +1282 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +65 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +33 -0
- package/dist/index.mjs.map +1 -0
- package/dist/server/index.d.mts +318 -0
- package/dist/server/index.d.ts +318 -0
- package/dist/server/index.js +697 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +668 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/types-DnHgU28a.d.mts +90 -0
- package/dist/types-DnHgU28a.d.ts +90 -0
- package/package.json +95 -0
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
import { a as BatchPayload, b as BatchEvmSigner } from '../types-DnHgU28a.js';
|
|
2
|
+
import { Chain, Address, PublicClient, Transport, WalletClient, Account, Hex } from 'viem';
|
|
3
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* PaymentRequirements interface (minimal subset needed).
|
|
7
|
+
*/
|
|
8
|
+
interface PaymentRequirements$1 {
|
|
9
|
+
scheme: string;
|
|
10
|
+
network: string;
|
|
11
|
+
asset: string;
|
|
12
|
+
amount: string;
|
|
13
|
+
payTo: string;
|
|
14
|
+
maxTimeoutSeconds: number;
|
|
15
|
+
extra?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* PaymentPayload interface (minimal subset needed).
|
|
19
|
+
*/
|
|
20
|
+
interface PaymentPayload$1 {
|
|
21
|
+
x402Version: number;
|
|
22
|
+
payload: BatchPayload;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* SchemeNetworkClient interface from @x402/core.
|
|
26
|
+
*/
|
|
27
|
+
interface SchemeNetworkClient$1 {
|
|
28
|
+
readonly scheme: string;
|
|
29
|
+
createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements$1): Promise<Pick<PaymentPayload$1, 'x402Version' | 'payload'>>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Circle Batching EVM Scheme Client.
|
|
33
|
+
*
|
|
34
|
+
* A SchemeNetworkClient implementation for Circle Gateway batched payments.
|
|
35
|
+
* Signs EIP-3009 TransferWithAuthorization against the GatewayWallet contract
|
|
36
|
+
* (from `extra.verifyingContract`) instead of the USDC token contract.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import { x402Client } from "@x402/core/client";
|
|
41
|
+
* import { BatchEvmScheme } from "@circle-fin/x402-batching/client";
|
|
42
|
+
*
|
|
43
|
+
* const scheme = new BatchEvmScheme(evmSigner);
|
|
44
|
+
* const client = new x402Client();
|
|
45
|
+
* client.register("eip155:84532", scheme);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare class BatchEvmScheme implements SchemeNetworkClient$1 {
|
|
49
|
+
private readonly signer;
|
|
50
|
+
readonly scheme = "exact";
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new BatchEvmScheme.
|
|
53
|
+
*
|
|
54
|
+
* @param signer - The EVM signer for signing payment authorizations
|
|
55
|
+
*/
|
|
56
|
+
constructor(signer: BatchEvmSigner);
|
|
57
|
+
/**
|
|
58
|
+
* Creates a payment payload for Circle batching.
|
|
59
|
+
*
|
|
60
|
+
* @param x402Version - The x402 protocol version
|
|
61
|
+
* @param paymentRequirements - The payment requirements (must be a Circle batching option)
|
|
62
|
+
* @returns Promise resolving to the payment payload
|
|
63
|
+
* @throws Error if requirements are not a Circle batching option
|
|
64
|
+
*/
|
|
65
|
+
createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements$1): Promise<Pick<PaymentPayload$1, 'x402Version' | 'payload'>>;
|
|
66
|
+
/**
|
|
67
|
+
* Sign the EIP-3009 authorization using EIP-712.
|
|
68
|
+
* Uses the GatewayWallet contract as verifyingContract instead of USDC.
|
|
69
|
+
*/
|
|
70
|
+
private signAuthorization;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* PaymentRequirements interface (minimal subset needed).
|
|
75
|
+
*/
|
|
76
|
+
interface PaymentRequirements {
|
|
77
|
+
scheme: string;
|
|
78
|
+
network: string;
|
|
79
|
+
asset: string;
|
|
80
|
+
amount: string;
|
|
81
|
+
payTo: string;
|
|
82
|
+
maxTimeoutSeconds: number;
|
|
83
|
+
extra?: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* PaymentPayload interface (minimal subset needed).
|
|
87
|
+
*/
|
|
88
|
+
interface PaymentPayload {
|
|
89
|
+
x402Version: number;
|
|
90
|
+
payload: unknown;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* SchemeNetworkClient interface from @x402/core.
|
|
94
|
+
*/
|
|
95
|
+
interface SchemeNetworkClient {
|
|
96
|
+
readonly scheme: string;
|
|
97
|
+
createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Composite EVM Scheme that dispatches between BatchEvmScheme and a fallback.
|
|
101
|
+
*
|
|
102
|
+
* When both BatchEvmScheme and ExactEvmScheme register for the same `scheme`
|
|
103
|
+
* ("exact") on the same network wildcard ("eip155:*"), x402Client uses
|
|
104
|
+
* first-registered-wins. This means one scheme always shadows the other.
|
|
105
|
+
*
|
|
106
|
+
* CompositeEvmScheme solves this by registering once and dispatching at
|
|
107
|
+
* runtime: if the payment requirements include batching metadata
|
|
108
|
+
* (`supportsBatching()`), it delegates to BatchEvmScheme; otherwise it
|
|
109
|
+
* delegates to the fallback (typically ExactEvmScheme).
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* import { CompositeEvmScheme, BatchEvmScheme } from "@circle-fin/x402-batching/client";
|
|
114
|
+
* import { ExactEvmScheme } from "@x402/evm/exact/client";
|
|
115
|
+
*
|
|
116
|
+
* const composite = new CompositeEvmScheme(
|
|
117
|
+
* new BatchEvmScheme(signer),
|
|
118
|
+
* new ExactEvmScheme(signer),
|
|
119
|
+
* );
|
|
120
|
+
* client.register("eip155:*", composite);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare class CompositeEvmScheme implements SchemeNetworkClient {
|
|
124
|
+
private readonly batchScheme;
|
|
125
|
+
private readonly fallbackScheme;
|
|
126
|
+
readonly scheme = "exact";
|
|
127
|
+
constructor(batchScheme: BatchEvmScheme, fallbackScheme: SchemeNetworkClient);
|
|
128
|
+
createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* x402Client-like interface (minimal subset needed for registration).
|
|
133
|
+
*/
|
|
134
|
+
interface X402ClientLike {
|
|
135
|
+
register(network: string, client: {
|
|
136
|
+
readonly scheme: string;
|
|
137
|
+
}): unknown;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* SchemeNetworkClient-like interface (minimal subset for fallback).
|
|
141
|
+
*/
|
|
142
|
+
interface SchemeNetworkClientLike {
|
|
143
|
+
readonly scheme: string;
|
|
144
|
+
createPaymentPayload(x402Version: number, paymentRequirements: {
|
|
145
|
+
scheme: string;
|
|
146
|
+
network: string;
|
|
147
|
+
asset: string;
|
|
148
|
+
amount: string;
|
|
149
|
+
payTo: string;
|
|
150
|
+
maxTimeoutSeconds: number;
|
|
151
|
+
extra?: Record<string, unknown>;
|
|
152
|
+
}): Promise<{
|
|
153
|
+
x402Version: number;
|
|
154
|
+
payload: unknown;
|
|
155
|
+
}>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Configuration for registering batch scheme.
|
|
159
|
+
*/
|
|
160
|
+
interface RegisterBatchSchemeConfig {
|
|
161
|
+
/** The EVM signer for signing payment authorizations */
|
|
162
|
+
signer: BatchEvmSigner;
|
|
163
|
+
/** Networks to register for (default: all EVM networks via wildcard) */
|
|
164
|
+
networks?: string[];
|
|
165
|
+
/**
|
|
166
|
+
* Optional fallback SchemeNetworkClient for non-batching "exact" payments.
|
|
167
|
+
*
|
|
168
|
+
* When provided, a CompositeEvmScheme is registered that dispatches to
|
|
169
|
+
* BatchEvmScheme for Gateway payments and to the fallback for standard
|
|
170
|
+
* on-chain payments. This avoids the scheme collision where x402Client's
|
|
171
|
+
* first-registered-wins policy causes one scheme to shadow the other.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* import { ExactEvmScheme } from "@x402/evm/exact/client";
|
|
176
|
+
*
|
|
177
|
+
* registerBatchScheme(client, {
|
|
178
|
+
* signer: evmSigner,
|
|
179
|
+
* fallbackScheme: new ExactEvmScheme(evmSigner),
|
|
180
|
+
* });
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
fallbackScheme?: SchemeNetworkClientLike;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Register batch scheme with an x402Client.
|
|
187
|
+
*
|
|
188
|
+
* This registers a BatchEvmScheme that can handle payment requirements
|
|
189
|
+
* with `extra.name === "GatewayWalletBatched"`. The scheme signs EIP-3009
|
|
190
|
+
* authorizations against the batch wallet contract instead of USDC.
|
|
191
|
+
*
|
|
192
|
+
* When `fallbackScheme` is provided, a CompositeEvmScheme is registered
|
|
193
|
+
* instead, which handles both batch and standard payments in a single
|
|
194
|
+
* registration — no need to separately register ExactEvmScheme.
|
|
195
|
+
*
|
|
196
|
+
* @param client - The x402Client to register with
|
|
197
|
+
* @param config - Configuration including the EVM signer
|
|
198
|
+
* @returns The registered scheme instance
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* import { x402Client } from "@x402/core/client";
|
|
203
|
+
* import { registerBatchScheme } from "@circle-fin/x402-batching/client";
|
|
204
|
+
* import { ExactEvmScheme } from "@x402/evm/exact/client";
|
|
205
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
206
|
+
*
|
|
207
|
+
* const account = privateKeyToAccount("0x...");
|
|
208
|
+
* const client = new x402Client();
|
|
209
|
+
*
|
|
210
|
+
* // Handles both batch and standard EVM payments:
|
|
211
|
+
* registerBatchScheme(client, {
|
|
212
|
+
* signer: account,
|
|
213
|
+
* fallbackScheme: new ExactEvmScheme(account),
|
|
214
|
+
* });
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
declare function registerBatchScheme(client: X402ClientLike, config: RegisterBatchSchemeConfig & {
|
|
218
|
+
fallbackScheme: SchemeNetworkClientLike;
|
|
219
|
+
}): CompositeEvmScheme;
|
|
220
|
+
declare function registerBatchScheme(client: X402ClientLike, config: Omit<RegisterBatchSchemeConfig, 'fallbackScheme'>): BatchEvmScheme;
|
|
221
|
+
declare function registerBatchScheme(client: X402ClientLike, config: RegisterBatchSchemeConfig): BatchEvmScheme | CompositeEvmScheme;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Gateway domain identifiers for supported chains.
|
|
225
|
+
* See: https://developers.circle.com/gateway/gateway-supported-blockchains
|
|
226
|
+
*/
|
|
227
|
+
declare const GATEWAY_DOMAINS: {
|
|
228
|
+
readonly arbitrumSepolia: 3;
|
|
229
|
+
readonly arcTestnet: 26;
|
|
230
|
+
readonly avalancheFuji: 1;
|
|
231
|
+
readonly baseSepolia: 6;
|
|
232
|
+
readonly sepolia: 0;
|
|
233
|
+
readonly hyperEvmTestnet: 19;
|
|
234
|
+
readonly optimismSepolia: 2;
|
|
235
|
+
readonly polygonAmoy: 7;
|
|
236
|
+
readonly seiAtlantic: 16;
|
|
237
|
+
readonly sonicTestnet: 13;
|
|
238
|
+
readonly unichainSepolia: 10;
|
|
239
|
+
readonly worldChainSepolia: 14;
|
|
240
|
+
readonly arbitrum: 3;
|
|
241
|
+
readonly avalanche: 1;
|
|
242
|
+
readonly base: 6;
|
|
243
|
+
readonly mainnet: 0;
|
|
244
|
+
readonly hyperEvm: 19;
|
|
245
|
+
readonly optimism: 2;
|
|
246
|
+
readonly polygon: 7;
|
|
247
|
+
readonly sei: 16;
|
|
248
|
+
readonly sonic: 13;
|
|
249
|
+
readonly unichain: 10;
|
|
250
|
+
readonly worldChain: 14;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Supported chain names type.
|
|
254
|
+
*/
|
|
255
|
+
type SupportedChainName = keyof typeof GATEWAY_DOMAINS;
|
|
256
|
+
/**
|
|
257
|
+
* Chain configuration with contract addresses.
|
|
258
|
+
*/
|
|
259
|
+
interface ChainConfig {
|
|
260
|
+
chain: Chain;
|
|
261
|
+
domain: number;
|
|
262
|
+
usdc: Address;
|
|
263
|
+
gatewayWallet: Address;
|
|
264
|
+
gatewayMinter: Address;
|
|
265
|
+
rpcUrl?: string;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Chain configurations for supported chains.
|
|
269
|
+
*/
|
|
270
|
+
declare const CHAIN_CONFIGS: Record<SupportedChainName, ChainConfig>;
|
|
271
|
+
/**
|
|
272
|
+
* Configuration for creating a GatewayClient.
|
|
273
|
+
*/
|
|
274
|
+
interface GatewayClientConfig {
|
|
275
|
+
/** The chain to connect to */
|
|
276
|
+
chain: SupportedChainName;
|
|
277
|
+
/** Private key for signing transactions */
|
|
278
|
+
privateKey: Hex;
|
|
279
|
+
/** Optional custom RPC URL */
|
|
280
|
+
rpcUrl?: string;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Result of a deposit operation.
|
|
284
|
+
*/
|
|
285
|
+
interface DepositResult {
|
|
286
|
+
/** Transaction hash of the approval (if needed) */
|
|
287
|
+
approvalTxHash?: Hex;
|
|
288
|
+
/** Transaction hash of the deposit */
|
|
289
|
+
depositTxHash: Hex;
|
|
290
|
+
/** Amount deposited in USDC atomic units */
|
|
291
|
+
amount: bigint;
|
|
292
|
+
/** Formatted amount deposited */
|
|
293
|
+
formattedAmount: string;
|
|
294
|
+
/** Depositor address */
|
|
295
|
+
depositor: Address;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Result of a withdraw operation (instant transfer).
|
|
299
|
+
*/
|
|
300
|
+
interface WithdrawResult {
|
|
301
|
+
/** Transaction hash of the mint on destination chain */
|
|
302
|
+
mintTxHash: Hex;
|
|
303
|
+
/** Amount withdrawn in USDC atomic units */
|
|
304
|
+
amount: bigint;
|
|
305
|
+
/** Formatted amount withdrawn */
|
|
306
|
+
formattedAmount: string;
|
|
307
|
+
/** Source chain name */
|
|
308
|
+
sourceChain: string;
|
|
309
|
+
/** Destination chain name */
|
|
310
|
+
destinationChain: string;
|
|
311
|
+
/** Recipient address */
|
|
312
|
+
recipient: Address;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Result of a pay operation.
|
|
316
|
+
*/
|
|
317
|
+
interface PayResult<T = unknown> {
|
|
318
|
+
/** The response data from the resource */
|
|
319
|
+
data: T;
|
|
320
|
+
/** Amount paid in USDC atomic units */
|
|
321
|
+
amount: bigint;
|
|
322
|
+
/** Formatted amount paid */
|
|
323
|
+
formattedAmount: string;
|
|
324
|
+
/** Transaction hash from settlement */
|
|
325
|
+
transaction: string;
|
|
326
|
+
/** HTTP status code */
|
|
327
|
+
status: number;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* All balances in one object.
|
|
331
|
+
*/
|
|
332
|
+
interface Balances {
|
|
333
|
+
/** Regular wallet USDC balance */
|
|
334
|
+
wallet: {
|
|
335
|
+
balance: bigint;
|
|
336
|
+
formatted: string;
|
|
337
|
+
};
|
|
338
|
+
/** Gateway balances */
|
|
339
|
+
gateway: {
|
|
340
|
+
total: bigint;
|
|
341
|
+
available: bigint;
|
|
342
|
+
withdrawing: bigint;
|
|
343
|
+
withdrawable: bigint;
|
|
344
|
+
formattedTotal: string;
|
|
345
|
+
formattedAvailable: string;
|
|
346
|
+
formattedWithdrawing: string;
|
|
347
|
+
formattedWithdrawable: string;
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Result of checking if a URL supports batching.
|
|
352
|
+
*/
|
|
353
|
+
interface SupportsResult {
|
|
354
|
+
/** Whether the URL supports Gateway batching */
|
|
355
|
+
supported: boolean;
|
|
356
|
+
/** Payment requirements if supported */
|
|
357
|
+
requirements?: Record<string, unknown>;
|
|
358
|
+
/** Error message if not supported */
|
|
359
|
+
error?: string;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Result of a trustless withdrawal initiation.
|
|
363
|
+
*
|
|
364
|
+
* NOTE: Trustless withdrawals are for emergency use only when Circle's API is unavailable.
|
|
365
|
+
* For normal withdrawals, use `transfer()` with the same source and destination chain.
|
|
366
|
+
*/
|
|
367
|
+
interface TrustlessWithdrawalInitResult {
|
|
368
|
+
/** Transaction hash */
|
|
369
|
+
txHash: Hex;
|
|
370
|
+
/** Amount being withdrawn in USDC atomic units */
|
|
371
|
+
amount: bigint;
|
|
372
|
+
/** Formatted amount being withdrawn */
|
|
373
|
+
formattedAmount: string;
|
|
374
|
+
/** Block number at which withdrawal becomes available (~7 days) */
|
|
375
|
+
withdrawalBlock: bigint;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Result of a trustless withdrawal completion.
|
|
379
|
+
*
|
|
380
|
+
* NOTE: Trustless withdrawals are for emergency use only when Circle's API is unavailable.
|
|
381
|
+
*/
|
|
382
|
+
interface TrustlessWithdrawalResult {
|
|
383
|
+
/** Transaction hash */
|
|
384
|
+
txHash: Hex;
|
|
385
|
+
/** Amount withdrawn in USDC atomic units */
|
|
386
|
+
amount: bigint;
|
|
387
|
+
/** Formatted amount withdrawn */
|
|
388
|
+
formattedAmount: string;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Gateway balance information.
|
|
392
|
+
*/
|
|
393
|
+
interface GatewayBalance {
|
|
394
|
+
/** Total balance in Gateway */
|
|
395
|
+
total: bigint;
|
|
396
|
+
/** Available balance (can be used) */
|
|
397
|
+
available: bigint;
|
|
398
|
+
/** Balance in withdrawal process */
|
|
399
|
+
withdrawing: bigint;
|
|
400
|
+
/** Balance ready to be withdrawn */
|
|
401
|
+
withdrawable: bigint;
|
|
402
|
+
/** Formatted total balance */
|
|
403
|
+
formattedTotal: string;
|
|
404
|
+
/** Formatted available balance */
|
|
405
|
+
formattedAvailable: string;
|
|
406
|
+
/** Formatted withdrawing balance */
|
|
407
|
+
formattedWithdrawing: string;
|
|
408
|
+
/** Formatted withdrawable balance */
|
|
409
|
+
formattedWithdrawable: string;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Gateway Client for gasless payments and cross-chain USDC via Circle Gateway.
|
|
413
|
+
*
|
|
414
|
+
* This client provides a simple interface for buyers to:
|
|
415
|
+
* - **Deposit**: Move USDC into Gateway for gasless payments
|
|
416
|
+
* - **Pay**: Pay for x402-protected resources (handles 402 automatically)
|
|
417
|
+
* - **Withdraw**: Get USDC back (same-chain or cross-chain, instant)
|
|
418
|
+
* - **GetBalances**: Check all balances in one call
|
|
419
|
+
*
|
|
420
|
+
* ## Typical Flow
|
|
421
|
+
* ```typescript
|
|
422
|
+
* const gateway = new GatewayClient({ chain: 'arcTestnet', privateKey });
|
|
423
|
+
*
|
|
424
|
+
* // 1. Deposit (one-time setup)
|
|
425
|
+
* await gateway.deposit('100');
|
|
426
|
+
*
|
|
427
|
+
* // 2. Pay for resources (many times, gasless!)
|
|
428
|
+
* const { data } = await gateway.pay('https://api.example.com/resource');
|
|
429
|
+
*
|
|
430
|
+
* // 3. Check balances
|
|
431
|
+
* const balances = await gateway.getBalances();
|
|
432
|
+
*
|
|
433
|
+
* // 4. Withdraw when done
|
|
434
|
+
* await gateway.withdraw('50');
|
|
435
|
+
* // Or withdraw to a different chain (requires gas on destination)
|
|
436
|
+
* await gateway.withdraw('25', { chain: 'baseSepolia' });
|
|
437
|
+
* ```
|
|
438
|
+
*
|
|
439
|
+
* ## Emergency Flow (when Circle's API is down)
|
|
440
|
+
* Use `initiateTrustlessWithdrawal()` and `completeTrustlessWithdrawal()` (7-day delay).
|
|
441
|
+
*/
|
|
442
|
+
declare class GatewayClient {
|
|
443
|
+
readonly chainConfig: ChainConfig;
|
|
444
|
+
readonly account: ReturnType<typeof privateKeyToAccount>;
|
|
445
|
+
readonly publicClient: PublicClient<Transport, Chain>;
|
|
446
|
+
readonly walletClient: WalletClient<Transport, Chain, Account>;
|
|
447
|
+
/**
|
|
448
|
+
* Creates a new GatewayClient.
|
|
449
|
+
*
|
|
450
|
+
* @param config - Configuration including chain and private key
|
|
451
|
+
*/
|
|
452
|
+
constructor(config: GatewayClientConfig);
|
|
453
|
+
/**
|
|
454
|
+
* Get the account address.
|
|
455
|
+
*/
|
|
456
|
+
get address(): Address;
|
|
457
|
+
/**
|
|
458
|
+
* Get the chain name.
|
|
459
|
+
*/
|
|
460
|
+
get chainName(): string;
|
|
461
|
+
/**
|
|
462
|
+
* Get the Gateway domain for this chain.
|
|
463
|
+
*/
|
|
464
|
+
get domain(): number;
|
|
465
|
+
/**
|
|
466
|
+
* Get the chain name key for this client.
|
|
467
|
+
*/
|
|
468
|
+
getChainName(): SupportedChainName;
|
|
469
|
+
/**
|
|
470
|
+
* Get the USDC balance of the account (not in Gateway).
|
|
471
|
+
*
|
|
472
|
+
* @param address - Optional address to check (defaults to account address)
|
|
473
|
+
* @returns USDC balance in atomic units and formatted
|
|
474
|
+
*/
|
|
475
|
+
getUsdcBalance(address?: Address): Promise<{
|
|
476
|
+
balance: bigint;
|
|
477
|
+
formatted: string;
|
|
478
|
+
}>;
|
|
479
|
+
/**
|
|
480
|
+
* Get all balances (wallet + gateway) in one call.
|
|
481
|
+
*
|
|
482
|
+
* @param address - Optional address to check (defaults to account address)
|
|
483
|
+
* @returns All balances
|
|
484
|
+
*/
|
|
485
|
+
getBalances(address?: Address): Promise<Balances>;
|
|
486
|
+
/**
|
|
487
|
+
* Get the Gateway balance for an address.
|
|
488
|
+
*
|
|
489
|
+
* @param address - Optional address to check (defaults to account address)
|
|
490
|
+
* @returns Gateway balance information
|
|
491
|
+
* @deprecated Use `getBalances()` instead for a unified view
|
|
492
|
+
*/
|
|
493
|
+
getBalance(address?: Address): Promise<GatewayBalance>;
|
|
494
|
+
/**
|
|
495
|
+
* Get the Gateway balance for an address via Gateway API.
|
|
496
|
+
*/
|
|
497
|
+
private getGatewayBalance;
|
|
498
|
+
/**
|
|
499
|
+
* Deposit USDC into the Gateway Wallet.
|
|
500
|
+
*
|
|
501
|
+
* This method first approves the Gateway Wallet contract to spend USDC,
|
|
502
|
+
* then deposits the specified amount.
|
|
503
|
+
*
|
|
504
|
+
* @param amount - Amount of USDC to deposit (as a decimal string, e.g., "10.5")
|
|
505
|
+
* @param options - Optional deposit options
|
|
506
|
+
* @returns Deposit result with transaction hashes
|
|
507
|
+
*/
|
|
508
|
+
deposit(amount: string, options?: {
|
|
509
|
+
/** Amount to approve (defaults to amount) */
|
|
510
|
+
approveAmount?: string;
|
|
511
|
+
/** Skip approval if already approved */
|
|
512
|
+
skipApprovalCheck?: boolean;
|
|
513
|
+
}): Promise<DepositResult>;
|
|
514
|
+
/**
|
|
515
|
+
* Deposit USDC into the Gateway Wallet on behalf of another address.
|
|
516
|
+
*
|
|
517
|
+
* The resulting balance belongs to the specified depositor address,
|
|
518
|
+
* not the caller.
|
|
519
|
+
*
|
|
520
|
+
* @param amount - Amount of USDC to deposit (as a decimal string)
|
|
521
|
+
* @param depositor - Address that will own the resulting balance
|
|
522
|
+
* @param options - Optional deposit options
|
|
523
|
+
* @returns Deposit result with transaction hashes
|
|
524
|
+
*/
|
|
525
|
+
depositFor(amount: string, depositor: Address, options?: {
|
|
526
|
+
/** Amount to approve (defaults to amount) */
|
|
527
|
+
approveAmount?: string;
|
|
528
|
+
/** Skip approval if already approved */
|
|
529
|
+
skipApprovalCheck?: boolean;
|
|
530
|
+
}): Promise<DepositResult>;
|
|
531
|
+
/**
|
|
532
|
+
* Check if a URL supports Gateway batching before paying.
|
|
533
|
+
*
|
|
534
|
+
* @param url - The URL to check
|
|
535
|
+
* @returns Whether batching is supported and payment requirements
|
|
536
|
+
*/
|
|
537
|
+
supports(url: string): Promise<SupportsResult>;
|
|
538
|
+
/**
|
|
539
|
+
* Pay for an x402-protected resource.
|
|
540
|
+
*
|
|
541
|
+
* This method handles the full 402 payment flow automatically:
|
|
542
|
+
* 1. Makes initial request
|
|
543
|
+
* 2. If 402, finds Gateway batching option
|
|
544
|
+
* 3. Signs payment authorization
|
|
545
|
+
* 4. Retries with Payment-Signature header
|
|
546
|
+
*
|
|
547
|
+
* @param url - The URL to pay for
|
|
548
|
+
* @param options - Optional request options
|
|
549
|
+
* @returns The response data and payment info
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```typescript
|
|
553
|
+
* const { data, amount } = await gateway.pay('https://api.example.com/resource');
|
|
554
|
+
* console.log('Paid', amount, 'USDC for:', data);
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
pay<T = unknown>(url: string, options?: {
|
|
558
|
+
/** HTTP method (default: GET) */
|
|
559
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
560
|
+
/** Request body for POST/PUT */
|
|
561
|
+
body?: unknown;
|
|
562
|
+
/** Additional headers */
|
|
563
|
+
headers?: Record<string, string>;
|
|
564
|
+
}): Promise<PayResult<T>>;
|
|
565
|
+
/**
|
|
566
|
+
* Create a payment payload for x402 (low-level).
|
|
567
|
+
*/
|
|
568
|
+
private createPaymentPayload;
|
|
569
|
+
/**
|
|
570
|
+
* Withdraw USDC from Gateway to your wallet.
|
|
571
|
+
*
|
|
572
|
+
* By default, withdraws to the same chain (instant, no 7-day delay).
|
|
573
|
+
* Optionally, withdraw to a different chain (requires gas on destination).
|
|
574
|
+
*
|
|
575
|
+
* @param amount - Amount of USDC to withdraw (as a decimal string)
|
|
576
|
+
* @param options - Optional withdrawal options
|
|
577
|
+
* @returns Withdrawal result with transaction hash
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```typescript
|
|
581
|
+
* // Withdraw to same chain (instant!)
|
|
582
|
+
* await gateway.withdraw('50');
|
|
583
|
+
*
|
|
584
|
+
* // Withdraw to Base Sepolia (requires ETH on Base for gas)
|
|
585
|
+
* await gateway.withdraw('25', { chain: 'baseSepolia' });
|
|
586
|
+
* ```
|
|
587
|
+
*/
|
|
588
|
+
withdraw(amount: string, options?: {
|
|
589
|
+
/** Destination chain (defaults to same chain) */
|
|
590
|
+
chain?: SupportedChainName;
|
|
591
|
+
/** Recipient address (defaults to your address) */
|
|
592
|
+
recipient?: Address;
|
|
593
|
+
/** Max fee willing to pay in USDC (defaults to 2.01) */
|
|
594
|
+
maxFee?: string;
|
|
595
|
+
}): Promise<WithdrawResult>;
|
|
596
|
+
/**
|
|
597
|
+
* Transfer USDC to any supported chain (alias for withdraw with destination chain).
|
|
598
|
+
* @deprecated Use `withdraw({ chain })` instead
|
|
599
|
+
*/
|
|
600
|
+
transfer(amount: string, destinationChain: SupportedChainName, recipient?: Address): Promise<WithdrawResult>;
|
|
601
|
+
/**
|
|
602
|
+
* Create a burn intent for a transfer.
|
|
603
|
+
*/
|
|
604
|
+
private createBurnIntent;
|
|
605
|
+
/**
|
|
606
|
+
* Check if this client is connected to a testnet.
|
|
607
|
+
*/
|
|
608
|
+
private isTestnet;
|
|
609
|
+
/**
|
|
610
|
+
* Get the trustless withdrawal delay in blocks (~7 days).
|
|
611
|
+
*
|
|
612
|
+
* NOTE: This is for the emergency trustless withdrawal flow only.
|
|
613
|
+
* For normal withdrawals, use `transfer()` to the same chain.
|
|
614
|
+
*
|
|
615
|
+
* @returns Number of blocks to wait after initiating trustless withdrawal
|
|
616
|
+
*/
|
|
617
|
+
getTrustlessWithdrawalDelay(): Promise<bigint>;
|
|
618
|
+
/**
|
|
619
|
+
* Get the block at which a pending trustless withdrawal becomes available.
|
|
620
|
+
*
|
|
621
|
+
* @param address - Optional address to check (defaults to account address)
|
|
622
|
+
* @returns Block number, or 0n if no pending withdrawal
|
|
623
|
+
*/
|
|
624
|
+
getTrustlessWithdrawalBlock(address?: Address): Promise<bigint>;
|
|
625
|
+
/**
|
|
626
|
+
* Initiate a trustless withdrawal from the Gateway Wallet.
|
|
627
|
+
*
|
|
628
|
+
* ⚠️ **WARNING: This is for emergency use only!**
|
|
629
|
+
*
|
|
630
|
+
* Use this only when Circle's Gateway API is unavailable. For normal
|
|
631
|
+
* withdrawals, use `transfer()` to the same chain - it's instant.
|
|
632
|
+
*
|
|
633
|
+
* After calling this, you must wait ~7 days (`withdrawalDelay` blocks)
|
|
634
|
+
* before calling `completeTrustlessWithdrawal()`.
|
|
635
|
+
*
|
|
636
|
+
* @param amount - Amount of USDC to withdraw (as a decimal string)
|
|
637
|
+
* @returns Withdrawal initiation result
|
|
638
|
+
*/
|
|
639
|
+
initiateTrustlessWithdrawal(amount: string): Promise<TrustlessWithdrawalInitResult>;
|
|
640
|
+
/**
|
|
641
|
+
* Complete a trustless withdrawal after the ~7-day delay period.
|
|
642
|
+
*
|
|
643
|
+
* ⚠️ **WARNING: This is for emergency use only!**
|
|
644
|
+
*
|
|
645
|
+
* Use this only when Circle's Gateway API is unavailable. For normal
|
|
646
|
+
* withdrawals, use `transfer()` to the same chain - it's instant.
|
|
647
|
+
*
|
|
648
|
+
* @returns Withdrawal result
|
|
649
|
+
*/
|
|
650
|
+
completeTrustlessWithdrawal(): Promise<TrustlessWithdrawalResult>;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
export { type Balances, BatchEvmScheme, CHAIN_CONFIGS, type ChainConfig, CompositeEvmScheme, type DepositResult, GATEWAY_DOMAINS, type GatewayBalance, GatewayClient, type GatewayClientConfig, type PayResult, type RegisterBatchSchemeConfig, type SupportedChainName, type SupportsResult, type TrustlessWithdrawalInitResult, type TrustlessWithdrawalResult, type WithdrawResult, registerBatchScheme };
|