@dzapio/sdk 2.0.52 → 2.0.53

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { encodeAbiParameters, parseAbiParameters, keccak256, encodePacked, stringToHex, defineChain as defineChain$1, zeroAddress as zeroAddress$1, isAddress, getAddress, formatUnits, maxUint256, encodeFunctionData, http, createPublicClient, fallback, parseEventLogs, maxUint48, getContract, decodeAbiParameters } from 'viem';
1
+ import { encodeAbiParameters, parseAbiParameters, keccak256, encodePacked, stringToHex, defineChain as defineChain$1, zeroAddress as zeroAddress$1, isAddress, getAddress, formatUnits, maxUint256, encodeFunctionData, http, createPublicClient, fallback, decodeAbiParameters, parseEventLogs, maxUint48, getContract } from 'viem';
2
2
  import Axios from 'axios';
3
3
  import { Signer, ethers } from 'ethers';
4
4
  import * as viemChains from 'viem/chains';
@@ -10968,6 +10968,17 @@ BigInt(15) / BigInt(10);
10968
10968
  var isTypeSigner = (variable) => {
10969
10969
  return variable instanceof Signer;
10970
10970
  };
10971
+ var getSignerAddress = async (signer) => {
10972
+ var _a;
10973
+ if (isTypeSigner(signer)) {
10974
+ return await signer.getAddress();
10975
+ }
10976
+ const address = (_a = signer.account) == null ? void 0 : _a.address;
10977
+ if (!address) {
10978
+ throw new Error("The provided walletClient has no account attached.");
10979
+ }
10980
+ return address;
10981
+ };
10971
10982
  var isDZapNativeToken = (srcToken) => srcToken === dZapNativeTokenFormat;
