@matterlabs/zksync-js 0.0.25 → 0.0.26

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,5 +1,5 @@
1
- export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalProtocolService, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs } from '../../chunk-NFRRRT5X.js';
2
- import '../../chunk-JWN26CQ6.js';
1
+ export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalProtocolService, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs } from '../../chunk-VVDJFIXV.js';
2
+ import '../../chunk-FMBQ7PVA.js';
3
3
  import '../../chunk-BQ2YCWV2.js';
4
4
  import '../../chunk-OEUDZFXH.js';
5
5
  export { createViemClient as createClient, createViemClient } from '../../chunk-YEA74D2L.js';
@@ -34,6 +34,19 @@ export declare function quoteL1Gas(input: QuoteL1GasInput): Promise<GasQuote | u
34
34
  * Quote L2 gas for an L2 execution.
35
35
  */
36
36
  export declare function quoteL2Gas(input: QuoteL2GasInput): Promise<GasQuote | undefined>;
37
+ /**
38
+ * `l2GasLimit` for a base-token deposit (`requestL2TransactionDirect`). An explicit
39
+ * `ctx.l2GasLimit` wins. EraVM chains use the protocol floor. ZKsync OS chains use the node's
40
+ * estimate of the real L2 call, floored by the protocol minimum. When L1 cannot be read, the chain
41
+ * is treated as a ZKsync OS rollup, which only errs toward a higher limit.
42
+ */
43
+ export declare function determineBaseL2GasLimit(input: {
44
+ ctx: BuildCtx;
45
+ l2Contract: Address;
46
+ l2Value: bigint;
47
+ l2Calldata: `0x${string}`;
48
+ priorityFloorGasLimit: bigint;
49
+ }): Promise<bigint>;
37
50
  export declare function determineErc20L2Gas(input: {
38
51
  ctx: BuildCtx;
39
52
  l1Token: Address;
@@ -6616,10 +6616,28 @@ async function quoteL2Gas(input) {
6616
6616
  }
6617
6617
 
6618
6618
  // src/core/resources/deposits/priority.ts
6619
+ var ZK_CHAIN_PRICING_GETTERS_ABI = [
6620
+ {
6621
+ type: "function",
6622
+ name: "getZKsyncOS",
6623
+ stateMutability: "view",
6624
+ inputs: [],
6625
+ outputs: [{ name: "", type: "bool" }]
6626
+ },
6627
+ {
6628
+ type: "function",
6629
+ name: "getPubdataPricingMode",
6630
+ stateMutability: "view",
6631
+ inputs: [],
6632
+ outputs: [{ name: "", type: "uint8" }]
6633
+ }
6634
+ ];
6635
+ var PUBDATA_PRICING_MODE_ROLLUP = 0;
6619
6636
  var PRIORITY_TX_ENCODING_STEP_BYTES = 544n;
6620
6637
  var L1_TO_L2_ALIAS_OFFSET = 0x1111000000000000000000000000000000001111n;
6621
6638
  var DEFAULT_PRIORITY_BODY_GAS_ESTIMATE_MULTIPLIER = 7n;
6622
6639
  var PRIORITY_L2_GAS_BUFFER = 40n;
6640
+ var BASE_TOKEN_DEPOSIT_NEW_ACCOUNTS_PUBDATA_BYTES = 590n;
6623
6641
  var maxBigInt2 = (a, b) => a > b ? a : b;
6624
6642
  var ceilDiv = (a, b) => (a + b - 1n) / b;
6625
6643
  function applyL1ToL2Alias(address) {
@@ -6666,6 +6684,16 @@ function clampPriorityL2GasLimit(input) {
6666
6684
  })
6667
6685
  );
6668
6686
  }
