@dcentralab/d402-client 0.2.2 → 0.2.4

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/index.d.mts CHANGED
@@ -1,5 +1,5 @@
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
@@ -191,11 +191,10 @@ interface D402ClientConfig {
191
191
  */
192
192
  operatorAccount: Account;
193
193
  /**
194
- * IATPWallet contract address (optional).
195
- * If not provided, uses operatorAccount.address directly.
194
+ * IATPWallet contract address.
196
195
  * Get from `createIATPWallet()` or `getWalletsByOwner()`.
197
196
  */
198
- iatpWalletAddress?: `0x${string}`;
197
+ iatpWalletAddress: `0x${string}`;
199
198
  /**
200
199
  * Optional safety limit for maximum payment amount per request in base units.
201
200
  *
@@ -308,6 +307,41 @@ declare class D402Client {
308
307
  * @returns Maximum value in base units, or undefined if no limit
309
308
  */
310
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>;
311
345
  /**
312
346
  * Fetch with automatic 402 payment handling.
313
347
  *
@@ -884,5 +918,118 @@ declare function getWalletsByOwner(params: {
884
918
  network?: 'sepolia';
885
919
  rpcUrl?: string;
886
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.tokenAddress - Token contract address
975
+ * @param params.network - Network (default: "sepolia")
976
+ * @returns Locked balance in base units (wei)
977
+ *
978
+ * @example
979
+ * ```ts
980
+ * import { getLockedBalanceForProvider } from '@dcentralab/d402-client'
981
+ *
982
+ * const locked = await getLockedBalanceForProvider({
983
+ * publicClient,
984
+ * settlementLayerAddress: '0x...',
985
+ * providerAddress: '0xUtilityAgent...',
986
+ * tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
987
+ * })
988
+ *
989
+ * console.log('Locked balance:', locked)
990
+ * ```
991
+ */
992
+ declare function getLockedBalanceForProvider(params: {
993
+ publicClient: PublicClient;
994
+ settlementLayerAddress: Address;
995
+ providerAddress: Address;
996
+ tokenAddress: Address;
997
+ network?: 'sepolia';
998
+ }): Promise<bigint>;
999
+ /**
1000
+ * Get unlocked balance for a provider across released epochs.
1001
+ *
1002
+ * This is the balance that's available for withdrawal (epochs have passed
1003
+ * the release delay and haven't been withdrawn yet).
1004
+ *
1005
+ * @param params - Query parameters
1006
+ * @param params.publicClient - Viem PublicClient (from wagmi usePublicClient)
1007
+ * @param params.settlementLayerAddress - IATPSettlementLayer contract address
1008
+ * @param params.providerAddress - Provider (utility agent) address
1009
+ * @param params.tokenAddress - Token contract address
1010
+ * @param params.network - Network (default: "sepolia")
1011
+ * @returns Unlocked balance in base units (wei)
1012
+ *
1013
+ * @example
1014
+ * ```ts
1015
+ * import { getUnlockedBalanceForProvider } from '@dcentralab/d402-client'
1016
+ *
1017
+ * const unlocked = await getUnlockedBalanceForProvider({
1018
+ * publicClient,
1019
+ * settlementLayerAddress: '0x...',
1020
+ * providerAddress: '0xUtilityAgent...',
1021
+ * tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
1022
+ * })
1023
+ *
1024
+ * console.log('Unlocked balance:', unlocked)
1025
+ * ```
1026
+ */
1027
+ declare function getUnlockedBalanceForProvider(params: {
1028
+ publicClient: PublicClient;
1029
+ settlementLayerAddress: Address;
1030
+ providerAddress: Address;
1031
+ tokenAddress: Address;
1032
+ network?: 'sepolia';
1033
+ }): Promise<bigint>;
887
1034
 
888
- 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 };
1035
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
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
@@ -191,11 +191,10 @@ interface D402ClientConfig {
191
191
  */
192
192
  operatorAccount: Account;
193
193
  /**
194
- * IATPWallet contract address (optional).
195
- * If not provided, uses operatorAccount.address directly.
194
+ * IATPWallet contract address.
196
195
  * Get from `createIATPWallet()` or `getWalletsByOwner()`.
197
196
  */
198
- iatpWalletAddress?: `0x${string}`;
197
+ iatpWalletAddress: `0x${string}`;
199
198
  /**
200
199
  * Optional safety limit for maximum payment amount per request in base units.
201
200
  *
@@ -308,6 +307,41 @@ declare class D402Client {
308
307
  * @returns Maximum value in base units, or undefined if no limit
309
308
  */
310
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>;
311
345
  /**
312
346
  * Fetch with automatic 402 payment handling.
313
347
  *
@@ -884,5 +918,118 @@ declare function getWalletsByOwner(params: {
884
918
  network?: 'sepolia';
885
919
  rpcUrl?: string;
886
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.tokenAddress - Token contract address
975
+ * @param params.network - Network (default: "sepolia")
976
+ * @returns Locked balance in base units (wei)
977
+ *
978
+ * @example
979
+ * ```ts
980
+ * import { getLockedBalanceForProvider } from '@dcentralab/d402-client'
981
+ *
982
+ * const locked = await getLockedBalanceForProvider({
983
+ * publicClient,
984
+ * settlementLayerAddress: '0x...',
985
+ * providerAddress: '0xUtilityAgent...',
986
+ * tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
987
+ * })
988
+ *
989
+ * console.log('Locked balance:', locked)
990
+ * ```
991
+ */
992
+ declare function getLockedBalanceForProvider(params: {
993
+ publicClient: PublicClient;
994
+ settlementLayerAddress: Address;
995
+ providerAddress: Address;
996
+ tokenAddress: Address;
997
+ network?: 'sepolia';
998
+ }): Promise<bigint>;
999
+ /**
1000
+ * Get unlocked balance for a provider across released epochs.
1001
+ *
1002
+ * This is the balance that's available for withdrawal (epochs have passed
1003
+ * the release delay and haven't been withdrawn yet).
1004
+ *
1005
+ * @param params - Query parameters
1006
+ * @param params.publicClient - Viem PublicClient (from wagmi usePublicClient)
1007
+ * @param params.settlementLayerAddress - IATPSettlementLayer contract address
1008
+ * @param params.providerAddress - Provider (utility agent) address
1009
+ * @param params.tokenAddress - Token contract address
1010
+ * @param params.network - Network (default: "sepolia")
1011
+ * @returns Unlocked balance in base units (wei)
1012
+ *
1013
+ * @example
1014
+ * ```ts
1015
+ * import { getUnlockedBalanceForProvider } from '@dcentralab/d402-client'
1016
+ *
1017
+ * const unlocked = await getUnlockedBalanceForProvider({
1018
+ * publicClient,
1019
+ * settlementLayerAddress: '0x...',
1020
+ * providerAddress: '0xUtilityAgent...',
1021
+ * tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
1022
+ * })
1023
+ *
1024
+ * console.log('Unlocked balance:', unlocked)
1025
+ * ```
1026
+ */
1027
+ declare function getUnlockedBalanceForProvider(params: {
1028
+ publicClient: PublicClient;
1029
+ settlementLayerAddress: Address;
1030
+ providerAddress: Address;
1031
+ tokenAddress: Address;
1032
+ network?: 'sepolia';
1033
+ }): Promise<bigint>;
887
1034
 
888
- 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 };
1035
+ 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 };