@dcentralab/d402-client 0.2.7 → 0.3.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/index.js CHANGED
@@ -13,10 +13,10 @@ var __export = (target, all) => {
13
13
  __defProp(target, name, { get: all[name], enumerable: true });
14
14
  };
15
15
 
16
- // src/errors.ts
16
+ // src/core/errors.ts
17
17
  exports.PaymentError = void 0; exports.PaymentAmountExceededError = void 0; exports.MissingRequestConfigError = void 0; exports.PaymentAlreadyAttemptedError = void 0; exports.UnsupportedSchemeError = void 0; exports.PaymentVerificationError = void 0; exports.Invalid402ResponseError = void 0; exports.UnsupportedNetworkError = void 0;
18
18
  var init_errors = __esm({
19
- "src/errors.ts"() {
19
+ "src/core/errors.ts"() {
20
20
  exports.PaymentError = class _PaymentError extends Error {
21
21
  constructor(message) {
22
22
  super(message);
@@ -83,7 +83,7 @@ var init_errors = __esm({
83
83
  }
84
84
  });
85
85
 
86
- // src/parser.ts
86
+ // src/payment/parser.ts
87
87
  var parser_exports = {};
88
88
  __export(parser_exports, {
89
89
  parseAllPaymentRequirements: () => parseAllPaymentRequirements,
@@ -189,30 +189,19 @@ async function parseAllPaymentRequirements(response) {
189
189
  return requirements;
190
190
  }
191
191
  var init_parser = __esm({
192
- "src/parser.ts"() {
192
+ "src/payment/parser.ts"() {
193
193
  init_errors();
194
194
  }
195
195
  });
196
196
 
197
- // src/constants.ts
198
- exports.CHAIN_IDS = void 0; exports.NETWORKS = void 0; exports.TOKEN_ADDRESSES = void 0; exports.DEFAULT_VALIDITY_WINDOW_SECONDS = void 0; exports.EIP712_TYPES = void 0;
197
+ // src/core/constants.ts
198
+ var TOKEN_ADDRESSES, EIP712_TYPES;
199
199
  var init_constants = __esm({
200
- "src/constants.ts"() {
201
- exports.CHAIN_IDS = {
202
- sepolia: 11155111
203
- };
204
- exports.NETWORKS = {
205
- sepolia: {
206
- chainId: 11155111,
207
- name: "Sepolia Testnet",
208
- nativeCurrency: { name: "Sepolia Ether", symbol: "SEP", decimals: 18 }
209
- }
210
- };
211
- exports.TOKEN_ADDRESSES = {
200
+ "src/core/constants.ts"() {
201
+ TOKEN_ADDRESSES = {
212
202
  sepolia: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
213
203
  };
214
- exports.DEFAULT_VALIDITY_WINDOW_SECONDS = 300;
215
- exports.EIP712_TYPES = {
204
+ EIP712_TYPES = {
216
205
  PullFundsForSettlement: [
217
206
  { name: "wallet", type: "address" },
218
207
  { name: "provider", type: "address" },
@@ -225,7 +214,7 @@ var init_constants = __esm({
225
214
  }
226
215
  });
227
216
 
228
- // src/utils.ts
217
+ // src/core/utils.ts
229
218
  function parseMoney(amount, decimals) {
230
219
  if (typeof amount === "bigint") {
231
220
  return amount;
@@ -249,7 +238,7 @@ function formatMoney(amount, decimals) {
249
238
  return `${whole}.${fraction}`;
250
239
  }
251
240
  function getUsdcAddress(network) {
252
- const address = exports.TOKEN_ADDRESSES[network];
241
+ const address = TOKEN_ADDRESSES[network];
253
242
  if (!address) {
254
243
  throw new exports.UnsupportedNetworkError(network);
255
244
  }
@@ -278,13 +267,6 @@ function getChainId(network) {
278
267
  }
279
268
  return chainId;
280
269
  }
281
- function findMatchingPaymentRequirement(requirements, scheme = "exact", network) {
282
- return requirements.find((req) => {
283
- const schemeMatches = req.scheme === scheme;
284
- const networkMatches = !network || req.network === network;
285
- return schemeMatches && networkMatches;
286
- });
287
- }
288
270
  function usdToUsdc(usdAmount, decimals = 6) {
289
271
  return parseMoney(usdAmount, decimals);
290
272
  }
@@ -302,24 +284,14 @@ function isValidAddress(address) {
302
284
  function normalizeAddress(address) {
303
285
  return address.toLowerCase();
304
286
  }
305
- function decodePaymentResponse(header) {
306
- try {
307
- const binString = atob(header);
308
- const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
309
- const decoded = new TextDecoder().decode(bytes);
310
- return JSON.parse(decoded);
311
- } catch (error) {
312
- throw new Error(`Failed to decode payment response: ${error}`);
313
- }
314
- }
315
287
  var init_utils = __esm({
316
- "src/utils.ts"() {
288
+ "src/core/utils.ts"() {
317
289
  init_constants();
318
290
  init_errors();
319
291
  }
320
292
  });
321
293
 
322
- // src/signer.ts
294
+ // src/payment/signer.ts
323
295
  var signer_exports = {};
324
296
  __export(signer_exports, {
325
297
  signD402Payment: () => signD402Payment
@@ -365,7 +337,7 @@ async function signD402Payment(params) {
365
337
  const signature = await operatorAccount.signTypedData({
366
338
  domain,
367
339
  types: {
368
- PullFundsForSettlement: exports.EIP712_TYPES.PullFundsForSettlement
340
+ PullFundsForSettlement: EIP712_TYPES.PullFundsForSettlement
369
341
  },
370
342
  primaryType: "PullFundsForSettlement",
371
343
  message
@@ -390,17 +362,17 @@ async function signD402Payment(params) {
390
362
  return signedPayment;
391
363
  }
392
364
  var init_signer = __esm({
393
- "src/signer.ts"() {
394
- init_utils();
365
+ "src/payment/signer.ts"() {
395
366
  init_utils();
396
367
  init_constants();
397
368
  }
398
369
  });
399
370
 
400
- // src/encoder.ts
371
+ // src/payment/encoder.ts
401
372
  var encoder_exports = {};
402
373
  __export(encoder_exports, {
403
374
  decodePayment: () => decodePayment,
375
+ decodePaymentResponse: () => decodePaymentResponse,
404
376
  encodePayment: () => encodePayment,
405
377
  safeBase64Decode: () => safeBase64Decode,
406
378
  safeBase64Encode: () => safeBase64Encode
@@ -423,12 +395,20 @@ function decodePayment(encodedPayment) {
423
395
  const jsonString = safeBase64Decode(encodedPayment);
424
396
  return JSON.parse(jsonString);
425
397
  }
398
+ function decodePaymentResponse(header) {
399
+ try {
400
+ const jsonString = safeBase64Decode(header);
401
+ return JSON.parse(jsonString);
402
+ } catch (error) {
403
+ throw new Error(`Failed to decode payment response: ${error instanceof Error ? error.message : "Unknown error"}`);
404
+ }
405
+ }
426
406
  var init_encoder = __esm({
427
- "src/encoder.ts"() {
407
+ "src/payment/encoder.ts"() {
428
408
  }
429
409
  });
430
410
 
431
- // src/selector.ts
411
+ // src/payment/selector.ts
432
412
  init_errors();
433
413
  function selectPaymentRequirement(requirements, options = {}) {
434
414
  const {
@@ -510,6 +490,13 @@ function sortPaymentRequirements(requirements, preferredNetwork) {
510
490
  return 0;
511
491
  });
512
492
  }
493
+ function findMatchingPaymentRequirement(requirements, scheme = "exact", network) {
494
+ return requirements.find((req) => {
495
+ const schemeMatches = req.scheme === scheme;
496
+ const networkMatches = !network || req.network === network;
497
+ return schemeMatches && networkMatches;
498
+ });
499
+ }
513
500
 
514
501
  // src/contracts/abis/sepolia.json
515
502
  var sepolia_default = {
@@ -5015,7 +5002,14 @@ var implementations_default = {
5015
5002
  }
5016
5003
  };
5017
5004
 
5018
- // src/contracts.ts
5005
+ // src/contracts/index.ts
5006
+ var ContractName = /* @__PURE__ */ ((ContractName2) => {
5007
+ ContractName2["IATP_WALLET"] = "IATPWallet";
5008
+ ContractName2["IATP_WALLET_FACTORY"] = "IATPWalletFactory";
5009
+ ContractName2["IATP_SETTLEMENT_LAYER"] = "IATPSettlementLayer";
5010
+ ContractName2["ROLE_MANAGER"] = "RoleManager";
5011
+ return ContractName2;
5012
+ })(ContractName || {});
5019
5013
  var ABIS = {
5020
5014
  sepolia: sepolia_default.sepolia || {}
5021
5015
  };
@@ -5045,7 +5039,7 @@ function getContractConfig(contractName, network = "sepolia") {
5045
5039
  return { address, abi };
5046
5040
  }
5047
5041
 
5048
- // src/wallet.ts
5042
+ // src/wallet/creation.ts
5049
5043
  async function createIATPWallet(params) {
5050
5044
  const { ownerAccount, network = "sepolia", rpcUrl } = params;
5051
5045
  const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
@@ -5113,6 +5107,20 @@ async function createIATPWallet(params) {
5113
5107
  chainId: chain.id
5114
5108
  };
5115
5109
  }
5110
+ async function getAvailableBalance(params) {
5111
+ const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
5112
+ const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5113
+ if (!walletConfig) {
5114
+ throw new Error(`IATPWallet contract not found for network: ${network}`);
5115
+ }
5116
+ const balance = await publicClient.readContract({
5117
+ address: walletAddress,
5118
+ abi: walletConfig.abi,
5119
+ functionName: "getAvailableBalance",
5120
+ args: [tokenAddress]
5121
+ });
5122
+ return balance;
5123
+ }
5116
5124
  async function getWalletsByOwner(params) {
5117
5125
  const { ownerAddress, network = "sepolia", rpcUrl } = params;
5118
5126
  const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
@@ -5133,20 +5141,8 @@ async function getWalletsByOwner(params) {
5133
5141
  });
5134
5142
  return wallets;
5135
5143
  }
5136
- async function getAvailableBalance(params) {
5137
- const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
5138
- const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5139
- if (!walletConfig) {
5140
- throw new Error(`IATPWallet contract not found for network: ${network}`);
5141
- }
5142
- const balance = await publicClient.readContract({
5143
- address: walletAddress,
5144
- abi: walletConfig.abi,
5145
- functionName: "getAvailableBalance",
5146
- args: [tokenAddress]
5147
- });
5148
- return balance;
5149
- }
5144
+
5145
+ // src/wallet/withdrawals.ts
5150
5146
  async function getWithdrawalRequest(params) {
5151
5147
  const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
5152
5148
  const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
@@ -5170,26 +5166,27 @@ async function requestWithdrawal(params) {
5170
5166
  if (!walletConfig) {
5171
5167
  throw new Error(`IATPWallet contract not found for network: ${network}`);
5172
5168
  }
5173
- const data = viem.encodeFunctionData({
5169
+ await publicClient.simulateContract({
5170
+ account,
5171
+ address: walletAddress,
5174
5172
  abi: walletConfig.abi,
5175
5173
  functionName: "requestWithdrawal",
5176
5174
  args: [tokenAddress, amount]
5177
5175
  });
5178
- const estimatedGas = await publicClient.estimateGas({
5179
- account: account.address,
5180
- to: walletAddress,
5181
- data
5182
- });
5183
- const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
5184
- const { request } = await publicClient.simulateContract({
5185
- account,
5176
+ const estimatedGas = await publicClient.estimateContractGas({
5186
5177
  address: walletAddress,
5187
5178
  abi: walletConfig.abi,
5188
5179
  functionName: "requestWithdrawal",
5189
- args: [tokenAddress, amount]
5180
+ args: [tokenAddress, amount],
5181
+ account
5190
5182
  });
5183
+ const gasLimit = estimatedGas + estimatedGas / 5n;
5191
5184
  const hash = await walletClient.writeContract({
5192
- ...request,
5185
+ address: walletAddress,
5186
+ abi: walletConfig.abi,
5187
+ functionName: "requestWithdrawal",
5188
+ args: [tokenAddress, amount],
5189
+ account,
5193
5190
  gas: gasLimit
5194
5191
  });
5195
5192
  await publicClient.waitForTransactionReceipt({ hash });
@@ -5201,33 +5198,34 @@ async function executeWithdrawal(params) {
5201
5198
  if (!walletConfig) {
5202
5199
  throw new Error(`IATPWallet contract not found for network: ${network}`);
5203
5200
  }
5204
- const data = viem.encodeFunctionData({
5201
+ await publicClient.simulateContract({
5202
+ account,
5203
+ address: walletAddress,
5205
5204
  abi: walletConfig.abi,
5206
5205
  functionName: "executeWithdrawal",
5207
5206
  args: [tokenAddress]
5208
5207
  });
5209
- const estimatedGas = await publicClient.estimateGas({
5210
- account: account.address,
5211
- to: walletAddress,
5212
- data
5213
- });
5214
- const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
5215
- const { request } = await publicClient.simulateContract({
5216
- account,
5208
+ const estimatedGas = await publicClient.estimateContractGas({
5217
5209
  address: walletAddress,
5218
5210
  abi: walletConfig.abi,
5219
5211
  functionName: "executeWithdrawal",
5220
- args: [tokenAddress]
5212
+ args: [tokenAddress],
5213
+ account
5221
5214
  });
5215
+ const gasLimit = estimatedGas + estimatedGas / 5n;
5222
5216
  const hash = await walletClient.writeContract({
5223
- ...request,
5217
+ address: walletAddress,
5218
+ abi: walletConfig.abi,
5219
+ functionName: "executeWithdrawal",
5220
+ args: [tokenAddress],
5221
+ account,
5224
5222
  gas: gasLimit
5225
5223
  });
5226
5224
  await publicClient.waitForTransactionReceipt({ hash });
5227
5225
  return hash;
5228
5226
  }
5229
5227
 
5230
- // src/client.ts
5228
+ // src/client/D402Client.ts
5231
5229
  var D402Client = class {
5232
5230
  /**
5233
5231
  * Create a new D402 Client.
@@ -5420,9 +5418,8 @@ var D402Client = class {
5420
5418
  init_signer();
5421
5419
  init_parser();
5422
5420
  init_encoder();
5423
- init_utils();
5424
- init_errors();
5425
- init_constants();
5421
+
5422
+ // src/settlement/queries.ts
5426
5423
  async function getLockedBalanceForProvider(params) {
5427
5424
  const { publicClient, providerAddress, tokenAddress, network = "sepolia" } = params;
5428
5425
  const settlementConfig = getContractConfig("IATPSettlementLayer" /* IATP_SETTLEMENT_LAYER */, network);
@@ -5451,6 +5448,8 @@ async function getUnlockedBalanceForProvider(params) {
5451
5448
  });
5452
5449
  return balance;
5453
5450
  }
5451
+
5452
+ // src/settlement/operations.ts
5454
5453
  async function withdrawAllAvailableEpochs(params) {
5455
5454
  const { walletClient, publicClient, tokenAddress, network = "sepolia" } = params;
5456
5455
  if (!walletClient?.account) {
@@ -5461,52 +5460,65 @@ async function withdrawAllAvailableEpochs(params) {
5461
5460
  if (!settlementConfig) {
5462
5461
  throw new Error(`IATPSettlementLayer contract not found for network: ${network}`);
5463
5462
  }
5464
- const data = viem.encodeFunctionData({
5463
+ await publicClient.simulateContract({
5464
+ account,
5465
+ address: settlementConfig.address,
5465
5466
  abi: settlementConfig.abi,
5466
5467
  functionName: "withdrawAllAvailableEpochs",
5467
5468
  args: [tokenAddress]
5468
5469
  });
5469
- const estimatedGas = await publicClient.estimateGas({
5470
- account: account.address,
5471
- to: settlementConfig.address,
5472
- data
5473
- });
5474
- const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
5475
- const { request } = await publicClient.simulateContract({
5476
- account,
5470
+ const estimatedGas = await publicClient.estimateContractGas({
5477
5471
  address: settlementConfig.address,
5478
5472
  abi: settlementConfig.abi,
5479
5473
  functionName: "withdrawAllAvailableEpochs",
5480
- args: [tokenAddress]
5474
+ args: [tokenAddress],
5475
+ account
5481
5476
  });
5477
+ const gasLimit = estimatedGas + estimatedGas / 5n;
5482
5478
  const hash = await walletClient.writeContract({
5483
- ...request,
5479
+ address: settlementConfig.address,
5480
+ abi: settlementConfig.abi,
5481
+ functionName: "withdrawAllAvailableEpochs",
5482
+ args: [tokenAddress],
5483
+ account,
5484
5484
  gas: gasLimit
5485
5485
  });
5486
5486
  await publicClient.waitForTransactionReceipt({ hash });
5487
5487
  return hash;
5488
5488
  }
5489
5489
 
5490
+ // src/index.ts
5491
+ init_utils();
5492
+ init_errors();
5493
+
5494
+ exports.ContractName = ContractName;
5490
5495
  exports.D402Client = D402Client;
5491
5496
  exports.createIATPWallet = createIATPWallet;
5492
5497
  exports.createPaymentSelector = createPaymentSelector;
5493
5498
  exports.decodePayment = decodePayment;
5494
5499
  exports.decodePaymentResponse = decodePaymentResponse;
5495
5500
  exports.encodePayment = encodePayment;
5501
+ exports.executeWithdrawal = executeWithdrawal;
5496
5502
  exports.findMatchingPaymentRequirement = findMatchingPaymentRequirement;
5497
5503
  exports.formatMoney = formatMoney;
5498
5504
  exports.generateNonce = generateNonce;
5499
5505
  exports.getAvailableBalance = getAvailableBalance;
5500
5506
  exports.getChainId = getChainId;
5507
+ exports.getContractAbi = getContractAbi;
5508
+ exports.getContractAddress = getContractAddress;
5509
+ exports.getContractConfig = getContractConfig;
5501
5510
  exports.getCurrentTimestamp = getCurrentTimestamp;
5502
5511
  exports.getLockedBalanceForProvider = getLockedBalanceForProvider;
5503
5512
  exports.getUnlockedBalanceForProvider = getUnlockedBalanceForProvider;
5504
5513
  exports.getUsdcAddress = getUsdcAddress;
5505
5514
  exports.getWalletsByOwner = getWalletsByOwner;
5515
+ exports.getWithdrawalRequest = getWithdrawalRequest;
5506
5516
  exports.isValidAddress = isValidAddress;
5507
5517
  exports.normalizeAddress = normalizeAddress;
5518
+ exports.parseAllPaymentRequirements = parseAllPaymentRequirements;
5508
5519
  exports.parseMoney = parseMoney;
5509
5520
  exports.parsePaymentRequirement = parsePaymentRequirement;
5521
+ exports.requestWithdrawal = requestWithdrawal;
5510
5522
  exports.selectPaymentRequirement = selectPaymentRequirement;
5511
5523
  exports.signD402Payment = signD402Payment;
5512
5524
  exports.sortPaymentRequirements = sortPaymentRequirements;