@dzapio/sdk 2.0.38 → 2.0.40
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 +678 -674
- package/dist/index.d.ts +678 -674
- package/dist/index.js +111 -84
- package/dist/index.mjs +111 -84
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -765,7 +765,9 @@ var DZapPriceProvider = class {
|
|
|
765
765
|
this.requiresChainConfig = false;
|
|
766
766
|
this.fetchPrices = async (chainId, tokenAddresses) => {
|
|
767
767
|
try {
|
|
768
|
-
|
|
768
|
+
if (tokenAddresses.length === 0) return {};
|
|
769
|
+
const tokens = tokenAddresses.join(",");
|
|
770
|
+
const tokenPrices = await fetchTokenPrice(tokens, chainId);
|
|
769
771
|
return tokenPrices;
|
|
770
772
|
} catch (e) {
|
|
771
773
|
console.error("Failed to fetch token price", e);
|
|
@@ -14278,6 +14280,97 @@ var getPermit2Signature = async (params) => {
|
|
|
14278
14280
|
|
|
14279
14281
|
// src/transactionHandlers/permit.ts
|
|
14280
14282
|
var _PermitTxnHandler = class _PermitTxnHandler {
|
|
14283
|
+
static async signPermit(signPermitReq) {
|
|
14284
|
+
const { tokens } = signPermitReq;
|
|
14285
|
+
if (tokens.length === 0) {
|
|
14286
|
+
if (signPermitReq.permitType === PermitTypes.PermitBatchWitnessTransferFrom) {
|
|
14287
|
+
return {
|
|
14288
|
+
status: "success" /* success */,
|
|
14289
|
+
code: 200 /* Success */,
|
|
14290
|
+
batchPermitData: DEFAULT_PERMIT2_DATA,
|
|
14291
|
+
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14292
|
+
};
|
|
14293
|
+
}
|
|
14294
|
+
return { status: "success" /* success */, code: 200 /* Success */, tokens, permitType: signPermitReq.permitType };
|
|
14295
|
+
}
|
|
14296
|
+
const oneToMany = tokens.length > 1 && isOneToMany(tokens[0].address, tokens[1].address);
|
|
14297
|
+
const shouldUseBatchPermit = _PermitTxnHandler.shouldUseBatchPermit({
|
|
14298
|
+
permitType: signPermitReq.permitType,
|
|
14299
|
+
isBatchPermitAllowed: signPermitReq.isBatchPermitAllowed,
|
|
14300
|
+
tokens,
|
|
14301
|
+
oneToMany,
|
|
14302
|
+
contractVersion: signPermitReq.contractVersion,
|
|
14303
|
+
service: signPermitReq.service
|
|
14304
|
+
});
|
|
14305
|
+
if (shouldUseBatchPermit) {
|
|
14306
|
+
const resp = await _PermitTxnHandler.generateBatchPermitDataForTokens({
|
|
14307
|
+
...signPermitReq,
|
|
14308
|
+
tokens: tokens.map((token, index) => ({ ...token, index })),
|
|
14309
|
+
account: signPermitReq.sender,
|
|
14310
|
+
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14311
|
+
//override because only PermitBatchWitnessTransferFrom supports batch
|
|
14312
|
+
});
|
|
14313
|
+
if (resp.status !== "success" /* success */) {
|
|
14314
|
+
return { status: resp.status, code: resp.code, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14315
|
+
}
|
|
14316
|
+
if (signPermitReq.signatureCallback) {
|
|
14317
|
+
await signPermitReq.signatureCallback({
|
|
14318
|
+
batchPermitData: resp.permitData,
|
|
14319
|
+
tokens,
|
|
14320
|
+
permitType: resp.permitType
|
|
14321
|
+
});
|
|
14322
|
+
}
|
|
14323
|
+
return {
|
|
14324
|
+
status: "success" /* success */,
|
|
14325
|
+
code: 200 /* Success */,
|
|
14326
|
+
batchPermitData: resp.permitData,
|
|
14327
|
+
permitType: resp.permitType
|
|
14328
|
+
};
|
|
14329
|
+
} else {
|
|
14330
|
+
if (signPermitReq.permitType === PermitTypes.PermitBatchWitnessTransferFrom) {
|
|
14331
|
+
return { status: "error" /* error */, code: 500 /* Error */, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14332
|
+
}
|
|
14333
|
+
const totalSrcAmount = calcTotalSrcTokenAmount(tokens);
|
|
14334
|
+
let firstTokenNonce = null;
|
|
14335
|
+
let permitType = _PermitTxnHandler.v1PermitSupport({
|
|
14336
|
+
contractVersion: signPermitReq.contractVersion,
|
|
14337
|
+
service: signPermitReq.service
|
|
14338
|
+
}) ? PermitTypes.PermitSingle : signPermitReq.permitType;
|
|
14339
|
+
for (let dataIdx = 0; dataIdx < tokens.length; dataIdx++) {
|
|
14340
|
+
const isFirstToken = dataIdx === 0;
|
|
14341
|
+
const res = await _PermitTxnHandler.generatePermitDataForToken({
|
|
14342
|
+
...signPermitReq,
|
|
14343
|
+
token: {
|
|
14344
|
+
...tokens[dataIdx],
|
|
14345
|
+
index: dataIdx
|
|
14346
|
+
},
|
|
14347
|
+
firstTokenNonce: firstTokenNonce ?? void 0,
|
|
14348
|
+
oneToMany,
|
|
14349
|
+
totalSrcAmount,
|
|
14350
|
+
account: signPermitReq.sender,
|
|
14351
|
+
permitType
|
|
14352
|
+
});
|
|
14353
|
+
permitType = res.permitType;
|
|
14354
|
+
if (res.status !== "success" /* success */) {
|
|
14355
|
+
return { status: res.status, code: res.code, permitType: res.permitType };
|
|
14356
|
+
}
|
|
14357
|
+
tokens[dataIdx].permitData = res.permitData;
|
|
14358
|
+
if (isFirstToken && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14359
|
+
firstTokenNonce = res.nonce ?? null;
|
|
14360
|
+
}
|
|
14361
|
+
if (signPermitReq.signatureCallback && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14362
|
+
const amount = oneToMany && isFirstToken ? totalSrcAmount : BigInt(tokens[dataIdx].amount);
|
|
14363
|
+
await signPermitReq.signatureCallback({
|
|
14364
|
+
permitData: res.permitData,
|
|
14365
|
+
srcToken: tokens[dataIdx].address,
|
|
14366
|
+
amount: amount.toString(),
|
|
14367
|
+
permitType: res.permitType
|
|
14368
|
+
});
|
|
14369
|
+
}
|
|
14370
|
+
}
|
|
14371
|
+
return { status: "success" /* success */, tokens, code: 200 /* Success */, permitType };
|
|
14372
|
+
}
|
|
14373
|
+
}
|
|
14281
14374
|
};
|
|
14282
14375
|
_PermitTxnHandler.generateBatchPermitDataForTokens = async (params) => {
|
|
14283
14376
|
const resp = await getPermit2Signature(params);
|
|
@@ -14414,83 +14507,6 @@ _PermitTxnHandler.shouldUseBatchPermit = ({
|
|
|
14414
14507
|
const isContractSupport = !_PermitTxnHandler.v1PermitSupport({ contractVersion, service });
|
|
14415
14508
|
return isBatchPermitAllowed && (isBatchPermitRequested || shouldAutoBatch) && isContractSupport;
|
|
14416
14509
|
};
|
|
14417
|
-
_PermitTxnHandler.signPermit = async (signPermitReq) => {
|
|
14418
|
-
const { tokens } = signPermitReq;
|
|
14419
|
-
if (tokens.length === 0) {
|
|
14420
|
-
return { status: "success" /* success */, code: 200 /* Success */, tokens, permitType: signPermitReq.permitType };
|
|
14421
|
-
}
|
|
14422
|
-
const oneToMany = tokens.length > 1 && isOneToMany(tokens[0].address, tokens[1].address);
|
|
14423
|
-
const shouldUseBatchPermit = _PermitTxnHandler.shouldUseBatchPermit({
|
|
14424
|
-
permitType: signPermitReq.permitType,
|
|
14425
|
-
isBatchPermitAllowed: signPermitReq.isBatchPermitAllowed,
|
|
14426
|
-
tokens,
|
|
14427
|
-
oneToMany,
|
|
14428
|
-
contractVersion: signPermitReq.contractVersion,
|
|
14429
|
-
service: signPermitReq.service
|
|
14430
|
-
});
|
|
14431
|
-
if (shouldUseBatchPermit) {
|
|
14432
|
-
const resp = await _PermitTxnHandler.generateBatchPermitDataForTokens({
|
|
14433
|
-
...signPermitReq,
|
|
14434
|
-
tokens: tokens.map((token, index) => ({ ...token, index })),
|
|
14435
|
-
account: signPermitReq.sender,
|
|
14436
|
-
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14437
|
-
//override because only PermitBatchWitnessTransferFrom supports batch
|
|
14438
|
-
});
|
|
14439
|
-
if (resp.status !== "success" /* success */) {
|
|
14440
|
-
return { status: resp.status, code: resp.code, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14441
|
-
}
|
|
14442
|
-
if (signPermitReq.signatureCallback) {
|
|
14443
|
-
await signPermitReq.signatureCallback({
|
|
14444
|
-
batchPermitData: resp.permitData,
|
|
14445
|
-
tokens,
|
|
14446
|
-
permitType: resp.permitType
|
|
14447
|
-
});
|
|
14448
|
-
}
|
|
14449
|
-
return {
|
|
14450
|
-
status: "success" /* success */,
|
|
14451
|
-
code: 200 /* Success */,
|
|
14452
|
-
batchPermitData: resp.permitData,
|
|
14453
|
-
permitType: resp.permitType
|
|
14454
|
-
};
|
|
14455
|
-
} else {
|
|
14456
|
-
const totalSrcAmount = calcTotalSrcTokenAmount(tokens);
|
|
14457
|
-
let firstTokenNonce = null;
|
|
14458
|
-
let permitType = _PermitTxnHandler.v1PermitSupport({ contractVersion: signPermitReq.contractVersion, service: signPermitReq.service }) ? PermitTypes.PermitSingle : signPermitReq.permitType;
|
|
14459
|
-
for (let dataIdx = 0; dataIdx < tokens.length; dataIdx++) {
|
|
14460
|
-
const isFirstToken = dataIdx === 0;
|
|
14461
|
-
const res = await _PermitTxnHandler.generatePermitDataForToken({
|
|
14462
|
-
...signPermitReq,
|
|
14463
|
-
token: {
|
|
14464
|
-
...tokens[dataIdx],
|
|
14465
|
-
index: dataIdx
|
|
14466
|
-
},
|
|
14467
|
-
firstTokenNonce: firstTokenNonce ?? void 0,
|
|
14468
|
-
oneToMany,
|
|
14469
|
-
totalSrcAmount,
|
|
14470
|
-
account: signPermitReq.sender,
|
|
14471
|
-
permitType
|
|
14472
|
-
});
|
|
14473
|
-
permitType = res.permitType;
|
|
14474
|
-
if (res.status !== "success" /* success */) {
|
|
14475
|
-
return { status: res.status, code: res.code, permitType: res.permitType };
|
|
14476
|
-
}
|
|
14477
|
-
tokens[dataIdx].permitData = res.permitData;
|
|
14478
|
-
if (isFirstToken && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14479
|
-
firstTokenNonce = res.nonce ?? null;
|
|
14480
|
-
}
|
|
14481
|
-
if (signPermitReq.signatureCallback && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14482
|
-
const amount = oneToMany && isFirstToken ? totalSrcAmount : BigInt(tokens[dataIdx].amount);
|
|
14483
|
-
await signPermitReq.signatureCallback({
|
|
14484
|
-
permitData: res.permitData,
|
|
14485
|
-
srcToken: tokens[dataIdx].address,
|
|
14486
|
-
amount: amount.toString(),
|
|
14487
|
-
permitType: res.permitType
|
|
14488
|
-
});
|
|
14489
|
-
}
|
|
14490
|
-
}
|
|
14491
|
-
return { status: "success" /* success */, tokens, code: 200 /* Success */, permitType };
|
|
14492
|
-
}
|
|
14493
|
-
};
|
|
14494
14510
|
var PermitTxnHandler = _PermitTxnHandler;
|
|
14495
14511
|
var permit_default = PermitTxnHandler;
|
|
14496
14512
|
function encodeApproveCallData({ spender, amount }) {
|
|
@@ -16004,11 +16020,19 @@ var _DZapClient = class _DZapClient {
|
|
|
16004
16020
|
* @param params.permitType - Optional permit type (defaults to AutoPermit for optimal compatibility)
|
|
16005
16021
|
* @param params.signatureCallback - Optional callback function for each completed signature
|
|
16006
16022
|
* @param params.spender - Optional custom spender address (if not using default DZap contract)
|
|
16007
|
-
* @returns Promise resolving to
|
|
16023
|
+
* @returns Promise resolving to a {@link SignPermitResponse}, narrowed by the requested `permitType`:
|
|
16024
|
+
* - `AutoPermit` (default): `PermitSingleResponse | PermitBatchResponse | PermitErrorResponse` — the
|
|
16025
|
+
* effective mode is chosen at runtime, so narrow with `'batchPermitData' in result`.
|
|
16026
|
+
* - `PermitBatchWitnessTransferFrom`: `PermitBatchResponse | PermitErrorResponse`. If batch cannot be
|
|
16027
|
+
* fulfilled (e.g. a v1 contract or batch permits disabled) the result is an error rather than a
|
|
16028
|
+
* silent single-permit downgrade, so `batchPermitData` is guaranteed present on success.
|
|
16029
|
+
* - Any other (single) mode: `PermitSingleResponse | PermitErrorResponse`.
|
|
16030
|
+
* Always check `status === TxnStatus.success` before reading mode-specific fields.
|
|
16008
16031
|
*
|
|
16009
16032
|
* @example
|
|
16010
16033
|
* ```typescript
|
|
16011
|
-
*
|
|
16034
|
+
* // Explicit batch request -> result is typed PermitBatchResponse | PermitErrorResponse
|
|
16035
|
+
* const result = await client.sign({
|
|
16012
16036
|
* chainId: 1,
|
|
16013
16037
|
* sender: '0x...',
|
|
16014
16038
|
* tokens: [
|
|
@@ -16016,11 +16040,14 @@ var _DZapClient = class _DZapClient {
|
|
|
16016
16040
|
* ],
|
|
16017
16041
|
* service: 'swap',
|
|
16018
16042
|
* signer: walletClient,
|
|
16019
|
-
* permitType: PermitTypes.
|
|
16020
|
-
* signatureCallback: async ({ permitData, srcToken }) => {
|
|
16021
|
-
* console.log(`Signed permit for ${srcToken}:`, permitData);
|
|
16022
|
-
* }
|
|
16043
|
+
* permitType: PermitTypes.PermitBatchWitnessTransferFrom,
|
|
16023
16044
|
* });
|
|
16045
|
+
*
|
|
16046
|
+
* if (result.status === TxnStatus.success) {
|
|
16047
|
+
* console.log('batch permit:', result.batchPermitData);
|
|
16048
|
+
* } else {
|
|
16049
|
+
* console.log('batch permit could not be produced:', result.code);
|
|
16050
|
+
* }
|
|
16024
16051
|
* ```
|
|
16025
16052
|
*/
|
|
16026
16053
|
async sign(params) {
|
package/dist/index.mjs
CHANGED
|
@@ -739,7 +739,9 @@ var DZapPriceProvider = class {
|
|
|
739
739
|
this.requiresChainConfig = false;
|
|
740
740
|
this.fetchPrices = async (chainId, tokenAddresses) => {
|
|
741
741
|
try {
|
|
742
|
-
|
|
742
|
+
if (tokenAddresses.length === 0) return {};
|
|
743
|
+
const tokens = tokenAddresses.join(",");
|
|
744
|
+
const tokenPrices = await fetchTokenPrice(tokens, chainId);
|
|
743
745
|
return tokenPrices;
|
|
744
746
|
} catch (e) {
|
|
745
747
|
console.error("Failed to fetch token price", e);
|
|
@@ -14252,6 +14254,97 @@ var getPermit2Signature = async (params) => {
|
|
|
14252
14254
|
|
|
14253
14255
|
// src/transactionHandlers/permit.ts
|
|
14254
14256
|
var _PermitTxnHandler = class _PermitTxnHandler {
|
|
14257
|
+
static async signPermit(signPermitReq) {
|
|
14258
|
+
const { tokens } = signPermitReq;
|
|
14259
|
+
if (tokens.length === 0) {
|
|
14260
|
+
if (signPermitReq.permitType === PermitTypes.PermitBatchWitnessTransferFrom) {
|
|
14261
|
+
return {
|
|
14262
|
+
status: "success" /* success */,
|
|
14263
|
+
code: 200 /* Success */,
|
|
14264
|
+
batchPermitData: DEFAULT_PERMIT2_DATA,
|
|
14265
|
+
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14266
|
+
};
|
|
14267
|
+
}
|
|
14268
|
+
return { status: "success" /* success */, code: 200 /* Success */, tokens, permitType: signPermitReq.permitType };
|
|
14269
|
+
}
|
|
14270
|
+
const oneToMany = tokens.length > 1 && isOneToMany(tokens[0].address, tokens[1].address);
|
|
14271
|
+
const shouldUseBatchPermit = _PermitTxnHandler.shouldUseBatchPermit({
|
|
14272
|
+
permitType: signPermitReq.permitType,
|
|
14273
|
+
isBatchPermitAllowed: signPermitReq.isBatchPermitAllowed,
|
|
14274
|
+
tokens,
|
|
14275
|
+
oneToMany,
|
|
14276
|
+
contractVersion: signPermitReq.contractVersion,
|
|
14277
|
+
service: signPermitReq.service
|
|
14278
|
+
});
|
|
14279
|
+
if (shouldUseBatchPermit) {
|
|
14280
|
+
const resp = await _PermitTxnHandler.generateBatchPermitDataForTokens({
|
|
14281
|
+
...signPermitReq,
|
|
14282
|
+
tokens: tokens.map((token, index) => ({ ...token, index })),
|
|
14283
|
+
account: signPermitReq.sender,
|
|
14284
|
+
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14285
|
+
//override because only PermitBatchWitnessTransferFrom supports batch
|
|
14286
|
+
});
|
|
14287
|
+
if (resp.status !== "success" /* success */) {
|
|
14288
|
+
return { status: resp.status, code: resp.code, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14289
|
+
}
|
|
14290
|
+
if (signPermitReq.signatureCallback) {
|
|
14291
|
+
await signPermitReq.signatureCallback({
|
|
14292
|
+
batchPermitData: resp.permitData,
|
|
14293
|
+
tokens,
|
|
14294
|
+
permitType: resp.permitType
|
|
14295
|
+
});
|
|
14296
|
+
}
|
|
14297
|
+
return {
|
|
14298
|
+
status: "success" /* success */,
|
|
14299
|
+
code: 200 /* Success */,
|
|
14300
|
+
batchPermitData: resp.permitData,
|
|
14301
|
+
permitType: resp.permitType
|
|
14302
|
+
};
|
|
14303
|
+
} else {
|
|
14304
|
+
if (signPermitReq.permitType === PermitTypes.PermitBatchWitnessTransferFrom) {
|
|
14305
|
+
return { status: "error" /* error */, code: 500 /* Error */, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14306
|
+
}
|
|
14307
|
+
const totalSrcAmount = calcTotalSrcTokenAmount(tokens);
|
|
14308
|
+
let firstTokenNonce = null;
|
|
14309
|
+
let permitType = _PermitTxnHandler.v1PermitSupport({
|
|
14310
|
+
contractVersion: signPermitReq.contractVersion,
|
|
14311
|
+
service: signPermitReq.service
|
|
14312
|
+
}) ? PermitTypes.PermitSingle : signPermitReq.permitType;
|
|
14313
|
+
for (let dataIdx = 0; dataIdx < tokens.length; dataIdx++) {
|
|
14314
|
+
const isFirstToken = dataIdx === 0;
|
|
14315
|
+
const res = await _PermitTxnHandler.generatePermitDataForToken({
|
|
14316
|
+
...signPermitReq,
|
|
14317
|
+
token: {
|
|
14318
|
+
...tokens[dataIdx],
|
|
14319
|
+
index: dataIdx
|
|
14320
|
+
},
|
|
14321
|
+
firstTokenNonce: firstTokenNonce ?? void 0,
|
|
14322
|
+
oneToMany,
|
|
14323
|
+
totalSrcAmount,
|
|
14324
|
+
account: signPermitReq.sender,
|
|
14325
|
+
permitType
|
|
14326
|
+
});
|
|
14327
|
+
permitType = res.permitType;
|
|
14328
|
+
if (res.status !== "success" /* success */) {
|
|
14329
|
+
return { status: res.status, code: res.code, permitType: res.permitType };
|
|
14330
|
+
}
|
|
14331
|
+
tokens[dataIdx].permitData = res.permitData;
|
|
14332
|
+
if (isFirstToken && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14333
|
+
firstTokenNonce = res.nonce ?? null;
|
|
14334
|
+
}
|
|
14335
|
+
if (signPermitReq.signatureCallback && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14336
|
+
const amount = oneToMany && isFirstToken ? totalSrcAmount : BigInt(tokens[dataIdx].amount);
|
|
14337
|
+
await signPermitReq.signatureCallback({
|
|
14338
|
+
permitData: res.permitData,
|
|
14339
|
+
srcToken: tokens[dataIdx].address,
|
|
14340
|
+
amount: amount.toString(),
|
|
14341
|
+
permitType: res.permitType
|
|
14342
|
+
});
|
|
14343
|
+
}
|
|
14344
|
+
}
|
|
14345
|
+
return { status: "success" /* success */, tokens, code: 200 /* Success */, permitType };
|
|
14346
|
+
}
|
|
14347
|
+
}
|
|
14255
14348
|
};
|
|
14256
14349
|
_PermitTxnHandler.generateBatchPermitDataForTokens = async (params) => {
|
|
14257
14350
|
const resp = await getPermit2Signature(params);
|
|
@@ -14388,83 +14481,6 @@ _PermitTxnHandler.shouldUseBatchPermit = ({
|
|
|
14388
14481
|
const isContractSupport = !_PermitTxnHandler.v1PermitSupport({ contractVersion, service });
|
|
14389
14482
|
return isBatchPermitAllowed && (isBatchPermitRequested || shouldAutoBatch) && isContractSupport;
|
|
14390
14483
|
};
|
|
14391
|
-
_PermitTxnHandler.signPermit = async (signPermitReq) => {
|
|
14392
|
-
const { tokens } = signPermitReq;
|
|
14393
|
-
if (tokens.length === 0) {
|
|
14394
|
-
return { status: "success" /* success */, code: 200 /* Success */, tokens, permitType: signPermitReq.permitType };
|
|
14395
|
-
}
|
|
14396
|
-
const oneToMany = tokens.length > 1 && isOneToMany(tokens[0].address, tokens[1].address);
|
|
14397
|
-
const shouldUseBatchPermit = _PermitTxnHandler.shouldUseBatchPermit({
|
|
14398
|
-
permitType: signPermitReq.permitType,
|
|
14399
|
-
isBatchPermitAllowed: signPermitReq.isBatchPermitAllowed,
|
|
14400
|
-
tokens,
|
|
14401
|
-
oneToMany,
|
|
14402
|
-
contractVersion: signPermitReq.contractVersion,
|
|
14403
|
-
service: signPermitReq.service
|
|
14404
|
-
});
|
|
14405
|
-
if (shouldUseBatchPermit) {
|
|
14406
|
-
const resp = await _PermitTxnHandler.generateBatchPermitDataForTokens({
|
|
14407
|
-
...signPermitReq,
|
|
14408
|
-
tokens: tokens.map((token, index) => ({ ...token, index })),
|
|
14409
|
-
account: signPermitReq.sender,
|
|
14410
|
-
permitType: PermitTypes.PermitBatchWitnessTransferFrom
|
|
14411
|
-
//override because only PermitBatchWitnessTransferFrom supports batch
|
|
14412
|
-
});
|
|
14413
|
-
if (resp.status !== "success" /* success */) {
|
|
14414
|
-
return { status: resp.status, code: resp.code, permitType: PermitTypes.PermitBatchWitnessTransferFrom };
|
|
14415
|
-
}
|
|
14416
|
-
if (signPermitReq.signatureCallback) {
|
|
14417
|
-
await signPermitReq.signatureCallback({
|
|
14418
|
-
batchPermitData: resp.permitData,
|
|
14419
|
-
tokens,
|
|
14420
|
-
permitType: resp.permitType
|
|
14421
|
-
});
|
|
14422
|
-
}
|
|
14423
|
-
return {
|
|
14424
|
-
status: "success" /* success */,
|
|
14425
|
-
code: 200 /* Success */,
|
|
14426
|
-
batchPermitData: resp.permitData,
|
|
14427
|
-
permitType: resp.permitType
|
|
14428
|
-
};
|
|
14429
|
-
} else {
|
|
14430
|
-
const totalSrcAmount = calcTotalSrcTokenAmount(tokens);
|
|
14431
|
-
let firstTokenNonce = null;
|
|
14432
|
-
let permitType = _PermitTxnHandler.v1PermitSupport({ contractVersion: signPermitReq.contractVersion, service: signPermitReq.service }) ? PermitTypes.PermitSingle : signPermitReq.permitType;
|
|
14433
|
-
for (let dataIdx = 0; dataIdx < tokens.length; dataIdx++) {
|
|
14434
|
-
const isFirstToken = dataIdx === 0;
|
|
14435
|
-
const res = await _PermitTxnHandler.generatePermitDataForToken({
|
|
14436
|
-
...signPermitReq,
|
|
14437
|
-
token: {
|
|
14438
|
-
...tokens[dataIdx],
|
|
14439
|
-
index: dataIdx
|
|
14440
|
-
},
|
|
14441
|
-
firstTokenNonce: firstTokenNonce ?? void 0,
|
|
14442
|
-
oneToMany,
|
|
14443
|
-
totalSrcAmount,
|
|
14444
|
-
account: signPermitReq.sender,
|
|
14445
|
-
permitType
|
|
14446
|
-
});
|
|
14447
|
-
permitType = res.permitType;
|
|
14448
|
-
if (res.status !== "success" /* success */) {
|
|
14449
|
-
return { status: res.status, code: res.code, permitType: res.permitType };
|
|
14450
|
-
}
|
|
14451
|
-
tokens[dataIdx].permitData = res.permitData;
|
|
14452
|
-
if (isFirstToken && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14453
|
-
firstTokenNonce = res.nonce ?? null;
|
|
14454
|
-
}
|
|
14455
|
-
if (signPermitReq.signatureCallback && !isDZapNativeToken(tokens[dataIdx].address)) {
|
|
14456
|
-
const amount = oneToMany && isFirstToken ? totalSrcAmount : BigInt(tokens[dataIdx].amount);
|
|
14457
|
-
await signPermitReq.signatureCallback({
|
|
14458
|
-
permitData: res.permitData,
|
|
14459
|
-
srcToken: tokens[dataIdx].address,
|
|
14460
|
-
amount: amount.toString(),
|
|
14461
|
-
permitType: res.permitType
|
|
14462
|
-
});
|
|
14463
|
-
}
|
|
14464
|
-
}
|
|
14465
|
-
return { status: "success" /* success */, tokens, code: 200 /* Success */, permitType };
|
|
14466
|
-
}
|
|
14467
|
-
};
|
|
14468
14484
|
var PermitTxnHandler = _PermitTxnHandler;
|
|
14469
14485
|
var permit_default = PermitTxnHandler;
|
|
14470
14486
|
function encodeApproveCallData({ spender, amount }) {
|
|
@@ -15978,11 +15994,19 @@ var _DZapClient = class _DZapClient {
|
|
|
15978
15994
|
* @param params.permitType - Optional permit type (defaults to AutoPermit for optimal compatibility)
|
|
15979
15995
|
* @param params.signatureCallback - Optional callback function for each completed signature
|
|
15980
15996
|
* @param params.spender - Optional custom spender address (if not using default DZap contract)
|
|
15981
|
-
* @returns Promise resolving to
|
|
15997
|
+
* @returns Promise resolving to a {@link SignPermitResponse}, narrowed by the requested `permitType`:
|
|
15998
|
+
* - `AutoPermit` (default): `PermitSingleResponse | PermitBatchResponse | PermitErrorResponse` — the
|
|
15999
|
+
* effective mode is chosen at runtime, so narrow with `'batchPermitData' in result`.
|
|
16000
|
+
* - `PermitBatchWitnessTransferFrom`: `PermitBatchResponse | PermitErrorResponse`. If batch cannot be
|
|
16001
|
+
* fulfilled (e.g. a v1 contract or batch permits disabled) the result is an error rather than a
|
|
16002
|
+
* silent single-permit downgrade, so `batchPermitData` is guaranteed present on success.
|
|
16003
|
+
* - Any other (single) mode: `PermitSingleResponse | PermitErrorResponse`.
|
|
16004
|
+
* Always check `status === TxnStatus.success` before reading mode-specific fields.
|
|
15982
16005
|
*
|
|
15983
16006
|
* @example
|
|
15984
16007
|
* ```typescript
|
|
15985
|
-
*
|
|
16008
|
+
* // Explicit batch request -> result is typed PermitBatchResponse | PermitErrorResponse
|
|
16009
|
+
* const result = await client.sign({
|
|
15986
16010
|
* chainId: 1,
|
|
15987
16011
|
* sender: '0x...',
|
|
15988
16012
|
* tokens: [
|
|
@@ -15990,11 +16014,14 @@ var _DZapClient = class _DZapClient {
|
|
|
15990
16014
|
* ],
|
|
15991
16015
|
* service: 'swap',
|
|
15992
16016
|
* signer: walletClient,
|
|
15993
|
-
* permitType: PermitTypes.
|
|
15994
|
-
* signatureCallback: async ({ permitData, srcToken }) => {
|
|
15995
|
-
* console.log(`Signed permit for ${srcToken}:`, permitData);
|
|
15996
|
-
* }
|
|
16017
|
+
* permitType: PermitTypes.PermitBatchWitnessTransferFrom,
|
|
15997
16018
|
* });
|
|
16019
|
+
*
|
|
16020
|
+
* if (result.status === TxnStatus.success) {
|
|
16021
|
+
* console.log('batch permit:', result.batchPermitData);
|
|
16022
|
+
* } else {
|
|
16023
|
+
* console.log('batch permit could not be produced:', result.code);
|
|
16024
|
+
* }
|
|
15998
16025
|
* ```
|
|
15999
16026
|
*/
|
|
16000
16027
|
async sign(params) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dzapio/sdk",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.40",
|
|
4
4
|
"description": "A TypeScript/JavaScript SDK for interacting with the DZap protocol, providing utilities for DeFi operations including Swaps, Bridges, and Zaps.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"source": "src/index.ts",
|