@dcentralab/d402-client 0.2.1 → 0.2.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 +128 -68
- package/dist/index.d.mts +235 -96
- package/dist/index.d.ts +235 -96
- package/dist/index.js +508 -4259
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +508 -4262
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Account } from 'viem/accounts';
|
|
2
|
-
import { Hash, Address } from 'viem';
|
|
2
|
+
import { PublicClient, WalletClient, Hash, Address } from 'viem';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Type definitions for D402 payment protocol
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Supported blockchain networks
|
|
9
|
+
* Supported blockchain networks (where IATP contracts are deployed)
|
|
10
10
|
*/
|
|
11
|
-
type SupportedNetwork = '
|
|
11
|
+
type SupportedNetwork = 'sepolia';
|
|
12
12
|
/**
|
|
13
13
|
* D402 Client configuration
|
|
14
14
|
*/
|
|
@@ -34,9 +34,9 @@ interface EIP712Domain {
|
|
|
34
34
|
verifyingContract: `0x${string}`;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
* Payment requirement from 402 response
|
|
37
|
+
* Payment requirement from 402 response.
|
|
38
38
|
*
|
|
39
|
-
*
|
|
39
|
+
* Describes the payment details required by a 402-protected API.
|
|
40
40
|
*/
|
|
41
41
|
interface PaymentRequirement {
|
|
42
42
|
/** D402 protocol version */
|
|
@@ -128,7 +128,7 @@ interface PaymentSelectorOptions {
|
|
|
128
128
|
/**
|
|
129
129
|
* Select payment requirement from a list based on filters and preferences
|
|
130
130
|
*
|
|
131
|
-
* This function implements the same logic as
|
|
131
|
+
* This function implements the same logic as
|
|
132
132
|
*
|
|
133
133
|
* @param requirements - List of accepted payment requirements from 402 response
|
|
134
134
|
* @param options - Selection options and filters
|
|
@@ -177,22 +177,24 @@ declare function sortPaymentRequirements(requirements: PaymentRequirement[], pre
|
|
|
177
177
|
/**
|
|
178
178
|
* D402 Client - Main client class for handling HTTP 402 payments
|
|
179
179
|
*
|
|
180
|
-
* Matches Python implementation: IATP/src/traia_iatp/d402/clients/base.py d402Client
|
|
181
180
|
*/
|
|
182
181
|
|
|
183
182
|
/**
|
|
184
183
|
* D402 Client configuration.
|
|
185
184
|
*
|
|
186
|
-
*
|
|
185
|
+
* The client uses the user's wallet to sign payment authorizations for 402-protected APIs.
|
|
187
186
|
*/
|
|
188
187
|
interface D402ClientConfig {
|
|
189
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* User's account for signing payments.
|
|
190
|
+
* Typically from wagmi: `walletClient.account`
|
|
191
|
+
*/
|
|
190
192
|
operatorAccount: Account;
|
|
191
193
|
/**
|
|
192
194
|
* IATPWallet contract address.
|
|
193
|
-
*
|
|
195
|
+
* Get from `createIATPWallet()` or `getWalletsByOwner()`.
|
|
194
196
|
*/
|
|
195
|
-
iatpWalletAddress
|
|
197
|
+
iatpWalletAddress: `0x${string}`;
|
|
196
198
|
/**
|
|
197
199
|
* Optional safety limit for maximum payment amount per request in base units.
|
|
198
200
|
*
|
|
@@ -227,8 +229,6 @@ interface D402ClientConfig {
|
|
|
227
229
|
/**
|
|
228
230
|
* D402 Client for handling HTTP 402 Payment Required responses.
|
|
229
231
|
*
|
|
230
|
-
* Matches Python: d402Client class (base.py lines 63-219)
|
|
231
|
-
*
|
|
232
232
|
* This client automatically:
|
|
233
233
|
* 1. Detects HTTP 402 responses
|
|
234
234
|
* 2. Parses payment requirements
|
|
@@ -267,15 +267,23 @@ declare class D402Client {
|
|
|
267
267
|
/**
|
|
268
268
|
* Create a new D402 Client.
|
|
269
269
|
*
|
|
270
|
-
* Matches Python: d402Client.__init__ (base.py lines 66-95)
|
|
271
|
-
*
|
|
272
270
|
* @param config - Client configuration
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* const client = new D402Client({
|
|
275
|
+
* operatorAccount: walletClient.account, // User's wallet from wagmi
|
|
276
|
+
* iatpWalletAddress: userWallet, // From createIATPWallet() or getWalletsByOwner()
|
|
277
|
+
* maxValue: 1000000n // Safety limit: 1 USDC max
|
|
278
|
+
* })
|
|
279
|
+
* ```
|
|
273
280
|
*/
|
|
274
281
|
constructor(config: D402ClientConfig);
|
|
275
282
|
/**
|
|
276
283
|
* Select payment requirement from list of options.
|
|
277
284
|
*
|
|
278
|
-
*
|
|
285
|
+
* Applies configured filters (network, scheme, maxValue) to choose
|
|
286
|
+
* the appropriate payment option from the server's requirements list.
|
|
279
287
|
*
|
|
280
288
|
* @param requirements - List of payment requirements from 402 response
|
|
281
289
|
* @returns Selected payment requirement
|
|
@@ -284,13 +292,13 @@ declare class D402Client {
|
|
|
284
292
|
/**
|
|
285
293
|
* Get the IATP wallet address used for payments.
|
|
286
294
|
*
|
|
287
|
-
* @returns IATPWallet contract address or
|
|
295
|
+
* @returns IATPWallet contract address, or user's EOA address if no wallet configured
|
|
288
296
|
*/
|
|
289
297
|
getIATPWalletAddress(): `0x${string}`;
|
|
290
298
|
/**
|
|
291
|
-
* Get the
|
|
299
|
+
* Get the user's account used for signing payments.
|
|
292
300
|
*
|
|
293
|
-
* @returns
|
|
301
|
+
* @returns User's wallet account
|
|
294
302
|
*/
|
|
295
303
|
getOperatorAccount(): Account;
|
|
296
304
|
/**
|
|
@@ -299,10 +307,44 @@ declare class D402Client {
|
|
|
299
307
|
* @returns Maximum value in base units, or undefined if no limit
|
|
300
308
|
*/
|
|
301
309
|
getMaxValue(): bigint | undefined;
|
|
310
|
+
/**
|
|
311
|
+
* Get available balance for a token in the IATP wallet.
|
|
312
|
+
*
|
|
313
|
+
* @param publicClient - Viem PublicClient (from wagmi usePublicClient)
|
|
314
|
+
* @param tokenAddress - Token contract address
|
|
315
|
+
* @returns Available balance in token's base units (wei)
|
|
316
|
+
*/
|
|
317
|
+
getAvailableBalance(publicClient: PublicClient, tokenAddress: `0x${string}`): Promise<bigint>;
|
|
318
|
+
/**
|
|
319
|
+
* Get withdrawal request for a token.
|
|
320
|
+
*
|
|
321
|
+
* @param publicClient - Viem PublicClient
|
|
322
|
+
* @param tokenAddress - Token contract address
|
|
323
|
+
* @returns Withdrawal request: [amount, unlockTimestamp] or null if no request exists
|
|
324
|
+
*/
|
|
325
|
+
getWithdrawalRequest(publicClient: PublicClient, tokenAddress: `0x${string}`): Promise<[bigint, bigint] | null>;
|
|
326
|
+
/**
|
|
327
|
+
* Request a withdrawal from the IATP wallet.
|
|
328
|
+
*
|
|
329
|
+
* @param walletClient - Viem WalletClient (from wagmi useWalletClient)
|
|
330
|
+
* @param publicClient - Viem PublicClient (from wagmi usePublicClient)
|
|
331
|
+
* @param tokenAddress - Token contract address
|
|
332
|
+
* @param amount - Amount in token's base units (wei)
|
|
333
|
+
* @returns Transaction hash
|
|
334
|
+
*/
|
|
335
|
+
requestWithdrawal(walletClient: WalletClient, publicClient: PublicClient, tokenAddress: `0x${string}`, amount: bigint): Promise<Hash>;
|
|
336
|
+
/**
|
|
337
|
+
* Execute a withdrawal for a token (after unlock period).
|
|
338
|
+
*
|
|
339
|
+
* @param walletClient - Viem WalletClient
|
|
340
|
+
* @param publicClient - Viem PublicClient
|
|
341
|
+
* @param tokenAddress - Token contract address
|
|
342
|
+
* @returns Transaction hash
|
|
343
|
+
*/
|
|
344
|
+
executeWithdrawal(walletClient: WalletClient, publicClient: PublicClient, tokenAddress: `0x${string}`): Promise<Hash>;
|
|
302
345
|
/**
|
|
303
346
|
* Fetch with automatic 402 payment handling.
|
|
304
347
|
*
|
|
305
|
-
* Matches Python: HttpxHooks.on_response() (httpx.py lines 33-117)
|
|
306
348
|
*
|
|
307
349
|
* Flow:
|
|
308
350
|
* 1. Make request without payment
|
|
@@ -334,34 +376,37 @@ declare class D402Client {
|
|
|
334
376
|
|
|
335
377
|
/**
|
|
336
378
|
* EIP-712 payment signing utilities
|
|
337
|
-
*
|
|
338
|
-
* Matches Python implementation: IATP/src/traia_iatp/d402/payment_signing.py
|
|
339
379
|
*/
|
|
340
380
|
|
|
341
381
|
/**
|
|
342
382
|
* Sign a D402 payment using EIP-712 signature.
|
|
343
383
|
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
384
|
+
* Signs a payment authorization with the user's wallet (MetaMask/WalletConnect).
|
|
385
|
+
* This creates a PullFundsForSettlement signature that authorizes the payment.
|
|
346
386
|
*
|
|
347
|
-
*
|
|
387
|
+
* The user will see a MetaMask popup to approve the signature.
|
|
348
388
|
*
|
|
349
389
|
* @param params - Signing parameters
|
|
350
|
-
* @param params.operatorAccount -
|
|
390
|
+
* @param params.operatorAccount - User's account for signing (from wagmi walletClient)
|
|
351
391
|
* @param params.paymentRequirement - Payment requirements from 402 response
|
|
352
|
-
* @param params.iatpWalletAddress - IATPWallet contract address (optional
|
|
392
|
+
* @param params.iatpWalletAddress - IATPWallet contract address (optional)
|
|
353
393
|
* @param params.requestPath - API request path (optional, uses payment_requirements.resource if not provided)
|
|
354
394
|
* @returns Signed payment ready to encode and send
|
|
355
395
|
*
|
|
356
396
|
* @example
|
|
357
397
|
* ```ts
|
|
398
|
+
* // Get payment requirement from 402 response
|
|
358
399
|
* const requirement = await parsePaymentRequirement(response)
|
|
400
|
+
*
|
|
401
|
+
* // User signs payment (MetaMask popup appears)
|
|
359
402
|
* const signedPayment = await signD402Payment({
|
|
360
|
-
* operatorAccount: account,
|
|
403
|
+
* operatorAccount: walletClient.account, // User's wallet
|
|
361
404
|
* paymentRequirement: requirement,
|
|
362
|
-
* iatpWalletAddress: '
|
|
405
|
+
* iatpWalletAddress: '0xUserIATPWallet...'
|
|
363
406
|
* })
|
|
364
|
-
*
|
|
407
|
+
*
|
|
408
|
+
* // Encode for X-Payment header
|
|
409
|
+
* const paymentHeader = encodePayment(signedPayment)
|
|
365
410
|
* ```
|
|
366
411
|
*/
|
|
367
412
|
declare function signD402Payment(params: {
|
|
@@ -374,30 +419,27 @@ declare function signD402Payment(params: {
|
|
|
374
419
|
|
|
375
420
|
/**
|
|
376
421
|
* Parse 402 payment requirements from HTTP responses
|
|
377
|
-
*
|
|
378
|
-
* Matches Python implementation: IATP/src/traia_iatp/d402/clients/httpx.py lines 64-66
|
|
379
|
-
* Response structure from: IATP/src/traia_iatp/d402/types.py d402PaymentRequiredResponse
|
|
380
422
|
*/
|
|
381
423
|
|
|
382
424
|
/**
|
|
383
425
|
* Parse payment requirements from HTTP 402 response.
|
|
384
426
|
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* payment_response = d402PaymentRequiredResponse(**data)
|
|
388
|
-
*
|
|
389
|
-
* The 402 response body contains payment requirements in camelCase JSON format.
|
|
427
|
+
* When an API returns 402 "Payment Required", this function extracts
|
|
428
|
+
* the payment details (amount, token, network) from the response body.
|
|
390
429
|
*
|
|
391
430
|
* @param response - HTTP Response object with status 402
|
|
392
|
-
* @returns First payment requirement from
|
|
431
|
+
* @returns First payment requirement from the response
|
|
393
432
|
* @throws {Invalid402ResponseError} If response is not valid 402 or malformed
|
|
394
433
|
*
|
|
395
434
|
* @example
|
|
396
435
|
* ```ts
|
|
397
|
-
* const response = await fetch('
|
|
436
|
+
* const response = await fetch('https://sentiment-api.com/analyze')
|
|
437
|
+
*
|
|
398
438
|
* if (response.status === 402) {
|
|
399
439
|
* const requirement = await parsePaymentRequirement(response)
|
|
400
|
-
* console.log(requirement.maxAmountRequired)
|
|
440
|
+
* console.log('Payment required:', requirement.maxAmountRequired, 'wei')
|
|
441
|
+
* console.log('Token:', requirement.asset)
|
|
442
|
+
* console.log('Network:', requirement.network)
|
|
401
443
|
* }
|
|
402
444
|
* ```
|
|
403
445
|
*/
|
|
@@ -405,32 +447,32 @@ declare function parsePaymentRequirement(response: Response): Promise<PaymentReq
|
|
|
405
447
|
|
|
406
448
|
/**
|
|
407
449
|
* Base64 encoding/decoding for payment payloads
|
|
408
|
-
*
|
|
409
|
-
* Matches Python implementation: IATP/src/traia_iatp/d402/encoding.py
|
|
410
450
|
*/
|
|
411
451
|
|
|
412
452
|
/**
|
|
413
|
-
* Encode a payment
|
|
453
|
+
* Encode a signed payment to base64 string for X-Payment header.
|
|
414
454
|
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
455
|
+
* Takes a signed payment object and encodes it to base64 format
|
|
456
|
+
* that can be sent in the X-Payment HTTP header.
|
|
417
457
|
*
|
|
418
|
-
* @param payment -
|
|
419
|
-
* @returns Base64 encoded
|
|
458
|
+
* @param payment - Signed payment object (from signD402Payment)
|
|
459
|
+
* @returns Base64 encoded string ready for X-Payment header
|
|
420
460
|
*
|
|
421
461
|
* @example
|
|
422
462
|
* ```ts
|
|
423
|
-
* const
|
|
424
|
-
* const encoded = encodePayment(
|
|
425
|
-
*
|
|
463
|
+
* const signedPayment = await signD402Payment({...})
|
|
464
|
+
* const encoded = encodePayment(signedPayment)
|
|
465
|
+
*
|
|
466
|
+
* // Send in HTTP request
|
|
467
|
+
* fetch(url, {
|
|
468
|
+
* headers: { 'X-Payment': encoded }
|
|
469
|
+
* })
|
|
426
470
|
* ```
|
|
427
471
|
*/
|
|
428
472
|
declare function encodePayment(payment: SignedPayment): string;
|
|
429
473
|
/**
|
|
430
474
|
* Decode a base64 encoded payment string back to payment object.
|
|
431
475
|
*
|
|
432
|
-
* Matches Python: decode_payment(encoded_payment: str) -> Dict[str, Any]
|
|
433
|
-
*
|
|
434
476
|
* @param encodedPayment - Base64 encoded payment string
|
|
435
477
|
* @returns Decoded payment object
|
|
436
478
|
*
|
|
@@ -481,9 +523,12 @@ declare function getUsdcAddress(network: SupportedNetwork): `0x${string}`;
|
|
|
481
523
|
/**
|
|
482
524
|
* Get chain ID for a given network.
|
|
483
525
|
*
|
|
484
|
-
*
|
|
526
|
+
* This function maps network names to chain IDs for parsing 402 payment requirements
|
|
527
|
+
* from external APIs. It supports many networks that may appear in 402 responses,
|
|
528
|
+
* even if IATP contracts are not deployed on those networks.
|
|
485
529
|
*
|
|
486
|
-
*
|
|
530
|
+
* Note: IATP contracts are currently only deployed on Sepolia. This function exists
|
|
531
|
+
* to handle 402 responses from any API that might support other networks.
|
|
487
532
|
*
|
|
488
533
|
* @param network - Network name or chain ID as string
|
|
489
534
|
* @returns Chain ID as number
|
|
@@ -494,7 +539,7 @@ declare function getUsdcAddress(network: SupportedNetwork): `0x${string}`;
|
|
|
494
539
|
* ```ts
|
|
495
540
|
* getChainId('sepolia') // Returns 11155111
|
|
496
541
|
* getChainId('11155111') // Returns 11155111 (passthrough)
|
|
497
|
-
* getChainId('base') // Returns 8453
|
|
542
|
+
* getChainId('base') // Returns 8453 (for parsing 402 responses)
|
|
498
543
|
* ```
|
|
499
544
|
*/
|
|
500
545
|
declare function getChainId(network: string): number;
|
|
@@ -675,16 +720,13 @@ declare const EIP712_TYPES: {
|
|
|
675
720
|
};
|
|
676
721
|
|
|
677
722
|
/**
|
|
678
|
-
* Contract configuration and utilities
|
|
679
|
-
*
|
|
680
|
-
* Matches Python implementation: IATP/src/traia_iatp/contracts/config.py
|
|
723
|
+
* Contract configuration and utilities.
|
|
681
724
|
*
|
|
682
|
-
*
|
|
725
|
+
* Provides access to IATPWallet contract ABIs and deployed addresses.
|
|
726
|
+
* Contract data is bundled directly in the package for reliability.
|
|
683
727
|
*/
|
|
684
728
|
/**
|
|
685
729
|
* Supported contract names.
|
|
686
|
-
*
|
|
687
|
-
* Matches Python: ContractName enum (config.py lines 16-21)
|
|
688
730
|
*/
|
|
689
731
|
declare enum ContractName {
|
|
690
732
|
IATP_WALLET = "IATPWallet",
|
|
@@ -694,16 +736,11 @@ declare enum ContractName {
|
|
|
694
736
|
}
|
|
695
737
|
/**
|
|
696
738
|
* Supported networks for contract deployments.
|
|
697
|
-
*
|
|
698
|
-
* Matches Python: SUPPORTED_NETWORKS (config.py line 25)
|
|
699
739
|
*/
|
|
700
|
-
type SupportedContractNetwork = 'sepolia'
|
|
740
|
+
type SupportedContractNetwork = 'sepolia';
|
|
701
741
|
/**
|
|
702
742
|
* Get contract address for a given network.
|
|
703
743
|
*
|
|
704
|
-
* Matches Python: get_contract_address(contract_name: str, network: str) -> Optional[str]
|
|
705
|
-
* (config.py lines 77-118)
|
|
706
|
-
*
|
|
707
744
|
* @param contractName - Name of the contract (use ContractName enum)
|
|
708
745
|
* @param network - Network name (default: "sepolia")
|
|
709
746
|
* @param useProxy - Use proxy address if true, implementation if false (default: true)
|
|
@@ -719,9 +756,6 @@ declare function getContractAddress(contractName: string | ContractName, network
|
|
|
719
756
|
/**
|
|
720
757
|
* Get contract ABI for a given network.
|
|
721
758
|
*
|
|
722
|
-
* Matches Python: get_contract_abi(contract_name: str, network: str) -> Optional[List[dict]]
|
|
723
|
-
* (config.py lines 121-156)
|
|
724
|
-
*
|
|
725
759
|
* @param contractName - Name of the contract (use ContractName enum)
|
|
726
760
|
* @param network - Network name (default: "sepolia")
|
|
727
761
|
* @returns Contract ABI as array of objects, or null if not found
|
|
@@ -736,8 +770,6 @@ declare function getContractAbi(contractName: string | ContractName, network?: S
|
|
|
736
770
|
/**
|
|
737
771
|
* Get contract configuration (address and ABI) for a network.
|
|
738
772
|
*
|
|
739
|
-
* Matches Python: get_contract_config(network: str) from wallet_creator.py
|
|
740
|
-
*
|
|
741
773
|
* @param contractName - Name of the contract
|
|
742
774
|
* @param network - Network name (default: "sepolia")
|
|
743
775
|
* @returns Object with address and abi, or null if not found
|
|
@@ -787,23 +819,16 @@ declare function isContractDeployed(contractName: string | ContractName, network
|
|
|
787
819
|
/**
|
|
788
820
|
* IATPWallet creation and management
|
|
789
821
|
*
|
|
790
|
-
* Matches Python implementation: IATP/src/traia_iatp/scripts/create_wallet.py
|
|
791
822
|
*/
|
|
792
823
|
|
|
793
824
|
/**
|
|
794
825
|
* Result of wallet creation.
|
|
795
|
-
*
|
|
796
|
-
* Matches Python: create_wallet() return type (lines 138-147)
|
|
797
826
|
*/
|
|
798
827
|
interface WalletCreationResult {
|
|
799
828
|
/** Address of the created IATPWallet contract */
|
|
800
829
|
walletAddress: `0x${string}`;
|
|
801
|
-
/** Owner address (who created it) */
|
|
830
|
+
/** Owner address (who created it, also the operator) */
|
|
802
831
|
ownerAddress: `0x${string}`;
|
|
803
|
-
/** Operator address (can sign payments) */
|
|
804
|
-
operatorAddress: `0x${string}`;
|
|
805
|
-
/** Operator private key (store securely!) */
|
|
806
|
-
operatorPrivateKey: `0x${string}`;
|
|
807
832
|
/** Transaction hash */
|
|
808
833
|
transactionHash: Hash;
|
|
809
834
|
/** Block number where wallet was created */
|
|
@@ -816,15 +841,15 @@ interface WalletCreationResult {
|
|
|
816
841
|
/**
|
|
817
842
|
* Create a new IATPWallet using IATPWalletFactory.
|
|
818
843
|
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
844
|
+
* Creates an on-chain smart contract wallet where the user is both owner and operator.
|
|
845
|
+
* The user signs all payments themselves with their wallet (MetaMask/WalletConnect).
|
|
821
846
|
*
|
|
822
847
|
* This function:
|
|
823
|
-
* 1.
|
|
824
|
-
* 2.
|
|
848
|
+
* 1. Calls IATPWalletFactory.createWallet(ownerAddress)
|
|
849
|
+
* 2. Sets owner as operator (user signs all payments)
|
|
825
850
|
* 3. Waits for transaction confirmation
|
|
826
851
|
* 4. Extracts wallet address from WalletCreated event
|
|
827
|
-
* 5. Returns wallet
|
|
852
|
+
* 5. Returns wallet address and transaction info
|
|
828
853
|
*
|
|
829
854
|
* @param params - Creation parameters
|
|
830
855
|
* @param params.ownerAccount - Owner's account (will own the wallet)
|
|
@@ -851,9 +876,8 @@ interface WalletCreationResult {
|
|
|
851
876
|
*/
|
|
852
877
|
declare function createIATPWallet(params: {
|
|
853
878
|
ownerAccount: Account;
|
|
854
|
-
network?: 'sepolia'
|
|
879
|
+
network?: 'sepolia';
|
|
855
880
|
rpcUrl?: string;
|
|
856
|
-
operatorPrivateKey?: `0x${string}`;
|
|
857
881
|
}): Promise<WalletCreationResult>;
|
|
858
882
|
/**
|
|
859
883
|
* Get all IATPWallet addresses owned by a user.
|
|
@@ -861,30 +885,145 @@ declare function createIATPWallet(params: {
|
|
|
861
885
|
* Queries the IATPWalletFactory contract to retrieve all wallets
|
|
862
886
|
* where the specified address is the owner.
|
|
863
887
|
*
|
|
888
|
+
* Use this to check if a user already has a wallet before calling createIATPWallet().
|
|
889
|
+
*
|
|
864
890
|
* @param params - Query parameters
|
|
865
|
-
* @param params.ownerAddress -
|
|
891
|
+
* @param params.ownerAddress - User's wallet address (from MetaMask)
|
|
866
892
|
* @param params.network - Network to query (default: "sepolia")
|
|
867
893
|
* @param params.rpcUrl - Custom RPC URL (optional)
|
|
868
|
-
* @returns Array of IATPWallet contract addresses
|
|
894
|
+
* @returns Array of IATPWallet contract addresses (empty if user has no wallets)
|
|
869
895
|
*
|
|
870
896
|
* @example
|
|
871
897
|
* ```ts
|
|
872
898
|
* import { getWalletsByOwner } from '@dcentralab/d402-client'
|
|
899
|
+
* import { useAccount } from 'wagmi'
|
|
873
900
|
*
|
|
874
|
-
*
|
|
901
|
+
* const { address } = useAccount()
|
|
902
|
+
*
|
|
903
|
+
* // Check if user has any wallets
|
|
875
904
|
* const wallets = await getWalletsByOwner({
|
|
876
|
-
* ownerAddress:
|
|
905
|
+
* ownerAddress: address,
|
|
877
906
|
* network: 'sepolia'
|
|
878
907
|
* })
|
|
879
908
|
*
|
|
880
|
-
*
|
|
881
|
-
*
|
|
909
|
+
* if (wallets.length === 0) {
|
|
910
|
+
* // Show "Create Wallet" button
|
|
911
|
+
* } else {
|
|
912
|
+
* // Use existing wallet: wallets[0]
|
|
913
|
+
* }
|
|
882
914
|
* ```
|
|
883
915
|
*/
|
|
884
916
|
declare function getWalletsByOwner(params: {
|
|
885
917
|
ownerAddress: Address;
|
|
886
|
-
network?: 'sepolia'
|
|
918
|
+
network?: 'sepolia';
|
|
887
919
|
rpcUrl?: string;
|
|
888
920
|
}): Promise<Address[]>;
|
|
921
|
+
/**
|
|
922
|
+
* Get available balance for a specific token in an IATPWallet.
|
|
923
|
+
*
|
|
924
|
+
* Queries the IATPWallet contract's getAvailableBalance function to retrieve
|
|
925
|
+
* the available balance for a given token.
|
|
926
|
+
*
|
|
927
|
+
* @param params - Query parameters
|
|
928
|
+
* @param params.publicClient - Viem PublicClient (from wagmi usePublicClient)
|
|
929
|
+
* @param params.walletAddress - IATPWallet contract address
|
|
930
|
+
* @param params.tokenAddress - Token contract address (e.g., USDC)
|
|
931
|
+
* @param params.network - Network to query (default: "sepolia")
|
|
932
|
+
* @returns Available balance in token's base units (wei)
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* ```ts
|
|
936
|
+
* import { getAvailableBalance } from '@dcentralab/d402-client'
|
|
937
|
+
* import { usePublicClient } from 'wagmi'
|
|
938
|
+
*
|
|
939
|
+
* const publicClient = usePublicClient()
|
|
940
|
+
*
|
|
941
|
+
* const balance = await getAvailableBalance({
|
|
942
|
+
* publicClient,
|
|
943
|
+
* walletAddress: '0xUserIATPWallet...',
|
|
944
|
+
* tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC on Sepolia
|
|
945
|
+
* })
|
|
946
|
+
*
|
|
947
|
+
* console.log('Available balance:', balance) // 1000000n (1 USDC)
|
|
948
|
+
* ```
|
|
949
|
+
*/
|
|
950
|
+
declare function getAvailableBalance(params: {
|
|
951
|
+
publicClient: PublicClient;
|
|
952
|
+
walletAddress: Address;
|
|
953
|
+
tokenAddress: Address;
|
|
954
|
+
network?: 'sepolia';
|
|
955
|
+
}): Promise<bigint>;
|
|
956
|
+
|
|
957
|
+
/**
|
|
958
|
+
* IATPSettlementLayer contract operations
|
|
959
|
+
*
|
|
960
|
+
* Functions for providers (utility agents) to query settlement balances,
|
|
961
|
+
* epochs, and manage withdrawals from the settlement layer.
|
|
962
|
+
*/
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Get locked balance for a provider across unreleased epochs.
|
|
966
|
+
*
|
|
967
|
+
* This is the balance that's locked in the settlement layer and cannot
|
|
968
|
+
* be withdrawn yet (still within the release delay period).
|
|
969
|
+
*
|
|
970
|
+
* @param params - Query parameters
|
|
971
|
+
* @param params.publicClient - Viem PublicClient (from wagmi usePublicClient)
|
|
972
|
+
* @param params.settlementLayerAddress - IATPSettlementLayer contract address
|
|
973
|
+
* @param params.providerAddress - Provider (utility agent) address
|
|
974
|
+
* @param params.network - Network (default: "sepolia")
|
|
975
|
+
* @returns Locked balance in base units (wei)
|
|
976
|
+
*
|
|
977
|
+
* @example
|
|
978
|
+
* ```ts
|
|
979
|
+
* import { getLockedBalanceForProvider } from '@dcentralab/d402-client'
|
|
980
|
+
*
|
|
981
|
+
* const locked = await getLockedBalanceForProvider({
|
|
982
|
+
* publicClient,
|
|
983
|
+
* settlementLayerAddress: '0x...',
|
|
984
|
+
* providerAddress: '0xUtilityAgent...',
|
|
985
|
+
* })
|
|
986
|
+
*
|
|
987
|
+
* console.log('Locked balance:', locked)
|
|
988
|
+
* ```
|
|
989
|
+
*/
|
|
990
|
+
declare function getLockedBalanceForProvider(params: {
|
|
991
|
+
publicClient: PublicClient;
|
|
992
|
+
settlementLayerAddress: Address;
|
|
993
|
+
providerAddress: Address;
|
|
994
|
+
network?: 'sepolia';
|
|
995
|
+
}): Promise<bigint>;
|
|
996
|
+
/**
|
|
997
|
+
* Get unlocked balance for a provider across released epochs.
|
|
998
|
+
*
|
|
999
|
+
* This is the balance that's available for withdrawal (epochs have passed
|
|
1000
|
+
* the release delay and haven't been withdrawn yet).
|
|
1001
|
+
*
|
|
1002
|
+
* @param params - Query parameters
|
|
1003
|
+
* @param params.publicClient - Viem PublicClient (from wagmi usePublicClient)
|
|
1004
|
+
* @param params.settlementLayerAddress - IATPSettlementLayer contract address
|
|
1005
|
+
* @param params.providerAddress - Provider (utility agent) address
|
|
1006
|
+
* @param params.network - Network (default: "sepolia")
|
|
1007
|
+
* @returns Unlocked balance in base units (wei)
|
|
1008
|
+
*
|
|
1009
|
+
* @example
|
|
1010
|
+
* ```ts
|
|
1011
|
+
* import { getUnlockedBalanceForProvider } from '@dcentralab/d402-client'
|
|
1012
|
+
*
|
|
1013
|
+
* const unlocked = await getUnlockedBalanceForProvider({
|
|
1014
|
+
* publicClient,
|
|
1015
|
+
* settlementLayerAddress: '0x...',
|
|
1016
|
+
* providerAddress: '0xUtilityAgent...',
|
|
1017
|
+
* })
|
|
1018
|
+
*
|
|
1019
|
+
* console.log('Unlocked balance:', unlocked)
|
|
1020
|
+
* ```
|
|
1021
|
+
*/
|
|
1022
|
+
declare function getUnlockedBalanceForProvider(params: {
|
|
1023
|
+
publicClient: PublicClient;
|
|
1024
|
+
settlementLayerAddress: Address;
|
|
1025
|
+
providerAddress: Address;
|
|
1026
|
+
network?: 'sepolia';
|
|
1027
|
+
}): Promise<bigint>;
|
|
889
1028
|
|
|
890
|
-
export { CHAIN_IDS, ContractName, D402Client, type D402ClientConfig, type D402Config, DEFAULT_VALIDITY_WINDOW_SECONDS, type EIP712Domain, EIP712_TYPES, Invalid402ResponseError, MissingRequestConfigError, NETWORKS, PaymentAlreadyAttemptedError, PaymentAmountExceededError, type PaymentAuthorization, PaymentError, type PaymentRequirement, type PaymentSelector, type PaymentSelectorOptions, PaymentVerificationError, type SignedPayment, type SupportedContractNetwork, type SupportedNetwork, TOKEN_ADDRESSES, UnsupportedNetworkError, UnsupportedSchemeError, type WalletCreationResult, createIATPWallet, createPaymentSelector, decodePayment, decodePaymentResponse, encodePayment, findMatchingPaymentRequirement, formatMoney, generateNonce, getAllContractAddresses, getChainId, getContractAbi, getContractAddress, getContractConfig, getCurrentTimestamp, getUsdcAddress, getWalletsByOwner, isContractDeployed, isValidAddress, normalizeAddress, parseMoney, parsePaymentRequirement, selectPaymentRequirement, signD402Payment, sortPaymentRequirements, usdToUsdc };
|
|
1029
|
+
export { CHAIN_IDS, ContractName, D402Client, type D402ClientConfig, type D402Config, DEFAULT_VALIDITY_WINDOW_SECONDS, type EIP712Domain, EIP712_TYPES, Invalid402ResponseError, MissingRequestConfigError, NETWORKS, PaymentAlreadyAttemptedError, PaymentAmountExceededError, type PaymentAuthorization, PaymentError, type PaymentRequirement, type PaymentSelector, type PaymentSelectorOptions, PaymentVerificationError, type SignedPayment, type SupportedContractNetwork, type SupportedNetwork, TOKEN_ADDRESSES, UnsupportedNetworkError, UnsupportedSchemeError, type WalletCreationResult, createIATPWallet, createPaymentSelector, decodePayment, decodePaymentResponse, encodePayment, findMatchingPaymentRequirement, formatMoney, generateNonce, getAllContractAddresses, getAvailableBalance, getChainId, getContractAbi, getContractAddress, getContractConfig, getCurrentTimestamp, getLockedBalanceForProvider, getUnlockedBalanceForProvider, getUsdcAddress, getWalletsByOwner, isContractDeployed, isValidAddress, normalizeAddress, parseMoney, parsePaymentRequirement, selectPaymentRequirement, signD402Payment, sortPaymentRequirements, usdToUsdc };
|