@dcentralab/d402-client 0.2.7 → 0.3.0
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/LICENSE +22 -0
- package/README.md +58 -553
- package/dist/index.d.mts +452 -283
- package/dist/index.d.ts +452 -283
- package/dist/index.js +84 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +74 -67
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
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
|
-
|
|
197
|
+
// src/core/constants.ts
|
|
198
|
+
var TOKEN_ADDRESSES, EIP712_TYPES;
|
|
199
199
|
var init_constants = __esm({
|
|
200
|
-
"src/constants.ts"() {
|
|
201
|
-
|
|
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
|
-
|
|
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 =
|
|
241
|
+
const address = TOKEN_ADDRESSES[network];
|
|
253
242
|
if (!address) {
|
|
254
243
|
throw new exports.UnsupportedNetworkError(network);
|
|
255
244
|
}
|
|
@@ -313,13 +302,13 @@ function decodePaymentResponse(header) {
|
|
|
313
302
|
}
|
|
314
303
|
}
|
|
315
304
|
var init_utils = __esm({
|
|
316
|
-
"src/utils.ts"() {
|
|
305
|
+
"src/core/utils.ts"() {
|
|
317
306
|
init_constants();
|
|
318
307
|
init_errors();
|
|
319
308
|
}
|
|
320
309
|
});
|
|
321
310
|
|
|
322
|
-
// src/signer.ts
|
|
311
|
+
// src/payment/signer.ts
|
|
323
312
|
var signer_exports = {};
|
|
324
313
|
__export(signer_exports, {
|
|
325
314
|
signD402Payment: () => signD402Payment
|
|
@@ -365,7 +354,7 @@ async function signD402Payment(params) {
|
|
|
365
354
|
const signature = await operatorAccount.signTypedData({
|
|
366
355
|
domain,
|
|
367
356
|
types: {
|
|
368
|
-
PullFundsForSettlement:
|
|
357
|
+
PullFundsForSettlement: EIP712_TYPES.PullFundsForSettlement
|
|
369
358
|
},
|
|
370
359
|
primaryType: "PullFundsForSettlement",
|
|
371
360
|
message
|
|
@@ -390,14 +379,13 @@ async function signD402Payment(params) {
|
|
|
390
379
|
return signedPayment;
|
|
391
380
|
}
|
|
392
381
|
var init_signer = __esm({
|
|
393
|
-
"src/signer.ts"() {
|
|
394
|
-
init_utils();
|
|
382
|
+
"src/payment/signer.ts"() {
|
|
395
383
|
init_utils();
|
|
396
384
|
init_constants();
|
|
397
385
|
}
|
|
398
386
|
});
|
|
399
387
|
|
|
400
|
-
// src/encoder.ts
|
|
388
|
+
// src/payment/encoder.ts
|
|
401
389
|
var encoder_exports = {};
|
|
402
390
|
__export(encoder_exports, {
|
|
403
391
|
decodePayment: () => decodePayment,
|
|
@@ -424,11 +412,11 @@ function decodePayment(encodedPayment) {
|
|
|
424
412
|
return JSON.parse(jsonString);
|
|
425
413
|
}
|
|
426
414
|
var init_encoder = __esm({
|
|
427
|
-
"src/encoder.ts"() {
|
|
415
|
+
"src/payment/encoder.ts"() {
|
|
428
416
|
}
|
|
429
417
|
});
|
|
430
418
|
|
|
431
|
-
// src/selector.ts
|
|
419
|
+
// src/payment/selector.ts
|
|
432
420
|
init_errors();
|
|
433
421
|
function selectPaymentRequirement(requirements, options = {}) {
|
|
434
422
|
const {
|
|
@@ -5015,7 +5003,14 @@ var implementations_default = {
|
|
|
5015
5003
|
}
|
|
5016
5004
|
};
|
|
5017
5005
|
|
|
5018
|
-
// src/contracts.ts
|
|
5006
|
+
// src/contracts/index.ts
|
|
5007
|
+
var ContractName = /* @__PURE__ */ ((ContractName2) => {
|
|
5008
|
+
ContractName2["IATP_WALLET"] = "IATPWallet";
|
|
5009
|
+
ContractName2["IATP_WALLET_FACTORY"] = "IATPWalletFactory";
|
|
5010
|
+
ContractName2["IATP_SETTLEMENT_LAYER"] = "IATPSettlementLayer";
|
|
5011
|
+
ContractName2["ROLE_MANAGER"] = "RoleManager";
|
|
5012
|
+
return ContractName2;
|
|
5013
|
+
})(ContractName || {});
|
|
5019
5014
|
var ABIS = {
|
|
5020
5015
|
sepolia: sepolia_default.sepolia || {}
|
|
5021
5016
|
};
|
|
@@ -5045,7 +5040,7 @@ function getContractConfig(contractName, network = "sepolia") {
|
|
|
5045
5040
|
return { address, abi };
|
|
5046
5041
|
}
|
|
5047
5042
|
|
|
5048
|
-
// src/wallet.ts
|
|
5043
|
+
// src/wallet/creation.ts
|
|
5049
5044
|
async function createIATPWallet(params) {
|
|
5050
5045
|
const { ownerAccount, network = "sepolia", rpcUrl } = params;
|
|
5051
5046
|
const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
|
|
@@ -5133,6 +5128,8 @@ async function getWalletsByOwner(params) {
|
|
|
5133
5128
|
});
|
|
5134
5129
|
return wallets;
|
|
5135
5130
|
}
|
|
5131
|
+
|
|
5132
|
+
// src/wallet/queries.ts
|
|
5136
5133
|
async function getAvailableBalance(params) {
|
|
5137
5134
|
const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
|
|
5138
5135
|
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
@@ -5147,6 +5144,8 @@ async function getAvailableBalance(params) {
|
|
|
5147
5144
|
});
|
|
5148
5145
|
return balance;
|
|
5149
5146
|
}
|
|
5147
|
+
|
|
5148
|
+
// src/wallet/withdrawals.ts
|
|
5150
5149
|
async function getWithdrawalRequest(params) {
|
|
5151
5150
|
const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
|
|
5152
5151
|
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
@@ -5170,26 +5169,27 @@ async function requestWithdrawal(params) {
|
|
|
5170
5169
|
if (!walletConfig) {
|
|
5171
5170
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5172
5171
|
}
|
|
5173
|
-
|
|
5172
|
+
await publicClient.simulateContract({
|
|
5173
|
+
account,
|
|
5174
|
+
address: walletAddress,
|
|
5174
5175
|
abi: walletConfig.abi,
|
|
5175
5176
|
functionName: "requestWithdrawal",
|
|
5176
5177
|
args: [tokenAddress, amount]
|
|
5177
5178
|
});
|
|
5178
|
-
const estimatedGas = await publicClient.
|
|
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,
|
|
5179
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5186
5180
|
address: walletAddress,
|
|
5187
5181
|
abi: walletConfig.abi,
|
|
5188
5182
|
functionName: "requestWithdrawal",
|
|
5189
|
-
args: [tokenAddress, amount]
|
|
5183
|
+
args: [tokenAddress, amount],
|
|
5184
|
+
account
|
|
5190
5185
|
});
|
|
5186
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5191
5187
|
const hash = await walletClient.writeContract({
|
|
5192
|
-
|
|
5188
|
+
address: walletAddress,
|
|
5189
|
+
abi: walletConfig.abi,
|
|
5190
|
+
functionName: "requestWithdrawal",
|
|
5191
|
+
args: [tokenAddress, amount],
|
|
5192
|
+
account,
|
|
5193
5193
|
gas: gasLimit
|
|
5194
5194
|
});
|
|
5195
5195
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
@@ -5201,33 +5201,34 @@ async function executeWithdrawal(params) {
|
|
|
5201
5201
|
if (!walletConfig) {
|
|
5202
5202
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5203
5203
|
}
|
|
5204
|
-
|
|
5204
|
+
await publicClient.simulateContract({
|
|
5205
|
+
account,
|
|
5206
|
+
address: walletAddress,
|
|
5205
5207
|
abi: walletConfig.abi,
|
|
5206
5208
|
functionName: "executeWithdrawal",
|
|
5207
5209
|
args: [tokenAddress]
|
|
5208
5210
|
});
|
|
5209
|
-
const estimatedGas = await publicClient.
|
|
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,
|
|
5211
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5217
5212
|
address: walletAddress,
|
|
5218
5213
|
abi: walletConfig.abi,
|
|
5219
5214
|
functionName: "executeWithdrawal",
|
|
5220
|
-
args: [tokenAddress]
|
|
5215
|
+
args: [tokenAddress],
|
|
5216
|
+
account
|
|
5221
5217
|
});
|
|
5218
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5222
5219
|
const hash = await walletClient.writeContract({
|
|
5223
|
-
|
|
5220
|
+
address: walletAddress,
|
|
5221
|
+
abi: walletConfig.abi,
|
|
5222
|
+
functionName: "executeWithdrawal",
|
|
5223
|
+
args: [tokenAddress],
|
|
5224
|
+
account,
|
|
5224
5225
|
gas: gasLimit
|
|
5225
5226
|
});
|
|
5226
5227
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5227
5228
|
return hash;
|
|
5228
5229
|
}
|
|
5229
5230
|
|
|
5230
|
-
// src/client.ts
|
|
5231
|
+
// src/client/D402Client.ts
|
|
5231
5232
|
var D402Client = class {
|
|
5232
5233
|
/**
|
|
5233
5234
|
* Create a new D402 Client.
|
|
@@ -5420,9 +5421,8 @@ var D402Client = class {
|
|
|
5420
5421
|
init_signer();
|
|
5421
5422
|
init_parser();
|
|
5422
5423
|
init_encoder();
|
|
5423
|
-
|
|
5424
|
-
|
|
5425
|
-
init_constants();
|
|
5424
|
+
|
|
5425
|
+
// src/settlement/queries.ts
|
|
5426
5426
|
async function getLockedBalanceForProvider(params) {
|
|
5427
5427
|
const { publicClient, providerAddress, tokenAddress, network = "sepolia" } = params;
|
|
5428
5428
|
const settlementConfig = getContractConfig("IATPSettlementLayer" /* IATP_SETTLEMENT_LAYER */, network);
|
|
@@ -5451,6 +5451,8 @@ async function getUnlockedBalanceForProvider(params) {
|
|
|
5451
5451
|
});
|
|
5452
5452
|
return balance;
|
|
5453
5453
|
}
|
|
5454
|
+
|
|
5455
|
+
// src/settlement/operations.ts
|
|
5454
5456
|
async function withdrawAllAvailableEpochs(params) {
|
|
5455
5457
|
const { walletClient, publicClient, tokenAddress, network = "sepolia" } = params;
|
|
5456
5458
|
if (!walletClient?.account) {
|
|
@@ -5461,52 +5463,65 @@ async function withdrawAllAvailableEpochs(params) {
|
|
|
5461
5463
|
if (!settlementConfig) {
|
|
5462
5464
|
throw new Error(`IATPSettlementLayer contract not found for network: ${network}`);
|
|
5463
5465
|
}
|
|
5464
|
-
|
|
5466
|
+
await publicClient.simulateContract({
|
|
5467
|
+
account,
|
|
5468
|
+
address: settlementConfig.address,
|
|
5465
5469
|
abi: settlementConfig.abi,
|
|
5466
5470
|
functionName: "withdrawAllAvailableEpochs",
|
|
5467
5471
|
args: [tokenAddress]
|
|
5468
5472
|
});
|
|
5469
|
-
const estimatedGas = await publicClient.
|
|
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,
|
|
5473
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5477
5474
|
address: settlementConfig.address,
|
|
5478
5475
|
abi: settlementConfig.abi,
|
|
5479
5476
|
functionName: "withdrawAllAvailableEpochs",
|
|
5480
|
-
args: [tokenAddress]
|
|
5477
|
+
args: [tokenAddress],
|
|
5478
|
+
account
|
|
5481
5479
|
});
|
|
5480
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5482
5481
|
const hash = await walletClient.writeContract({
|
|
5483
|
-
|
|
5482
|
+
address: settlementConfig.address,
|
|
5483
|
+
abi: settlementConfig.abi,
|
|
5484
|
+
functionName: "withdrawAllAvailableEpochs",
|
|
5485
|
+
args: [tokenAddress],
|
|
5486
|
+
account,
|
|
5484
5487
|
gas: gasLimit
|
|
5485
5488
|
});
|
|
5486
5489
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5487
5490
|
return hash;
|
|
5488
5491
|
}
|
|
5489
5492
|
|
|
5493
|
+
// src/index.ts
|
|
5494
|
+
init_utils();
|
|
5495
|
+
init_errors();
|
|
5496
|
+
|
|
5497
|
+
exports.ContractName = ContractName;
|
|
5490
5498
|
exports.D402Client = D402Client;
|
|
5491
5499
|
exports.createIATPWallet = createIATPWallet;
|
|
5492
5500
|
exports.createPaymentSelector = createPaymentSelector;
|
|
5493
5501
|
exports.decodePayment = decodePayment;
|
|
5494
5502
|
exports.decodePaymentResponse = decodePaymentResponse;
|
|
5495
5503
|
exports.encodePayment = encodePayment;
|
|
5504
|
+
exports.executeWithdrawal = executeWithdrawal;
|
|
5496
5505
|
exports.findMatchingPaymentRequirement = findMatchingPaymentRequirement;
|
|
5497
5506
|
exports.formatMoney = formatMoney;
|
|
5498
5507
|
exports.generateNonce = generateNonce;
|
|
5499
5508
|
exports.getAvailableBalance = getAvailableBalance;
|
|
5500
5509
|
exports.getChainId = getChainId;
|
|
5510
|
+
exports.getContractAbi = getContractAbi;
|
|
5511
|
+
exports.getContractAddress = getContractAddress;
|
|
5512
|
+
exports.getContractConfig = getContractConfig;
|
|
5501
5513
|
exports.getCurrentTimestamp = getCurrentTimestamp;
|
|
5502
5514
|
exports.getLockedBalanceForProvider = getLockedBalanceForProvider;
|
|
5503
5515
|
exports.getUnlockedBalanceForProvider = getUnlockedBalanceForProvider;
|
|
5504
5516
|
exports.getUsdcAddress = getUsdcAddress;
|
|
5505
5517
|
exports.getWalletsByOwner = getWalletsByOwner;
|
|
5518
|
+
exports.getWithdrawalRequest = getWithdrawalRequest;
|
|
5506
5519
|
exports.isValidAddress = isValidAddress;
|
|
5507
5520
|
exports.normalizeAddress = normalizeAddress;
|
|
5521
|
+
exports.parseAllPaymentRequirements = parseAllPaymentRequirements;
|
|
5508
5522
|
exports.parseMoney = parseMoney;
|
|
5509
5523
|
exports.parsePaymentRequirement = parsePaymentRequirement;
|
|
5524
|
+
exports.requestWithdrawal = requestWithdrawal;
|
|
5510
5525
|
exports.selectPaymentRequirement = selectPaymentRequirement;
|
|
5511
5526
|
exports.signD402Payment = signD402Payment;
|
|
5512
5527
|
exports.sortPaymentRequirements = sortPaymentRequirements;
|