@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.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { http, createPublicClient, createWalletClient, decodeEventLog,
|
|
1
|
+
import { http, createPublicClient, createWalletClient, decodeEventLog, isAddress } from 'viem';
|
|
2
2
|
import { sepolia } from 'viem/chains';
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -11,10 +11,10 @@ var __export = (target, all) => {
|
|
|
11
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
// src/errors.ts
|
|
14
|
+
// src/core/errors.ts
|
|
15
15
|
var PaymentError, PaymentAmountExceededError, MissingRequestConfigError, PaymentAlreadyAttemptedError, UnsupportedSchemeError, PaymentVerificationError, Invalid402ResponseError, UnsupportedNetworkError;
|
|
16
16
|
var init_errors = __esm({
|
|
17
|
-
"src/errors.ts"() {
|
|
17
|
+
"src/core/errors.ts"() {
|
|
18
18
|
PaymentError = class _PaymentError extends Error {
|
|
19
19
|
constructor(message) {
|
|
20
20
|
super(message);
|
|
@@ -81,7 +81,7 @@ var init_errors = __esm({
|
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
-
// src/parser.ts
|
|
84
|
+
// src/payment/parser.ts
|
|
85
85
|
var parser_exports = {};
|
|
86
86
|
__export(parser_exports, {
|
|
87
87
|
parseAllPaymentRequirements: () => parseAllPaymentRequirements,
|
|
@@ -187,29 +187,18 @@ async function parseAllPaymentRequirements(response) {
|
|
|
187
187
|
return requirements;
|
|
188
188
|
}
|
|
189
189
|
var init_parser = __esm({
|
|
190
|
-
"src/parser.ts"() {
|
|
190
|
+
"src/payment/parser.ts"() {
|
|
191
191
|
init_errors();
|
|
192
192
|
}
|
|
193
193
|
});
|
|
194
194
|
|
|
195
|
-
// src/constants.ts
|
|
196
|
-
var
|
|
195
|
+
// src/core/constants.ts
|
|
196
|
+
var TOKEN_ADDRESSES, EIP712_TYPES;
|
|
197
197
|
var init_constants = __esm({
|
|
198
|
-
"src/constants.ts"() {
|
|
199
|
-
CHAIN_IDS = {
|
|
200
|
-
sepolia: 11155111
|
|
201
|
-
};
|
|
202
|
-
NETWORKS = {
|
|
203
|
-
sepolia: {
|
|
204
|
-
chainId: 11155111,
|
|
205
|
-
name: "Sepolia Testnet",
|
|
206
|
-
nativeCurrency: { name: "Sepolia Ether", symbol: "SEP", decimals: 18 }
|
|
207
|
-
}
|
|
208
|
-
};
|
|
198
|
+
"src/core/constants.ts"() {
|
|
209
199
|
TOKEN_ADDRESSES = {
|
|
210
200
|
sepolia: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
|
|
211
201
|
};
|
|
212
|
-
DEFAULT_VALIDITY_WINDOW_SECONDS = 300;
|
|
213
202
|
EIP712_TYPES = {
|
|
214
203
|
PullFundsForSettlement: [
|
|
215
204
|
{ name: "wallet", type: "address" },
|
|
@@ -223,7 +212,7 @@ var init_constants = __esm({
|
|
|
223
212
|
}
|
|
224
213
|
});
|
|
225
214
|
|
|
226
|
-
// src/utils.ts
|
|
215
|
+
// src/core/utils.ts
|
|
227
216
|
function parseMoney(amount, decimals) {
|
|
228
217
|
if (typeof amount === "bigint") {
|
|
229
218
|
return amount;
|
|
@@ -311,13 +300,13 @@ function decodePaymentResponse(header) {
|
|
|
311
300
|
}
|
|
312
301
|
}
|
|
313
302
|
var init_utils = __esm({
|
|
314
|
-
"src/utils.ts"() {
|
|
303
|
+
"src/core/utils.ts"() {
|
|
315
304
|
init_constants();
|
|
316
305
|
init_errors();
|
|
317
306
|
}
|
|
318
307
|
});
|
|
319
308
|
|
|
320
|
-
// src/signer.ts
|
|
309
|
+
// src/payment/signer.ts
|
|
321
310
|
var signer_exports = {};
|
|
322
311
|
__export(signer_exports, {
|
|
323
312
|
signD402Payment: () => signD402Payment
|
|
@@ -388,14 +377,13 @@ async function signD402Payment(params) {
|
|
|
388
377
|
return signedPayment;
|
|
389
378
|
}
|
|
390
379
|
var init_signer = __esm({
|
|
391
|
-
"src/signer.ts"() {
|
|
392
|
-
init_utils();
|
|
380
|
+
"src/payment/signer.ts"() {
|
|
393
381
|
init_utils();
|
|
394
382
|
init_constants();
|
|
395
383
|
}
|
|
396
384
|
});
|
|
397
385
|
|
|
398
|
-
// src/encoder.ts
|
|
386
|
+
// src/payment/encoder.ts
|
|
399
387
|
var encoder_exports = {};
|
|
400
388
|
__export(encoder_exports, {
|
|
401
389
|
decodePayment: () => decodePayment,
|
|
@@ -422,11 +410,11 @@ function decodePayment(encodedPayment) {
|
|
|
422
410
|
return JSON.parse(jsonString);
|
|
423
411
|
}
|
|
424
412
|
var init_encoder = __esm({
|
|
425
|
-
"src/encoder.ts"() {
|
|
413
|
+
"src/payment/encoder.ts"() {
|
|
426
414
|
}
|
|
427
415
|
});
|
|
428
416
|
|
|
429
|
-
// src/selector.ts
|
|
417
|
+
// src/payment/selector.ts
|
|
430
418
|
init_errors();
|
|
431
419
|
function selectPaymentRequirement(requirements, options = {}) {
|
|
432
420
|
const {
|
|
@@ -5013,7 +5001,14 @@ var implementations_default = {
|
|
|
5013
5001
|
}
|
|
5014
5002
|
};
|
|
5015
5003
|
|
|
5016
|
-
// src/contracts.ts
|
|
5004
|
+
// src/contracts/index.ts
|
|
5005
|
+
var ContractName = /* @__PURE__ */ ((ContractName2) => {
|
|
5006
|
+
ContractName2["IATP_WALLET"] = "IATPWallet";
|
|
5007
|
+
ContractName2["IATP_WALLET_FACTORY"] = "IATPWalletFactory";
|
|
5008
|
+
ContractName2["IATP_SETTLEMENT_LAYER"] = "IATPSettlementLayer";
|
|
5009
|
+
ContractName2["ROLE_MANAGER"] = "RoleManager";
|
|
5010
|
+
return ContractName2;
|
|
5011
|
+
})(ContractName || {});
|
|
5017
5012
|
var ABIS = {
|
|
5018
5013
|
sepolia: sepolia_default.sepolia || {}
|
|
5019
5014
|
};
|
|
@@ -5043,7 +5038,7 @@ function getContractConfig(contractName, network = "sepolia") {
|
|
|
5043
5038
|
return { address, abi };
|
|
5044
5039
|
}
|
|
5045
5040
|
|
|
5046
|
-
// src/wallet.ts
|
|
5041
|
+
// src/wallet/creation.ts
|
|
5047
5042
|
async function createIATPWallet(params) {
|
|
5048
5043
|
const { ownerAccount, network = "sepolia", rpcUrl } = params;
|
|
5049
5044
|
const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
|
|
@@ -5131,6 +5126,8 @@ async function getWalletsByOwner(params) {
|
|
|
5131
5126
|
});
|
|
5132
5127
|
return wallets;
|
|
5133
5128
|
}
|
|
5129
|
+
|
|
5130
|
+
// src/wallet/queries.ts
|
|
5134
5131
|
async function getAvailableBalance(params) {
|
|
5135
5132
|
const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
|
|
5136
5133
|
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
@@ -5145,6 +5142,8 @@ async function getAvailableBalance(params) {
|
|
|
5145
5142
|
});
|
|
5146
5143
|
return balance;
|
|
5147
5144
|
}
|
|
5145
|
+
|
|
5146
|
+
// src/wallet/withdrawals.ts
|
|
5148
5147
|
async function getWithdrawalRequest(params) {
|
|
5149
5148
|
const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
|
|
5150
5149
|
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
@@ -5168,26 +5167,27 @@ async function requestWithdrawal(params) {
|
|
|
5168
5167
|
if (!walletConfig) {
|
|
5169
5168
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5170
5169
|
}
|
|
5171
|
-
|
|
5170
|
+
await publicClient.simulateContract({
|
|
5171
|
+
account,
|
|
5172
|
+
address: walletAddress,
|
|
5172
5173
|
abi: walletConfig.abi,
|
|
5173
5174
|
functionName: "requestWithdrawal",
|
|
5174
5175
|
args: [tokenAddress, amount]
|
|
5175
5176
|
});
|
|
5176
|
-
const estimatedGas = await publicClient.
|
|
5177
|
-
account: account.address,
|
|
5178
|
-
to: walletAddress,
|
|
5179
|
-
data
|
|
5180
|
-
});
|
|
5181
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5182
|
-
const { request } = await publicClient.simulateContract({
|
|
5183
|
-
account,
|
|
5177
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5184
5178
|
address: walletAddress,
|
|
5185
5179
|
abi: walletConfig.abi,
|
|
5186
5180
|
functionName: "requestWithdrawal",
|
|
5187
|
-
args: [tokenAddress, amount]
|
|
5181
|
+
args: [tokenAddress, amount],
|
|
5182
|
+
account
|
|
5188
5183
|
});
|
|
5184
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5189
5185
|
const hash = await walletClient.writeContract({
|
|
5190
|
-
|
|
5186
|
+
address: walletAddress,
|
|
5187
|
+
abi: walletConfig.abi,
|
|
5188
|
+
functionName: "requestWithdrawal",
|
|
5189
|
+
args: [tokenAddress, amount],
|
|
5190
|
+
account,
|
|
5191
5191
|
gas: gasLimit
|
|
5192
5192
|
});
|
|
5193
5193
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
@@ -5199,33 +5199,34 @@ async function executeWithdrawal(params) {
|
|
|
5199
5199
|
if (!walletConfig) {
|
|
5200
5200
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5201
5201
|
}
|
|
5202
|
-
|
|
5202
|
+
await publicClient.simulateContract({
|
|
5203
|
+
account,
|
|
5204
|
+
address: walletAddress,
|
|
5203
5205
|
abi: walletConfig.abi,
|
|
5204
5206
|
functionName: "executeWithdrawal",
|
|
5205
5207
|
args: [tokenAddress]
|
|
5206
5208
|
});
|
|
5207
|
-
const estimatedGas = await publicClient.
|
|
5208
|
-
account: account.address,
|
|
5209
|
-
to: walletAddress,
|
|
5210
|
-
data
|
|
5211
|
-
});
|
|
5212
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5213
|
-
const { request } = await publicClient.simulateContract({
|
|
5214
|
-
account,
|
|
5209
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5215
5210
|
address: walletAddress,
|
|
5216
5211
|
abi: walletConfig.abi,
|
|
5217
5212
|
functionName: "executeWithdrawal",
|
|
5218
|
-
args: [tokenAddress]
|
|
5213
|
+
args: [tokenAddress],
|
|
5214
|
+
account
|
|
5219
5215
|
});
|
|
5216
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5220
5217
|
const hash = await walletClient.writeContract({
|
|
5221
|
-
|
|
5218
|
+
address: walletAddress,
|
|
5219
|
+
abi: walletConfig.abi,
|
|
5220
|
+
functionName: "executeWithdrawal",
|
|
5221
|
+
args: [tokenAddress],
|
|
5222
|
+
account,
|
|
5222
5223
|
gas: gasLimit
|
|
5223
5224
|
});
|
|
5224
5225
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5225
5226
|
return hash;
|
|
5226
5227
|
}
|
|
5227
5228
|
|
|
5228
|
-
// src/client.ts
|
|
5229
|
+
// src/client/D402Client.ts
|
|
5229
5230
|
var D402Client = class {
|
|
5230
5231
|
/**
|
|
5231
5232
|
* Create a new D402 Client.
|
|
@@ -5418,9 +5419,8 @@ var D402Client = class {
|
|
|
5418
5419
|
init_signer();
|
|
5419
5420
|
init_parser();
|
|
5420
5421
|
init_encoder();
|
|
5421
|
-
|
|
5422
|
-
|
|
5423
|
-
init_constants();
|
|
5422
|
+
|
|
5423
|
+
// src/settlement/queries.ts
|
|
5424
5424
|
async function getLockedBalanceForProvider(params) {
|
|
5425
5425
|
const { publicClient, providerAddress, tokenAddress, network = "sepolia" } = params;
|
|
5426
5426
|
const settlementConfig = getContractConfig("IATPSettlementLayer" /* IATP_SETTLEMENT_LAYER */, network);
|
|
@@ -5449,6 +5449,8 @@ async function getUnlockedBalanceForProvider(params) {
|
|
|
5449
5449
|
});
|
|
5450
5450
|
return balance;
|
|
5451
5451
|
}
|
|
5452
|
+
|
|
5453
|
+
// src/settlement/operations.ts
|
|
5452
5454
|
async function withdrawAllAvailableEpochs(params) {
|
|
5453
5455
|
const { walletClient, publicClient, tokenAddress, network = "sepolia" } = params;
|
|
5454
5456
|
if (!walletClient?.account) {
|
|
@@ -5459,32 +5461,37 @@ async function withdrawAllAvailableEpochs(params) {
|
|
|
5459
5461
|
if (!settlementConfig) {
|
|
5460
5462
|
throw new Error(`IATPSettlementLayer contract not found for network: ${network}`);
|
|
5461
5463
|
}
|
|
5462
|
-
|
|
5464
|
+
await publicClient.simulateContract({
|
|
5465
|
+
account,
|
|
5466
|
+
address: settlementConfig.address,
|
|
5463
5467
|
abi: settlementConfig.abi,
|
|
5464
5468
|
functionName: "withdrawAllAvailableEpochs",
|
|
5465
5469
|
args: [tokenAddress]
|
|
5466
5470
|
});
|
|
5467
|
-
const estimatedGas = await publicClient.
|
|
5468
|
-
account: account.address,
|
|
5469
|
-
to: settlementConfig.address,
|
|
5470
|
-
data
|
|
5471
|
-
});
|
|
5472
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5473
|
-
const { request } = await publicClient.simulateContract({
|
|
5474
|
-
account,
|
|
5471
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5475
5472
|
address: settlementConfig.address,
|
|
5476
5473
|
abi: settlementConfig.abi,
|
|
5477
5474
|
functionName: "withdrawAllAvailableEpochs",
|
|
5478
|
-
args: [tokenAddress]
|
|
5475
|
+
args: [tokenAddress],
|
|
5476
|
+
account
|
|
5479
5477
|
});
|
|
5478
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5480
5479
|
const hash = await walletClient.writeContract({
|
|
5481
|
-
|
|
5480
|
+
address: settlementConfig.address,
|
|
5481
|
+
abi: settlementConfig.abi,
|
|
5482
|
+
functionName: "withdrawAllAvailableEpochs",
|
|
5483
|
+
args: [tokenAddress],
|
|
5484
|
+
account,
|
|
5482
5485
|
gas: gasLimit
|
|
5483
5486
|
});
|
|
5484
5487
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5485
5488
|
return hash;
|
|
5486
5489
|
}
|
|
5487
5490
|
|
|
5488
|
-
|
|
5491
|
+
// src/index.ts
|
|
5492
|
+
init_utils();
|
|
5493
|
+
init_errors();
|
|
5494
|
+
|
|
5495
|
+
export { ContractName, D402Client, Invalid402ResponseError, MissingRequestConfigError, PaymentAlreadyAttemptedError, PaymentAmountExceededError, PaymentError, PaymentVerificationError, UnsupportedNetworkError, UnsupportedSchemeError, createIATPWallet, createPaymentSelector, decodePayment, decodePaymentResponse, encodePayment, executeWithdrawal, findMatchingPaymentRequirement, formatMoney, generateNonce, getAvailableBalance, getChainId, getContractAbi, getContractAddress, getContractConfig, getCurrentTimestamp, getLockedBalanceForProvider, getUnlockedBalanceForProvider, getUsdcAddress, getWalletsByOwner, getWithdrawalRequest, isValidAddress, normalizeAddress, parseAllPaymentRequirements, parseMoney, parsePaymentRequirement, requestWithdrawal, selectPaymentRequirement, signD402Payment, sortPaymentRequirements, usdToUsdc, withdrawAllAvailableEpochs };
|
|
5489
5496
|
//# sourceMappingURL=index.mjs.map
|
|
5490
5497
|
//# sourceMappingURL=index.mjs.map
|