@matterlabs/zksync-js 0.0.17 → 0.0.18

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.
@@ -1,4 +1,4 @@
1
- export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs } from '../../chunk-ONCNOWNC.js';
1
+ export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs } from '../../chunk-GBS7KQFU.js';
2
2
  import '../../chunk-6LYAENO6.js';
3
3
  import '../../chunk-3HHUZXSV.js';
4
4
  export { createViemClient as createClient, createViemClient } from '../../chunk-CK5UFAZK.js';
@@ -0,0 +1,11 @@
1
+ import type { Address } from '../../../../../core/types/primitives';
2
+ import type { BuildCtx } from '../context';
3
+ import type { ViemPlanWriteRequest } from './types';
4
+ type BuildApprovalRequestInput = {
5
+ ctx: BuildCtx;
6
+ token: Address;
7
+ spender: Address;
8
+ amount: bigint;
9
+ };
10
+ export declare function buildApprovalRequest({ ctx, token, spender, amount, }: BuildApprovalRequestInput): Promise<ViemPlanWriteRequest>;
11
+ export {};
@@ -6729,8 +6729,73 @@ function routeEthDirect() {
6729
6729
  }
6730
6730
  };
6731
6731
  }
6732
+
6733
+ // src/adapters/viem/resources/deposits/routes/approval.ts
6734
+ function errorText(error) {
6735
+ const parts = [];
6736
+ let current = error;
6737
+ for (let depth = 0; current && depth < 8; depth++) {
6738
+ const record = current;
6739
+ for (const key of ["name", "shortMessage", "message", "details"]) {
6740
+ const value = record[key];
6741
+ if (typeof value === "string") parts.push(value);
6742
+ }
6743
+ current = record.cause;
6744
+ }
6745
+ return parts.join("\n");
6746
+ }
6747
+ function isNoReturnApproveSimulationError(error) {
6748
+ const text = errorText(error);
6749
+ return /approve/i.test(text) && (/returned no data/i.test(text) || /return(?:ed)? data[^\n]*0x/i.test(text) || /0x[^\n]*no data/i.test(text));
6750
+ }
6751
+ async function buildApprovalRequest({
6752
+ ctx,
6753
+ token,
6754
+ spender,
6755
+ amount
6756
+ }) {
6757
+ try {
6758
+ const sim = await ctx.client.l1.simulateContract({
6759
+ address: token,
6760
+ abi: IERC20_default,
6761
+ functionName: "approve",
6762
+ args: [spender, amount],
6763
+ account: ctx.client.account
6764
+ });
6765
+ return { ...sim.request };
6766
+ } catch (error) {
6767
+ if (!isNoReturnApproveSimulationError(error)) {
6768
+ throw error;
6769
+ }
6770
+ return {
6771
+ address: token,
6772
+ abi: IERC20_default,
6773
+ functionName: "approve",
6774
+ args: [spender, amount],
6775
+ account: ctx.client.account
6776
+ };
6777
+ }
6778
+ }
6779
+
6780
+ // src/adapters/viem/resources/deposits/routes/erc20-nonbase.ts
6732
6781
  var { wrapAs: wrapAs3 } = createErrorHandlers("deposits");
6733
6782
  var ZERO_ASSET_ID = "0x0000000000000000000000000000000000000000000000000000000000000000";