6687
+ function resolveBaseTokenPriorityL2GasLimit(input) {
6688
+ if (input.nodeEstimate != null && input.nodeEstimate > 0n) {
6689
+ return maxBigInt2(input.priorityFloorGasLimit, applyGasBuffer(input.nodeEstimate));
6690
+ }
6691
+ if (!input.chargesPubdata) return input.priorityFloorGasLimit;
6692
+ return maxBigInt2(
6693
+ input.priorityFloorGasLimit,
6694
+ applyGasBuffer(BASE_TOKEN_DEPOSIT_NEW_ACCOUNTS_PUBDATA_BYTES * input.gasPerPubdata)
6695
+ );
6696
+ }
6669
6697
  function resolveRegisteredTokenPriorityL2GasLimit(input) {
6670
6698
  if (input.nodeEstimate != null && input.nodeEstimate > 0n) {
6671
6699
  return maxBigInt2(input.priorityFloorGasLimit, applyGasBuffer(input.nodeEstimate));
@@ -6676,8 +6704,6 @@ function resolveRegisteredTokenPriorityL2GasLimit(input) {
6676
6704
  applyPriorityL2GasLimitBuffer({ gasLimit: modeledGas })
6677
6705
  );
6678
6706
  }
6679
-
6680
- // src/adapters/viem/estimator.ts
6681
6707
  function toCoreTx(tx) {
6682
6708
  return {
6683
6709
  to: tx.to,
@@ -6736,6 +6762,7 @@ function viemToGasEstimator(client) {
6736
6762
  from: tx.from,
6737
6763
  to: tx.to,
6738
6764
  data: tx.data,
6765
+ ...tx.value != null ? { value: viem.toHex(tx.value) } : {},
6739
6766
  type: ZKSYNC_OS_PRIORITY_TX_TYPE
6740
6767
  }
6741
6768
  ]
@@ -6784,6 +6811,48 @@ async function estimatePriorityTxL2Gas(ctx, leg) {
6784
6811
  return void 0;
6785
6812
  }
6786
6813
  }
6814
+ async function resolvePriorityTxL2Sender(ctx) {
6815
+ try {
6816
+ const code = await ctx.client.l1.getCode({ address: ctx.sender });
6817
+ if (!code || code === "0x") return ctx.sender;
6818
+ } catch {
6819
+ }
6820
+ return applyL1ToL2Alias(ctx.sender);
6821
+ }
6822
+ var chainPricingCache = /* @__PURE__ */ new WeakMap();
6823
+ var isContractRevert = (err) => err instanceof viem.BaseError && err.walk((e) => e instanceof viem.ContractFunctionRevertedError) != null;
6824
+ async function readChainPricing(ctx) {
6825
+ const cached = chainPricingCache.get(ctx.client)?.get(ctx.chainIdL2);
6826
+ if (cached) return cached;
6827
+ try {
6828
+ const zkChain = await ctx.client.l1.readContract({
6829
+ address: ctx.bridgehub,
6830
+ abi: IBridgehub_default,
6831
+ functionName: "getZKChain",
6832
+ args: [ctx.chainIdL2]
6833
+ });
6834
+ const read = (functionName) => ctx.client.l1.readContract({
6835
+ address: zkChain,
6836
+ abi: ZK_CHAIN_PRICING_GETTERS_ABI,
6837
+ functionName
6838
+ });
6839
+ let zksyncOS;
6840
+ try {
6841
+ zksyncOS = await read("getZKsyncOS");
6842
+ } catch (err) {
6843
+ if (!isContractRevert(err)) throw err;
6844
+ zksyncOS = false;
6845
+ }
6846
+ const chargesPubdata = zksyncOS && Number(await read("getPubdataPricingMode")) === PUBDATA_PRICING_MODE_ROLLUP;
6847
+ const pricing = { zksyncOS, chargesPubdata };
6848
+ const perClient = chainPricingCache.get(ctx.client) ?? /* @__PURE__ */ new Map();
6849
+ perClient.set(ctx.chainIdL2, pricing);
6850
+ chainPricingCache.set(ctx.client, perClient);
6851
+ return pricing;
6852
+ } catch {
6853
+ return void 0;
6854
+ }
6855
+ }
6787
6856
  async function quoteL1Fees2(input) {
6788
6857
  const { ctx } = input;
6789
6858
  return wrapAs(
@@ -6820,6 +6889,26 @@ async function quoteL2Gas2(input) {
6820
6889
  stateOverrides: input.stateOverrides
6821
6890
  });
6822
6891
  }
6892
+ async function determineBaseL2GasLimit(input) {
6893
+ const { ctx } = input;
6894
+ if (ctx.l2GasLimit != null) return ctx.l2GasLimit;
6895
+ const [pricing, from] = await Promise.all([
6896
+ readChainPricing(ctx),
6897
+ resolvePriorityTxL2Sender(ctx)
6898
+ ]);
6899
+ if (pricing && !pricing.zksyncOS) return input.priorityFloorGasLimit;
6900
+ return resolveBaseTokenPriorityL2GasLimit({
6901
+ priorityFloorGasLimit: input.priorityFloorGasLimit,
6902
+ gasPerPubdata: ctx.gasPerPubdata,
6903
+ chargesPubdata: pricing?.chargesPubdata ?? true,
6904
+ nodeEstimate: await estimatePriorityTxL2Gas(ctx, {
6905
+ from,
6906
+ to: input.l2Contract,
6907
+ data: input.l2Calldata,
6908
+ value: input.l2Value
6909
+ })
6910
+ });
6911
+ }
6823
6912
  async function determineNonBaseL2Gas(input) {
6824
6913
  const { ctx, l1Token, route } = input;
6825
6914
  const fallbackQuote = () => quoteL2Gas2({
@@ -7019,13 +7108,19 @@ function routeEthDirect() {
7019
7108
  l2Calldata,
7020
7109
  gasPerPubdata: ctx.gasPerPubdata
7021
7110
  });
7022
- const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
7023
- gasLimit: applyPriorityL2GasLimitBuffer({
7024
- chainIdL2: ctx.chainIdL2,
7025
- gasLimit: priorityFloorBreakdown.derivedL2GasLimit
7026
- }),
7111
+ const quotedL2GasLimit = await determineBaseL2GasLimit({
7112
+ ctx,
7113
+ l2Contract,
7114
+ l2Value,
7027
7115
  l2Calldata,
7028
- gasPerPubdata: ctx.gasPerPubdata
7116
+ priorityFloorGasLimit: clampPriorityL2GasLimit({
7117
+ gasLimit: applyPriorityL2GasLimitBuffer({
7118
+ chainIdL2: ctx.chainIdL2,
7119
+ gasLimit: priorityFloorBreakdown.derivedL2GasLimit
7120
+ }),
7121
+ l2Calldata,
7122
+ gasPerPubdata: ctx.gasPerPubdata
7123
+ })
7029
7124
  });
7030
7125
  const l2GasParams = await quoteL2Gas2({
7031
7126
  ctx,
@@ -7845,13 +7940,19 @@ function routeErc20Base() {
7845
7940
  l2Calldata,
7846
7941
  gasPerPubdata: ctx.gasPerPubdata
7847
7942
  });
7848
- const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
7849
- gasLimit: applyPriorityL2GasLimitBuffer({
7850
- chainIdL2: ctx.chainIdL2,
7851
- gasLimit: priorityFloorBreakdown.derivedL2GasLimit
7852
- }),
7943
+ const quotedL2GasLimit = await determineBaseL2GasLimit({
7944
+ ctx,
7945
+ l2Contract,
7946
+ l2Value,
7853
7947
  l2Calldata,
7854
- gasPerPubdata: ctx.gasPerPubdata
7948
+ priorityFloorGasLimit: clampPriorityL2GasLimit({
7949
+ gasLimit: applyPriorityL2GasLimitBuffer({
7950
+ chainIdL2: ctx.chainIdL2,
7951
+ gasLimit: priorityFloorBreakdown.derivedL2GasLimit
7952
+ }),
7953
+ l2Calldata,
7954
+ gasPerPubdata: ctx.gasPerPubdata
7955
+ })
7855
7956
  });
7856
7957
  const l2Gas = await quoteL2Gas2({
7857
7958
  ctx,