@dzapio/sdk 2.0.52 → 2.0.54

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
@@ -10993,6 +10993,17 @@ BigInt(15) / BigInt(10);
10993
10993
  var isTypeSigner = (variable) => {
10994
10994
  return variable instanceof ethers.Signer;
10995
10995
  };
10996
+ var getSignerAddress = async (signer) => {
10997
+ var _a;
10998
+ if (isTypeSigner(signer)) {
10999
+ return await signer.getAddress();
11000
+ }
11001
+ const address = (_a = signer.account) == null ? void 0 : _a.address;
11002
+ if (!address) {
11003
+ throw new Error("The provided walletClient has no account attached.");
11004
+ }
11005
+ return address;
11006
+ };
10996
11007
  var isDZapNativeToken = (srcToken) => srcToken === dZapNativeTokenFormat;
10997
11008
  var getDZapAbi = (service, version) => {
10998
11009
  switch (service) {
@@ -11058,6 +11069,14 @@ var handleViemTransactionError = ({ error }) => {
11058
11069
  status: "rejected" /* rejected */
11059
11070
  };
11060
11071
  }
11072
+ if (!Array.isArray(error == null ? void 0 : error.metaMessages) || error.metaMessages.length === 0) {
11073
+ return {
11074
+ status: "error" /* error */,
11075
+ error,
11076
+ errorMsg: (error == null ? void 0 : error.shortMessage) || (error == null ? void 0 : error.message) || "An unknown error occurred.",
11077
+ code: -500 /* ContractExecutionError */
11078
+ };
11079
+ }
11061
11080
  let errMsg = error.shortMessage;
11062
11081
  const errName = getErrorName(error.metaMessages[0]);
11063
11082
  if (errName == BRIDGE_ERRORS.BridgeCallFailed) {
@@ -11087,7 +11106,8 @@ GenericTxnHandler.sendTransaction = async ({
11087
11106
  from,
11088
11107
  to,
11089
11108
  data,
11090
- value
11109
+ value,
11110
+ gasLimit
11091
11111
  }) => {
11092
11112
  try {
11093
11113
  if (isTypeSigner(signer)) {
@@ -11096,7 +11116,8 @@ GenericTxnHandler.sendTransaction = async ({
11096
11116
  from,
11097
11117
  to,
11098
11118
  data,
11099
- value
11119
+ value,
11120
+ gasLimit: gasLimit && BigInt(gasLimit) > BigInt(0) ? BigInt(gasLimit) : void 0
11100
11121
  });
11101
11122
  return {
11102
11123
  status: "success" /* success */,
@@ -11110,7 +11131,8 @@ GenericTxnHandler.sendTransaction = async ({
11110
11131
  account: from,
11111
11132
  to,
11112
11133
  data,
11113
- value: BigInt(value)
11134
+ value: BigInt(value),
11135
+ gas: gasLimit && BigInt(gasLimit) > BigInt(0) ? BigInt(gasLimit) : void 0
11114
11136
  });
11115
11137
  return {
11116
11138
  status: "success" /* success */,
@@ -14120,113 +14142,198 @@ var trade_default = TradeTxnHandler;
14120
14142
 
14121
14143
  // src/zap/constants/step.ts
14122
14144
  var zapStepAction = {
14123
- execute: "execute"
14145
+ execute: "execute",
14146
+ approve: "approve"
14147
+ };
14148
+ var zapPreExecutionStepType = {
14149
+ sign: "sign"
14124
14150
  };
14125
14151
 
14126
- // src/transactionHandlers/zap.ts
14127
- var _ZapTxnHandler = class _ZapTxnHandler {
14152
+ // src/zap/handlers/preExecutionStepHandler.ts
14153
+ var _ZapPreExecutionStepHandler = class _ZapPreExecutionStepHandler {
14128
14154
  };
14129
- _ZapTxnHandler.execute = async ({
14130
- chainId,
14131
- txnData,
14132
- signer
14155
+ _ZapPreExecutionStepHandler.handleSignStep = async ({ step, signer, account }) => {
14156
+ const { domain, types, message, primaryType } = step.data;
14157
+ const result = await signCustomTypedData({
14158
+ signer,
14159
+ account: account ?? await getSignerAddress(signer),
14160
+ domain,
14161
+ types,
14162
+ message,
14163
+ primaryType
14164
+ });
14165
+ if (result.status !== "success" /* success */) {
14166
+ return result;
14167
+ }
14168
+ return {
14169
+ id: step.id,
14170
+ type: zapPreExecutionStepType.sign,
14171
+ signature: result.data.signature,
14172
+ message
14173
+ };
14174
+ };
14175
+ _ZapPreExecutionStepHandler.stepHandler = {
14176
+ [zapPreExecutionStepType.sign]: _ZapPreExecutionStepHandler.handleSignStep
14177
+ };
14178
+ _ZapPreExecutionStepHandler.handleStep = async (params) => {
14179
+ const handler = _ZapPreExecutionStepHandler.stepHandler[params.step.type];
14180
+ if (!handler) {
14181
+ throw new Error(`No handler found for pre-execution step type: ${params.step.type}`);
14182
+ }
14183
+ return handler(params);
14184
+ };
14185
+ _ZapPreExecutionStepHandler.handle = async ({
14186
+ steps,
14187
+ signer,
14188
+ account
14133
14189
  }) => {
14134
- var _a;
14190
+ if (!steps || steps.length === 0) {
14191
+ return { status: "success" /* success */, code: 200 /* Success */, preExecutionStepsData: [] };
14192
+ }
14135
14193
  try {
14136
- const { callData, callTo, value, estimatedGas } = txnData;
14137
- if (isTypeSigner(signer)) {
14138
- console.log("Using ethers signer.");
14139
- const from = await signer.getAddress();
14140
- const txnRes = await signer.sendTransaction({
14141
- from,
14142
- to: callTo,
14143
- data: callData,
14144
- value: BigInt(value),
14145
- gasLimit: BigInt(estimatedGas) ? BigInt(estimatedGas) : void 0
14146
- });
14147
- return {
14148
- status: "success" /* success */,
14149
- code: 200 /* Success */,
14150
- txnHash: txnRes.hash
14151
- };
14152
- } else {
14153
- console.log("Using viem walletClient.");
14154
- const txnHash = await signer.sendTransaction({
14155
- chain: viemChainsById[chainId],
14156
- account: (_a = signer.account) == null ? void 0 : _a.address,
14157
- to: txnData.callTo,
14158
- data: txnData.callData,
14159
- value: BigInt(txnData.value)
14160
- });
14161
- return {
14162
- status: "success" /* success */,
14163
- code: 200 /* Success */,
14164
- txnHash
14165
- };
14194
+ const preExecutionStepsData = [];
14195
+ for (const step of steps) {
14196
+ const result = await _ZapPreExecutionStepHandler.handleStep({ step, signer, account });
14197
+ if (!("id" in result)) {
14198
+ return result;
14199
+ }
14200
+ preExecutionStepsData.push(result);
14166
14201
  }
14202
+ return { status: "success" /* success */, code: 200 /* Success */, preExecutionStepsData };
14167
14203
  } catch (error) {
14168
14204
  console.log({ error });
14169
14205
  return handleViemTransactionError({ error });
14170
14206
  }
14171
14207
  };
14172
- _ZapTxnHandler.approve = async ({ chainId, data, signer }) => {
14173
- var _a;
14208
+ var ZapPreExecutionStepHandler = _ZapPreExecutionStepHandler;
14209
+ var preExecutionStepHandler_default = ZapPreExecutionStepHandler;
14210
+
14211
+ // src/zap/handlers/txnStepsHandler.ts
14212
+ var _ZapTxnStepsHandler = class _ZapTxnStepsHandler {
14213
+ };
14214
+ _ZapTxnStepsHandler.sendTxnStep = async ({
14215
+ chainId,
14216
+ txnData,
14217
+ signer
14218
+ }) => {
14219
+ if (txnData.type !== chainTypes.evm) {
14220
+ throw new Error(`Zap steps on ${txnData.type} chains cannot be executed with an EVM signer.`);
14221
+ }
14222
+ const from = await getSignerAddress(signer);
14223
+ return await generic_default.sendTransaction({
14224
+ chainId,
14225
+ signer,
14226
+ from,
14227
+ to: txnData.callTo,
14228
+ data: txnData.callData,
14229
+ value: txnData.value,
14230
+ gasLimit: txnData.estimatedGas
14231
+ });
14232
+ };
14233
+ _ZapTxnStepsHandler.handleExecuteStep = async ({
14234
+ chainId,
14235
+ txnData,
14236
+ signer
14237
+ }) => {
14238
+ return await _ZapTxnStepsHandler.sendTxnStep({ chainId, txnData, signer });
14239
+ };
14240
+ _ZapTxnStepsHandler.handleApproveStep = async ({
14241
+ chainId,
14242
+ txnData,
14243
+ signer,
14244
+ rpcUrls
14245
+ }) => {
14246
+ const result = await _ZapTxnStepsHandler.sendTxnStep({ chainId, txnData, signer });
14247
+ if (result.status !== "success" /* success */) {
14248
+ return result;
14249
+ }
14250
+ await getPublicClient({ chainId, rpcUrls }).waitForTransactionReceipt({ hash: result.txnHash });
14251
+ return result;
14252
+ };
14253
+ _ZapTxnStepsHandler.txnStepHandlers = {
14254
+ [zapStepAction.approve]: ({ chainId, txnData, signer, rpcUrls }) => _ZapTxnStepsHandler.handleApproveStep({ chainId, txnData, signer, rpcUrls }),
14255
+ [zapStepAction.execute]: ({ chainId, txnData, signer }) => _ZapTxnStepsHandler.handleExecuteStep({ chainId, txnData, signer })
14256
+ };
14257
+ _ZapTxnStepsHandler.processTxnStep = async ({
14258
+ step,
14259
+ ...params
14260
+ }) => {
14261
+ const handler = _ZapTxnStepsHandler.txnStepHandlers[step.action];
14262
+ if (!handler) {
14263
+ return {
14264
+ status: "error" /* error */,
14265
+ code: 32771 /* FunctionNotFound */,
14266
+ errorMsg: `Unsupported zap step action: ${String(step.action)}`
14267
+ };
14268
+ }
14269
+ return handler({ ...params, txnData: step.data });
14270
+ };
14271
+ _ZapTxnStepsHandler.handle = async ({
14272
+ chainId,
14273
+ steps,
14274
+ signer,
14275
+ rpcUrls
14276
+ }) => {
14174
14277
  try {
14175
- const { callData, callTo, value, estimatedGas } = data;
14176
- const publicClient = getPublicClient({ chainId, rpcUrls: void 0 });
14177
- const blockNumber = await publicClient.getBlockNumber();
14178
- console.log("block Number and data");
14179
- console.dir(
14180
- {
14181
- blockNumber,
14182
- ...data
14183
- },
14184
- { depth: null }
14185
- );
14186
- if (isTypeSigner(signer)) {
14187
- console.log("Using ethers signer.");
14188
- const from = await signer.getAddress();
14189
- const txnRes = await signer.sendTransaction({
14190
- from,
14191
- to: callTo,
14192
- data: callData,
14193
- value: BigInt(value),
14194
- gasLimit: BigInt(estimatedGas) ? BigInt(estimatedGas) : void 0
14195
- });
14196
- return {
14197
- status: "success" /* success */,
14198
- code: 200 /* Success */,
14199
- txnHash: txnRes.hash
14200
- };
14201
- } else {
14202
- console.log("Using viem walletClient.");
14203
- const txnHash = await signer.sendTransaction({
14204
- chain: viemChainsById[chainId],
14205
- account: (_a = signer.account) == null ? void 0 : _a.address,
14206
- to: data.callTo,
14207
- data: data.callData,
14208
- value: BigInt(data.value)
14209
- });
14278
+ let txnHash;
14279
+ for (const step of steps) {
14280
+ const result = await _ZapTxnStepsHandler.processTxnStep({ step, chainId, signer, rpcUrls });
14281
+ if (result.status !== "success" /* success */) {
14282
+ return result;
14283
+ }
14284
+ if (step.action !== zapStepAction.approve) {
14285
+ txnHash = result.txnHash;
14286
+ }
14287
+ }
14288
+ if (!txnHash) {
14210
14289
  return {
14211
- status: "success" /* success */,
14212
- code: 200 /* Success */,
14213
- txnHash
14290
+ status: "error" /* error */,
14291
+ code: 500 /* Error */,
14292
+ errorMsg: "No executable steps found."
14214
14293
  };
14215
14294
  }
14295
+ return { status: "success" /* success */, code: 200 /* Success */, txnHash };
14216
14296
  } catch (error) {
14217
14297
  console.log({ error });
14218
14298
  return handleViemTransactionError({ error });
14219
14299
  }
14220
14300
  };
14221
- _ZapTxnHandler.zap = async ({
14301
+ var ZapTxnStepsHandler = _ZapTxnStepsHandler;
14302
+ var txnStepsHandler_default = ZapTxnStepsHandler;
14303
+
14304
+ // src/transactionHandlers/zap.ts
14305
+ var ZapTxnHandler = class {
14306
+ };
14307
+ /**
14308
+ * Runs a zap end to end: pre-execution steps, then buildTx, then the steps it returns.
14309
+ *
14310
+ * `preExecutionSteps` come from a quote. Their signatures are sent with the buildTx request as
14311
+ * `preExecutionStepsData`, which some routes (Aave borrows, for instance) require before
14312
+ * they can be built at all — so they are handled before anything is built, and skipping them is
14313
+ * only safe when the quote didn't ask for any.
14314
+ *
14315
+ * Passing `steps` skips the build and runs them directly; in that case any pre-execution steps
14316
+ * must already have been folded into the request that produced them.
14317
+ */
14318
+ ZapTxnHandler.zap = async ({
14222
14319
  request,
14223
14320
  steps,
14224
- signer
14321
+ preExecutionSteps,
14322
+ signer,
14323
+ rpcUrls
14225
14324
  }) => {
14226
14325
  try {
14227
14326
  const chainId = "srcChainId" in request ? request.srcChainId : request.actions[0].srcChainId;
14327
+ let buildRequest = request;
14328
+ if (preExecutionSteps == null ? void 0 : preExecutionSteps.length) {
14329
+ const preExecutionResult = await preExecutionStepHandler_default.handle({ steps: preExecutionSteps, signer, account: request.account });
14330
+ if (preExecutionResult.status !== "success" /* success */ || !("preExecutionStepsData" in preExecutionResult)) {
14331
+ return preExecutionResult;
14332
+ }
14333
+ buildRequest = { ...request, preExecutionStepsData: preExecutionResult.preExecutionStepsData };
14334
+ }
14228
14335
  if (!steps || steps.length === 0) {
14229
- const route = "actions" in request ? (await fetchZapBundleBuildTx(request)).data : (await fetchZapBuildTxnData(request)).data;
14336
+ const route = "actions" in buildRequest ? (await fetchZapBundleBuildTx(buildRequest)).data : (await fetchZapBuildTxnData(buildRequest)).data;
14230
14337
  steps = route.steps;
14231
14338
  if (!steps || steps.length === 0) {
14232
14339
  return {
@@ -14236,35 +14343,12 @@ _ZapTxnHandler.zap = async ({
14236
14343
  };
14237
14344
  }
14238
14345
  }
14239
- let txnHash;
14240
- for (let i = 0; i < steps.length; i++) {
14241
- const step = steps[i];
14242
- if (step.action === zapStepAction.execute) {
14243
- const result = await _ZapTxnHandler.execute({ chainId, txnData: step.data, signer });
14244
- if (result.status !== "success" /* success */) {
14245
- return result;
14246
- }
14247
- txnHash = result.txnHash;
14248
- }
14249
- }
14250
- if (!txnHash) {
14251
- return {
14252
- status: "error" /* error */,
14253
- code: 500 /* Error */,
14254
- errorMsg: "No executable steps found."
14255
- };
14256
- }
14257
- return {
14258
- status: "success" /* success */,
14259
- code: 200 /* Success */,
14260
- txnHash
14261
- };
14346
+ return await txnStepsHandler_default.handle({ chainId, steps, signer, rpcUrls });
14262
14347
  } catch (error) {
14263
14348
  console.log({ error });
14264
14349
  return handleViemTransactionError({ error });
14265
14350
  }
14266
14351
  };
14267
- var ZapTxnHandler = _ZapTxnHandler;
14268
14352
  var zap_default = ZapTxnHandler;
14269
14353
  var SUI_DEFAULT_RPC = "https://sui-rpc.publicnode.com";
14270
14354
  var decodeSuivmTransaction = async ({ txHash, rpcUrls }) => {
@@ -16285,6 +16369,9 @@ var _DZapClient = class _DZapClient {
16285
16369
  * @param params.request - The zap build request containing operation parameters
16286
16370
  * @param params.signer - The wallet signer to sign and execute the transaction
16287
16371
  * @param params.steps - Optional array of pre-built transaction steps (if not provided, will build from request)
16372
+ * @param params.preExecutionSteps - Optional steps from a quote that must be signed before the route can be built.
16373
+ * Their signatures are sent with the buildTx request; routes that ask for them cannot be built without them.
16374
+ * @param params.rpcUrls - Optional custom RPC URLs for blockchain interactions
16288
16375
  * @returns Promise resolving to zap transaction execution result
16289
16376
  *
16290
16377
  * @example
@@ -16313,14 +16400,51 @@ var _DZapClient = class _DZapClient {
16313
16400
  async zap({
16314
16401
  request,
16315
16402
  steps,
16316
- signer
16403
+ preExecutionSteps,
16404
+ signer,
16405
+ rpcUrls
16317
16406
  }) {
16407
+ const chainId = "srcChainId" in request ? request.srcChainId : request.actions[0].srcChainId;
16318
16408
  return await zap_default.zap({
16319
16409
  request,
16320
16410
  steps,
16321
- signer
16411
+ preExecutionSteps,
16412
+ signer,
16413
+ rpcUrls: rpcUrls || config.getRpcUrlsByChainId(chainId)
16322
16414
  });
16323
16415
  }
16416
+ /**
16417
+ * Signs the pre-execution steps a zap quote asked for and returns the data to send with the
16418
+ * following buildTx request.
16419
+ *
16420
+ * Some routes (Aave borrows, for instance) cannot be built until the account has signed a
16421
+ * typed-data payload the quote hands back — the signature is embedded in the route itself, so it
16422
+ * has to exist before the route does. Building without it fails server-side.
16423
+ *
16424
+ * @param params.preExecutionSteps - The `preExecutionSteps` from a quote response
16425
+ * @param params.signer - The wallet signer asked to sign each step
16426
+ * @param params.account - Signing account; resolved from the signer when omitted
16427
+ * @returns On success, `preExecutionStepsData` to pass on the buildTx request; otherwise the failure
16428
+ * (including `status: rejected` when the user cancels the signature)
16429
+ *
16430
+ * @example
16431
+ * ```typescript
16432
+ * const quote = await client.getZapBundleQuote(request);
16433
+ * const result = await client.prepareZapPreExecutionData({
16434
+ * preExecutionSteps: quote.preExecutionSteps,
16435
+ * signer: walletClient,
16436
+ * });
16437
+ * if (result.status !== TxnStatus.success || !('preExecutionStepsData' in result)) return result;
16438
+ * const route = await client.buildZapBundleTx({ ...request, preExecutionStepsData: result.preExecutionStepsData });
16439
+ * ```
16440
+ */
16441
+ async prepareZapPreExecutionData({
16442
+ preExecutionSteps,
16443
+ signer,
16444
+ account
16445
+ }) {
16446
+ return await preExecutionStepHandler_default.handle({ steps: preExecutionSteps, signer, account });
16447
+ }
16324
16448
  /**
16325
16449
  * Builds comprehensive transaction data for zap operations without executing them.
16326
16450
  * This method prepares all necessary steps, routing information, and transaction parameters
@@ -17209,10 +17333,13 @@ var zapPathAction = {
17209
17333
  bridge: "bridge",
17210
17334
  deposit: "deposit",
17211
17335
  withdraw: "withdraw",
17336
+ borrow: "borrow",
17212
17337
  stake: "stake",
17213
17338
  unstake: "unstake",
17214
17339
  increase: "increase",
17215
- harvest: "harvest"
17340
+ harvest: "harvest",
17341
+ long: "long",
17342
+ short: "short"
17216
17343
  };
17217
17344
 
17218
17345
  // src/zap/constants/fee.ts
@@ -17285,5 +17412,6 @@ exports.tempo = tempo;
17285
17412
  exports.viemChainsById = viemChainsById;
17286
17413
  exports.zapFeeType = zapFeeType;
17287
17414
  exports.zapPathAction = zapPathAction;
17415
+ exports.zapPreExecutionStepType = zapPreExecutionStepType;
17288
17416
  exports.zapRefundType = zapRefundType;
17289
17417
  exports.zapStepAction = zapStepAction;