10972
10983
  var getDZapAbi = (service, version) => {
10973
10984
  switch (service) {
@@ -11033,6 +11044,14 @@ var handleViemTransactionError = ({ error }) => {
11033
11044
  status: "rejected" /* rejected */
11034
11045
  };
11035
11046
  }
11047
+ if (!Array.isArray(error == null ? void 0 : error.metaMessages) || error.metaMessages.length === 0) {
11048
+ return {
11049
+ status: "error" /* error */,
11050
+ error,
11051
+ errorMsg: (error == null ? void 0 : error.shortMessage) || (error == null ? void 0 : error.message) || "An unknown error occurred.",
11052
+ code: -500 /* ContractExecutionError */
11053
+ };
11054
+ }
11036
11055
  let errMsg = error.shortMessage;
11037
11056
  const errName = getErrorName(error.metaMessages[0]);
11038
11057
  if (errName == BRIDGE_ERRORS.BridgeCallFailed) {
@@ -11062,7 +11081,8 @@ GenericTxnHandler.sendTransaction = async ({
11062
11081
  from,
11063
11082
  to,
11064
11083
  data,
11065
- value
11084
+ value,
11085
+ gasLimit
11066
11086
  }) => {
11067
11087
  try {
11068
11088
  if (isTypeSigner(signer)) {
@@ -11071,7 +11091,8 @@ GenericTxnHandler.sendTransaction = async ({
11071
11091
  from,
11072
11092
  to,
11073
11093
  data,
11074
- value
11094
+ value,
11095
+ gasLimit: gasLimit && BigInt(gasLimit) > BigInt(0) ? BigInt(gasLimit) : void 0
11075
11096
  });
11076
11097
  return {
11077
11098
  status: "success" /* success */,
@@ -11085,7 +11106,8 @@ GenericTxnHandler.sendTransaction = async ({
11085
11106
  account: from,
11086
11107
  to,
11087
11108
  data,
11088
- value: BigInt(value)
11109
+ value: BigInt(value),
11110
+ gas: gasLimit && BigInt(gasLimit) > BigInt(0) ? BigInt(gasLimit) : void 0
11089
11111
  });
11090
11112
  return {
11091
11113
  status: "success" /* success */,
@@ -14095,113 +14117,198 @@ var trade_default = TradeTxnHandler;
14095
14117
 
14096
14118
  // src/zap/constants/step.ts
14097
14119
  var zapStepAction = {
14098
- execute: "execute"
14120
+ execute: "execute",
14121
+ approve: "approve"
14122
+ };
14123
+ var zapPreExecutionStepType = {
14124
+ sign: "sign"
14099
14125
  };
14100
14126
 
14101
- // src/transactionHandlers/zap.ts
14102
- var _ZapTxnHandler = class _ZapTxnHandler {
14127
+ // src/zap/handlers/preExecutionStepHandler.ts
14128
+ var _ZapPreExecutionStepHandler = class _ZapPreExecutionStepHandler {
14103
14129
  };
14104
- _ZapTxnHandler.execute = async ({
14105
- chainId,
14106
- txnData,
14107
- signer
14130
+ _ZapPreExecutionStepHandler.handleSignStep = async ({ step, signer, account }) => {
14131
+ const { domain, types, message, primaryType } = step.data;
14132
+ const result = await signCustomTypedData({
14133
+ signer,
14134
+ account: account ?? await getSignerAddress(signer),
14135
+ domain,
14136
+ types,
14137
+ message,
14138
+ primaryType
14139
+ });
14140
+ if (result.status !== "success" /* success */ || !result.data) {
14141
+ return result;
14142
+ }
14143
+ return {
14144
+ id: step.id,
14145
+ type: zapPreExecutionStepType.sign,
14146
+ signature: result.data.signature,
14147
+ message
14148
+ };
14149
+ };
14150
+ _ZapPreExecutionStepHandler.stepHandler = {
14151
+ [zapPreExecutionStepType.sign]: _ZapPreExecutionStepHandler.handleSignStep
14152
+ };
14153
+ _ZapPreExecutionStepHandler.handleStep = async (params) => {
14154
+ const handler = _ZapPreExecutionStepHandler.stepHandler[params.step.type];
14155
+ if (!handler) {
14156
+ throw new Error(`No handler found for pre-execution step type: ${params.step.type}`);
14157
+ }
14158
+ return handler(params);
14159
+ };
14160
+ _ZapPreExecutionStepHandler.handle = async ({
14161
+ steps,
14162
+ signer,
14163
+ account
14108
14164
  }) => {
14109
- var _a;
14165
+ if (!steps || steps.length === 0) {
14166
+ return { status: "success" /* success */, code: 200 /* Success */, preExecutionStepsData: [] };
14167
+ }
14110
14168
  try {
14111
- const { callData, callTo, value, estimatedGas } = txnData;
14112
- if (isTypeSigner(signer)) {
14113
- console.log("Using ethers signer.");
14114
- const from = await signer.getAddress();
14115
- const txnRes = await signer.sendTransaction({
14116
- from,
14117
- to: callTo,
14118
- data: callData,
14119
- value: BigInt(value),
14120
- gasLimit: BigInt(estimatedGas) ? BigInt(estimatedGas) : void 0
14121
- });
14122
- return {
14123
- status: "success" /* success */,
14124
- code: 200 /* Success */,
14125
- txnHash: txnRes.hash
14126
- };
14127
- } else {
14128
- console.log("Using viem walletClient.");
14129
- const txnHash = await signer.sendTransaction({
14130
- chain: viemChainsById[chainId],
14131
- account: (_a = signer.account) == null ? void 0 : _a.address,
14132
- to: txnData.callTo,
14133
- data: txnData.callData,
14134
- value: BigInt(txnData.value)
14135
- });
14136
- return {
14137
- status: "success" /* success */,
14138
- code: 200 /* Success */,
14139
- txnHash
14140
- };
14169
+ const preExecutionStepsData = [];
14170
+ for (const step of steps) {
14171
+ const result = await _ZapPreExecutionStepHandler.handleStep({ step, signer, account });
14172
+ if (!("id" in result)) {
14173
+ return result;
14174
+ }
14175
+ preExecutionStepsData.push(result);
14141
14176
  }
14177
+ return { status: "success" /* success */, code: 200 /* Success */, preExecutionStepsData };
14142
14178
  } catch (error) {
14143
14179
  console.log({ error });
14144
14180
  return handleViemTransactionError({ error });
14145
14181
  }
14146
14182
  };
14147
- _ZapTxnHandler.approve = async ({ chainId, data, signer }) => {
14148
- var _a;
14183
+ var ZapPreExecutionStepHandler = _ZapPreExecutionStepHandler;
14184
+ var preExecutionStepHandler_default = ZapPreExecutionStepHandler;
14185
+
14186
+ // src/zap/handlers/txnStepsHandler.ts
14187
+ var _ZapTxnStepsHandler = class _ZapTxnStepsHandler {
14188
+ };
14189
+ _ZapTxnStepsHandler.sendTxnStep = async ({
14190
+ chainId,
14191
+ txnData,
14192
+ signer
14193
+ }) => {
14194
+ if (txnData.type !== chainTypes.evm) {
14195
+ throw new Error(`Zap steps on ${txnData.type} chains cannot be executed with an EVM signer.`);
14196
+ }
14197
+ const from = await getSignerAddress(signer);
14198
+ return await generic_default.sendTransaction({
14199
+ chainId,
14200
+ signer,
14201
+ from,
14202
+ to: txnData.callTo,
14203
+ data: txnData.callData,
14204
+ value: txnData.value,
14205
+ gasLimit: txnData.estimatedGas
14206
+ });
14207
+ };
14208
+ _ZapTxnStepsHandler.handleExecuteStep = async ({
14209
+ chainId,
14210
+ txnData,
14211
+ signer
14212
+ }) => {
14213
+ return await _ZapTxnStepsHandler.sendTxnStep({ chainId, txnData, signer });
14214
+ };
14215
+ _ZapTxnStepsHandler.handleApproveStep = async ({
14216
+ chainId,
14217
+ txnData,
14218
+ signer,
14219
+ rpcUrls
14220
+ }) => {
14221
+ const result = await _ZapTxnStepsHandler.sendTxnStep({ chainId, txnData, signer });
14222
+ if (result.status !== "success" /* success */) {
14223
+ return result;
14224
+ }
14225
+ await getPublicClient({ chainId, rpcUrls }).waitForTransactionReceipt({ hash: result.txnHash });
14226
+ return result;
14227
+ };
14228
+ _ZapTxnStepsHandler.txnStepHandlers = {
14229
+ [zapStepAction.approve]: ({ chainId, txnData, signer, rpcUrls }) => _ZapTxnStepsHandler.handleApproveStep({ chainId, txnData, signer, rpcUrls }),
14230
+ [zapStepAction.execute]: ({ chainId, txnData, signer }) => _ZapTxnStepsHandler.handleExecuteStep({ chainId, txnData, signer })
14231
+ };
14232
+ _ZapTxnStepsHandler.processTxnStep = async ({
14233
+ step,
14234
+ ...params
14235
+ }) => {
14236
+ const handler = _ZapTxnStepsHandler.txnStepHandlers[step.action];
14237
+ if (!handler) {
14238
+ return {
14239
+ status: "error" /* error */,
14240
+ code: 32771 /* FunctionNotFound */,
14241
+ errorMsg: `Unsupported zap step action: ${String(step.action)}`
14242
+ };
14243
+ }
14244
+ return handler({ ...params, txnData: step.data });
14245
+ };
14246
+ _ZapTxnStepsHandler.handle = async ({
14247
+ chainId,
14248
+ steps,
14249
+ signer,
14250
+ rpcUrls
14251
+ }) => {
14149
14252
  try {
14150
- const { callData, callTo, value, estimatedGas } = data;
14151
- const publicClient = getPublicClient({ chainId, rpcUrls: void 0 });
14152
- const blockNumber = await publicClient.getBlockNumber();
14153
- console.log("block Number and data");
14154
- console.dir(
14155
- {
14156
- blockNumber,
14157
- ...data
14158
- },
14159
- { depth: null }
14160
- );
14161
- if (isTypeSigner(signer)) {
14162
- console.log("Using ethers signer.");
14163
- const from = await signer.getAddress();
14164
- const txnRes = await signer.sendTransaction({
14165
- from,
14166
- to: callTo,
14167
- data: callData,
14168
- value: BigInt(value),
14169
- gasLimit: BigInt(estimatedGas) ? BigInt(estimatedGas) : void 0
14170
- });
14171
- return {
14172
- status: "success" /* success */,
14173
- code: 200 /* Success */,
14174
- txnHash: txnRes.hash
14175
- };
14176
- } else {
14177
- console.log("Using viem walletClient.");
14178
- const txnHash = await signer.sendTransaction({
14179
- chain: viemChainsById[chainId],
14180
- account: (_a = signer.account) == null ? void 0 : _a.address,
14181
- to: data.callTo,
14182
- data: data.callData,
14183
- value: BigInt(data.value)
14184
- });
14253
+ let txnHash;
14254
+ for (const step of steps) {
14255
+ const result = await _ZapTxnStepsHandler.processTxnStep({ step, chainId, signer, rpcUrls });
14256
+ if (result.status !== "success" /* success */) {
14257
+ return result;
14258
+ }
14259
+ if (step.action !== zapStepAction.approve) {
14260
+ txnHash = result.txnHash;
14261
+ }
14262
+ }
14263
+ if (!txnHash) {
14185
14264
  return {
14186
- status: "success" /* success */,
14187
- code: 200 /* Success */,
14188
- txnHash
14265
+ status: "error" /* error */,
14266
+ code: 500 /* Error */,
14267
+ errorMsg: "No executable steps found."
14189
14268
  };
14190
14269
  }
14270
+ return { status: "success" /* success */, code: 200 /* Success */, txnHash };
14191
14271
  } catch (error) {
14192
14272
  console.log({ error });
14193
14273
  return handleViemTransactionError({ error });
14194
14274
  }
14195
14275
  };
14196
- _ZapTxnHandler.zap = async ({
14276
+ var ZapTxnStepsHandler = _ZapTxnStepsHandler;
14277
+ var txnStepsHandler_default = ZapTxnStepsHandler;
14278
+
14279
+ // src/transactionHandlers/zap.ts
14280
+ var ZapTxnHandler = class {
14281
+ };
14282
+ /**
14283
+ * Runs a zap end to end: pre-execution steps, then buildTx, then the steps it returns.
14284
+ *
14285
+ * `preExecutionSteps` come from a quote. Their signatures are sent with the buildTx request as
14286
+ * `preExecutionStepsData`, which some routes (Aave borrows, for instance) require before
14287
+ * they can be built at all — so they are handled before anything is built, and skipping them is
14288
+ * only safe when the quote didn't ask for any.
14289
+ *
14290
+ * Passing `steps` skips the build and runs them directly; in that case any pre-execution steps
14291
+ * must already have been folded into the request that produced them.
14292
+ */
14293
+ ZapTxnHandler.zap = async ({
14197
14294
  request,
14198
14295
  steps,
14199
- signer
14296
+ preExecutionSteps,
14297
+ signer,
14298
+ rpcUrls
14200
14299
  }) => {
14201
14300
  try {
14202
14301
  const chainId = "srcChainId" in request ? request.srcChainId : request.actions[0].srcChainId;
14302
+ let buildRequest = request;
14303
+ if (preExecutionSteps == null ? void 0 : preExecutionSteps.length) {
14304
+ const preExecutionResult = await preExecutionStepHandler_default.handle({ steps: preExecutionSteps, signer, account: request.account });
14305
+ if (preExecutionResult.status !== "success" /* success */ || !("preExecutionStepsData" in preExecutionResult)) {
14306
+ return preExecutionResult;
14307
+ }
14308
+ buildRequest = { ...request, preExecutionStepsData: preExecutionResult.preExecutionStepsData };
14309
+ }
14203
14310
  if (!steps || steps.length === 0) {
14204
- const route = "actions" in request ? (await fetchZapBundleBuildTx(request)).data : (await fetchZapBuildTxnData(request)).data;
14311
+ const route = "actions" in buildRequest ? (await fetchZapBundleBuildTx(buildRequest)).data : (await fetchZapBuildTxnData(buildRequest)).data;
14205
14312
  steps = route.steps;
14206
14313
  if (!steps || steps.length === 0) {
14207
14314
  return {
@@ -14211,35 +14318,12 @@ _ZapTxnHandler.zap = async ({
14211
14318
  };
14212
14319
  }
14213
14320
  }
14214
- let txnHash;
14215
- for (let i = 0; i < steps.length; i++) {
14216
- const step = steps[i];
14217
- if (step.action === zapStepAction.execute) {
14218
- const result = await _ZapTxnHandler.execute({ chainId, txnData: step.data, signer });
14219
- if (result.status !== "success" /* success */) {
14220
- return result;
14221
- }
14222
- txnHash = result.txnHash;
14223
- }
14224
- }
14225
- if (!txnHash) {
14226
- return {
14227
- status: "error" /* error */,
14228
- code: 500 /* Error */,
14229
- errorMsg: "No executable steps found."
14230
- };
14231
- }
14232
- return {
14233
- status: "success" /* success */,
14234
- code: 200 /* Success */,
14235
- txnHash
14236
- };
14321
+ return await txnStepsHandler_default.handle({ chainId, steps, signer, rpcUrls });
14237
14322
  } catch (error) {
14238
14323
  console.log({ error });
14239
14324
  return handleViemTransactionError({ error });
14240
14325
  }
14241
14326
  };
14242
- var ZapTxnHandler = _ZapTxnHandler;
14243
14327
  var zap_default = ZapTxnHandler;
14244
14328
  var SUI_DEFAULT_RPC = "https://sui-rpc.publicnode.com";
14245
14329
  var decodeSuivmTransaction = async ({ txHash, rpcUrls }) => {
@@ -16260,6 +16344,9 @@ var _DZapClient = class _DZapClient {
16260
16344
  * @param params.request - The zap build request containing operation parameters
16261
16345
  * @param params.signer - The wallet signer to sign and execute the transaction
16262
16346
  * @param params.steps - Optional array of pre-built transaction steps (if not provided, will build from request)
16347
+ * @param params.preExecutionSteps - Optional steps from a quote that must be signed before the route can be built.
16348
+ * Their signatures are sent with the buildTx request; routes that ask for them cannot be built without them.
16349
+ * @param params.rpcUrls - Optional custom RPC URLs for blockchain interactions
16263
16350
  * @returns Promise resolving to zap transaction execution result
16264
16351
  *
16265
16352
  * @example
@@ -16288,14 +16375,51 @@ var _DZapClient = class _DZapClient {
16288
16375
  async zap({
16289
16376
  request,
16290
16377
  steps,
16291
- signer
16378
+ preExecutionSteps,
16379
+ signer,
16380
+ rpcUrls
16292
16381
  }) {
16382
+ const chainId = "srcChainId" in request ? request.srcChainId : request.actions[0].srcChainId;
16293
16383
  return await zap_default.zap({
16294
16384
  request,
16295
16385
  steps,
16296
- signer
16386
+ preExecutionSteps,
16387
+ signer,
16388
+ rpcUrls: rpcUrls || config.getRpcUrlsByChainId(chainId)
16297
16389
  });
16298
16390
  }
16391
+ /**
16392
+ * Signs the pre-execution steps a zap quote asked for and returns the data to send with the
16393
+ * following buildTx request.
16394
+ *
16395
+ * Some routes (Aave borrows, for instance) cannot be built until the account has signed a
16396
+ * typed-data payload the quote hands back — the signature is embedded in the route itself, so it
16397
+ * has to exist before the route does. Building without it fails server-side.
16398
+ *
16399
+ * @param params.preExecutionSteps - The `preExecutionSteps` from a quote response
16400
+ * @param params.signer - The wallet signer asked to sign each step
16401
+ * @param params.account - Signing account; resolved from the signer when omitted
16402
+ * @returns On success, `preExecutionStepsData` to pass on the buildTx request; otherwise the failure
16403
+ * (including `status: rejected` when the user cancels the signature)
16404
+ *
16405
+ * @example
16406
+ * ```typescript
16407
+ * const quote = await client.getZapBundleQuote(request);
16408
+ * const result = await client.prepareZapPreExecutionData({
16409
+ * preExecutionSteps: quote.preExecutionSteps,
16410
+ * signer: walletClient,
16411
+ * });
16412
+ * if (result.status !== TxnStatus.success || !('preExecutionStepsData' in result)) return result;
16413
+ * const route = await client.buildZapBundleTx({ ...request, preExecutionStepsData: result.preExecutionStepsData });
16414
+ * ```
16415
+ */
16416
+ async prepareZapPreExecutionData({
16417
+ preExecutionSteps,
16418
+ signer,
16419
+ account
16420
+ }) {
16421
+ return await preExecutionStepHandler_default.handle({ steps: preExecutionSteps, signer, account });
16422
+ }
16299
16423
  /**
16300
16424
  * Builds comprehensive transaction data for zap operations without executing them.
16301
16425
  * This method prepares all necessary steps, routing information, and transaction parameters
@@ -17184,10 +17308,13 @@ var zapPathAction = {
17184
17308
  bridge: "bridge",
17185
17309
  deposit: "deposit",
17186
17310
  withdraw: "withdraw",
17311
+ borrow: "borrow",
17187
17312
  stake: "stake",
17188
17313
  unstake: "unstake",
17189
17314
  increase: "increase",
17190
- harvest: "harvest"
17315
+ harvest: "harvest",
17316
+ long: "long",
17317
+ short: "short"
17191
17318
  };
17192
17319
 
17193
17320
  // src/zap/constants/fee.ts
@@ -17216,4 +17343,4 @@ var zapRefundType = {
17216
17343
  rent: "rent"
17217
17344
  };
17218
17345
 
17219
- export { AllowanceTypes, ApprovalModes, dZapClient_default as DZapClient, DZapPermitMode, OtherAbis, PermitTypes, QuoteFilters, STATUS, Services, SignatureExpiryInSecs, StatusCodes, TxnStatus, arcTestnet, arthera, astralisTestnet, bitcoinTestnet, bitlayer, checkEIP2612PermitSupport, citrea, classifyAddress, contractErrorActions, customViemChains, erc20Functions, extendViemChain, fiveIre, formatToken, getTokensPairKey, hemi, hyperEvm, hyperliquid, katana, merlin, patchSwapAmountsFromTx, pushTestnet, robinhood, stableChain, tempo, viemChainsById, zapFeeType, zapPathAction, zapRefundType, zapStepAction };
17346
+ export { AllowanceTypes, ApprovalModes, dZapClient_default as DZapClient, DZapPermitMode, OtherAbis, PermitTypes, QuoteFilters, STATUS, Services, SignatureExpiryInSecs, StatusCodes, TxnStatus, arcTestnet, arthera, astralisTestnet, bitcoinTestnet, bitlayer, checkEIP2612PermitSupport, citrea, classifyAddress, contractErrorActions, customViemChains, erc20Functions, extendViemChain, fiveIre, formatToken, getTokensPairKey, hemi, hyperEvm, hyperliquid, katana, merlin, patchSwapAmountsFromTx, pushTestnet, robinhood, stableChain, tempo, viemChainsById, zapFeeType, zapPathAction, zapPreExecutionStepType, zapRefundType, zapStepAction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzapio/sdk",
3
- "version": "2.0.52",
3
+ "version": "2.0.53",
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",