6783
+ async function encodeSecondBridgeErc20DepositCalldata(input) {
6784
+ if (!input.ctx.resolvedToken) {
6785
+ return encodeSecondBridgeErc20Args(input.token, input.amount, input.receiver);
6786
+ }
6787
+ const l1ChainId = BigInt(await input.ctx.client.l1.getChainId());
6788
+ const isL1Origin = input.ctx.resolvedToken.assetId.toLowerCase() === ZERO_ASSET_ID || input.ctx.resolvedToken.originChainId === 0n || input.ctx.resolvedToken.originChainId === l1ChainId;
6789
+ if (isL1Origin) {
6790
+ return encodeSecondBridgeErc20Args(input.token, input.amount, input.receiver);
6791
+ }
6792
+ const transferData = encodeNativeTokenVaultTransferData(
6793
+ input.amount,
6794
+ input.receiver,
6795
+ input.token
6796
+ );
6797
+ return encodeSecondBridgeDataV1(input.ctx.resolvedToken.assetId, transferData);
6798
+ }
6734
6799
  async function getPriorityGasModel(input) {
6735
6800
  try {
6736
6801
  const l1NativeTokenVault = await input.ctx.contracts.l1NativeTokenVault();
@@ -6820,10 +6885,15 @@ function routeErc20NonBase() {
6820
6885
  const secondBridgeCalldata = await wrapAs3(
6821
6886
  "INTERNAL",
6822
6887
  OP_DEPOSITS.nonbase.encodeCalldata,
6823
- () => Promise.resolve(encodeSecondBridgeErc20Args(p.token, p.amount, receiver)),
6888
+ () => encodeSecondBridgeErc20DepositCalldata({
6889
+ ctx,
6890
+ token: p.token,
6891
+ amount: p.amount,
6892
+ receiver
6893
+ }),
6824
6894
  {
6825
6895
  ctx: {
6826
- where: "encodeSecondBridgeErc20Args",
6896
+ where: "encodeSecondBridgeErc20DepositCalldata",
6827
6897
  token: p.token,
6828
6898
  amount: p.amount.toString()
6829
6899
  },
@@ -6868,15 +6938,14 @@ function routeErc20NonBase() {
6868
6938
  }
6869
6939
  );
6870
6940
  if (depositAllowance < p.amount) {
6871
- const approveSim = await wrapAs3(
6941
+ const approveTx = await wrapAs3(
6872
6942
  "CONTRACT",
6873
6943
  OP_DEPOSITS.nonbase.estGas,
6874
- () => ctx.client.l1.simulateContract({
6875
- address: p.token,
6876
- abi: IERC20_default,
6877
- functionName: "approve",
6878
- args: [assetRouter, p.amount],
6879
- account: ctx.client.account
6944
+ () => buildApprovalRequest({
6945
+ ctx,
6946
+ token: p.token,
6947
+ spender: assetRouter,
6948
+ amount: p.amount
6880
6949
  }),
6881
6950
  {
6882
6951
  ctx: { where: "l1.simulateContract", to: p.token },
@@ -6888,7 +6957,7 @@ function routeErc20NonBase() {
6888
6957
  key: `approve:${p.token}:${assetRouter}`,
6889
6958
  kind: "approve",
6890
6959
  description: `Approve deposit token for amount`,
6891
- tx: { ...approveSim.request }
6960
+ tx: approveTx
6892
6961
  });
6893
6962
  }
6894
6963
  if (!baseIsEth) {
@@ -6907,15 +6976,14 @@ function routeErc20NonBase() {
6907
6976
  }
6908
6977
  );
6909
6978
  if (baseAllowance < mintValue) {
6910
- const approveBaseSim = await wrapAs3(
6979
+ const approveBaseTx = await wrapAs3(
6911
6980
  "CONTRACT",
6912
6981
  OP_DEPOSITS.nonbase.estGas,
6913
- () => ctx.client.l1.simulateContract({
6914
- address: baseToken,
6915
- abi: IERC20_default,
6916
- functionName: "approve",
6917
- args: [assetRouter, mintValue],
6918
- account: ctx.client.account
6982
+ () => buildApprovalRequest({
6983
+ ctx,
6984
+ token: baseToken,
6985
+ spender: assetRouter,
6986
+ amount: mintValue
6919
6987
  }),
6920
6988
  {
6921
6989
  ctx: { where: "l1.simulateContract", to: baseToken },
@@ -6927,7 +6995,7 @@ function routeErc20NonBase() {
6927
6995
  key: `approve:${baseToken}:${assetRouter}`,
6928
6996
  kind: "approve",
6929
6997
  description: `Approve base token for mintValue`,
6930
- tx: { ...approveBaseSim.request }
6998
+ tx: approveBaseTx
6931
6999
  });
6932
7000
  }
6933
7001
  }
@@ -7179,15 +7247,14 @@ function routeEthNonBase() {
7179
7247
  );
7180
7248
  const needsApprove = allowance < mintValue;
7181
7249
  if (needsApprove) {
7182
- const approveSim = await wrapAs4(
7250
+ const approveTx = await wrapAs4(
7183
7251
  "CONTRACT",
7184
7252
  OP_DEPOSITS.ethNonBase.estGas,
7185
- () => ctx.client.l1.simulateContract({
7186
- address: baseToken,
7187
- abi: IERC20_default,
7188
- functionName: "approve",
7189
- args: [ctx.l1AssetRouter, mintValue],
7190
- account: ctx.client.account
7253
+ () => buildApprovalRequest({
7254
+ ctx,
7255
+ token: baseToken,
7256
+ spender: ctx.l1AssetRouter,
7257
+ amount: mintValue
7191
7258
  }),
7192
7259
  {
7193
7260
  ctx: { where: "l1.simulateContract", to: baseToken },
@@ -7199,7 +7266,7 @@ function routeEthNonBase() {
7199
7266
  key: `approve:${baseToken}:${ctx.l1AssetRouter}`,
7200
7267
  kind: "approve",
7201
7268
  description: `Approve base token for fees (mintValue)`,
7202
- tx: { ...approveSim.request }
7269
+ tx: approveTx
7203
7270
  });
7204
7271
  }
7205
7272
  const secondBridgeCalldata = await wrapAs4(
@@ -7379,15 +7446,14 @@ function routeErc20Base() {
7379
7446
  );
7380
7447
  const needsApprove = allowance < mintValue;
7381
7448
  if (needsApprove) {
7382
- const approveSim = await wrapAs5(
7449
+ const approveTx = await wrapAs5(
7383
7450
  "CONTRACT",
7384
7451
  OP_DEPOSITS.base.estGas,
7385
- () => ctx.client.l1.simulateContract({
7386
- address: baseToken,
7387
- abi: IERC20_default,
7388
- functionName: "approve",
7389
- args: [ctx.l1AssetRouter, mintValue],
7390
- account: ctx.client.account
7452
+ () => buildApprovalRequest({
7453
+ ctx,
7454
+ token: baseToken,
7455
+ spender: ctx.l1AssetRouter,
7456
+ amount: mintValue
7391
7457
  }),
7392
7458
  {
7393
7459
  ctx: { where: "l1.simulateContract", to: baseToken },
@@ -7399,7 +7465,7 @@ function routeErc20Base() {
7399
7465
  key: `approve:${baseToken}:${ctx.l1AssetRouter}`,
7400
7466
  kind: "approve",
7401
7467
  description: "Approve base token for mintValue",
7402
- tx: { ...approveSim.request }
7468
+ tx: approveTx
7403
7469
  });
7404
7470
  }
7405
7471
  const req = buildDirectRequestStruct({