Estimate Gas


Estimate Gas

Estimation Methods

The estimation methods in the zkSync Era JS SDK provide a way to estimate the gas required for various types of transactions. These methods return a Promise that resolves to a BigNumber, representing the estimated gas cost in wei.

At the bottom of this section you can see a working example using Stackblitz for each of the method operations.

estimateGas Method

Overview

The estimateGas method returns a Promise that resolves to a BigNumber representing an estimate of the amount of gas required to submit a given transaction to the network.

Info

Keep in mind that the estimate may not be entirely accurate. Network conditions, such as other transactions being mined, can affect the actual gas required.

Method Signature

estimateGas(transaction: TransactionRequest): Promise<BigNumber>

Parameters

ParameterTypeDescription
transactionTransactionRequestAn object containing the details of the transaction you want to estimate for

TransactionRequest Object

PropertyTypeDescriptionExample
tostringThe recipient's address"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
datastringThe transaction data"0xd0e30db0"
valueBigNumberThe value being sent in the transactionparseEther("1.0")

Return Value

Returns a Promise that resolves to a BigNumber representing the estimated gas cost.

Example Usage

Here's how you can use estimateGas to estimate the gas for a deposit transaction:`

import { Provider } from "zksync-ethers";
import { utils } from "ethers";

const provider = new Provider("https://testnet.era.zksync.dev");

const estimate = await provider.estimateGas({
  // Wrapped ETH address
  to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",

  // `function deposit() payable`
  data: "0xd0e30db0",

  // 1 ether
  value: utils.parseEther("1.0"),
});

console.log(estimate);

estimateGasL1 Method

Overview

The estimateGasL1 method returns a Promise that resolves to a BigNumber, providing an estimate of the amount of gas required to submit a transaction from Layer 1 (L1) to Layer 2 (L2).

Method Signature

provider.estimateGasL1(transaction: <TransactionRequest>): Promise<BigNumber>

Parameters

ParameterTypeDescription
transactionTransactionRequestAn object containing the details of the L1 to L2 transaction you want to estimate for

TransactionRequest Object

PropertyTypeDescriptionExample
tostringThe recipient's address on L2"0xYourL2AddressHere"
datastringThe transaction data"0xYourDataHere"
valueBigNumberThe value being sent in the transactionparseEther("1.0")
customDataanyCustom data for the transaction (optional)<pre class="language-typescript" data-overflow="wrap"><code class="lang-typescript">customData: { gasPerPubdataByte: 0 } </code></pre>

Return Value

Returns a Promise that resolves to a BigNumber representing the estimated gas cost for the L1 to L2 transaction.

Example Usage

Here's how you can use estimateGasL1 to estimate the gas for a transaction from L1 to L2:

import { Provider, utils } from "zksync-ethers";
import { utils } from "ethers";

// Initialize a new Provider instance
const provider = new Provider("https://testnet.era.zksync.dev");

// Define the transaction details
const transaction = {
  to: "0x1111111111111111111111111111111111111111",
  data: "0xffffffff",
  value: utils.parseEther("1.0"),
  customData: { gasPerPubdataByte: 0 },
};

// Estimate the gas
const estimate = await provider.estimateGasL1(transaction);

// Log the estimated gas
console.log(`Estimated gas for L1 to L2 transaction: ${estimate}`);

estimateGasTransfer Method

Overview

The estimateGasTransfer method returns a Promise that resolves to a BigNumber, providing an estimate of the amount of gas required to execute a token transfer transaction.

Method Signature

provider.estimateGasTransfer(transaction: {
  to: Address;
  amount: BigNumberish;
  from?: Address;
  token?: Address;
  overrides?: ethers.CallOverrides;
}): Promise<BigNumber>

Parameters

ParameterTypeDescription
transactionObjectAn object containing the details of the token transfer transaction you want to estimate for

Transaction Object

PropertyTypeDescriptionExample
toAddressThe recipient's address"0xRecipientAddressHere"
amountBigNumberishThe amount of token to transfer1000
fromAddress (Optional)The sender's address"0xSenderAddressHere"
tokenAddress (Optional)The token's contract address"0xTokenAddressHere"
overridesethers.CallOverrides (Optional)Ethers call overrides object{ gasLimit: 21000 }

Return Value

Returns a Promise that resolves to a BigNumber representing the estimated gas cost for the token transfer transaction.

Example Usage

Here's how you can use estimateGasTransfer to estimate the gas for a token transfer transaction:

import { Provider } from "zksync-ethers";
import { BigNumber } from "ethers";

// Initialize a new Provider instance
const provider = new Provider("https://testnet.era.zksync.dev");

// Define the transaction details
const transaction = {
  to: "0xRecipientAddressHere",
  amount: BigNumber.from("1000"),
  from: "0xSenderAddressHere",
  token: "0xTokenAddressHere",
  overrides: { gasLimit: 21000 },
};

// Estimate the gas
const estimate = await provider.estimateGasTransfer(transaction);

// Log the estimated gas
console.log(`Estimated gas for token transfer: ${estimate}`);

estimateGasWithdraw Method

Overview

The estimateGasWithdraw method returns a Promise that resolves to a BigNumber, providing an estimate of the amount of gas required to execute a token withdrawal transaction.

Method Signature

provider.estimateGasWithdraw(transaction: {
  token: Address;
  amount: BigNumberish;
  from?: Address;
  to?: Address;
  bridgeAddress?: Address;
  overrides?: ethers.CallOverrides;
}): Promise<BigNumber>

Parameters

ParameterTypeDescription
transactionObjectAn object containing the details of the token withdrawal transaction you want to estimate for

Transaction Object

PropertyTypeDescriptionExample
tokenAddressThe token's contract address"0xTokenAddressHere"
amountBigNumberishThe amount of token to withdraw1000
fromAddress (Optional)The sender's address"0xSenderAddressHere"
toAddress (Optional)The recipient's address"0xRecipientAddressHere"
bridgeAddressAddress (Optional)The bridge contract address"0xBridgeAddressHere"
overridesethers.CallOverrides (Optional)Ethers call overrides object{ gasLimit: 21000 }

Return Value

Returns a Promise that resolves to a BigNumber representing the estimated gas cost for the token withdrawal transaction.

Example Usage

Here's how you can use estimateGasWithdraw to estimate the gas for a token withdrawal transaction:

import { Provider } from "zksync-ethers";
import { BigNumber } from "ethers";

// Initialize a new Provider instance
const provider = new Provider("https://testnet.era.zksync.dev");

// Define the transaction details
const transaction = {
  token: "0xTokenAddressHere",
  amount: BigNumber.from("1000"),
  from: "0xSenderAddressHere",
  to: "0xRecipientAddressHere",
  bridgeAddress: "0xBridgeAddressHere",
  overrides: { gasLimit: 21000 },
};

// Estimate the gas
const estimate = await provider.estimateGasWithdraw(transaction);

// Log the estimated gas
console.log(`Estimated gas for token withdrawal: ${estimate}`);

estimateL1ToL2Execute Method

Overview

The estimateL1ToL2Execute method returns a Promise that resolves to a BigNumber, providing an estimate of the amount of gas required to execute an L1 to L2 operation.

Method Signature

provider.estimateL1ToL2Execute(transaction: {
  contractAddress: Address;
  calldata: BytesLike;
  caller?: Address;
  l2Value?: BigNumberish;
  factoryDeps?: ethers.BytesLike[];
  gasPerPubdataByte?: BigNumberish;
  overrides?: ethers.PayableOverrides;
}): Promise<BigNumber>

Parameters

ParameterTypeDescription
transactionObjectAn object containing the details of the L1 to L2 operation you want to estimate for

Transaction Object

PropertyTypeDescriptionExample
contractAddressAddressThe contract's address"0xContractAddressHere"
calldataBytesLikeThe transaction call data"0xSomeCallDataHere"
callerAddress (Optional)The caller's address"0xCallerAddressHere"
l2ValueBigNumberish (Optional)Current L2 gas value1000
factoryDepsethers.BytesLike[] (Optional)Byte array containing contract bytecode["0xByteCode1", "0xByteCode2"]
gasPerPubdataByteBigNumberish (Optional)Constant representing current amount of gas per byte100
overridesethers.PayableOverrides (Optional)Ethers payable overrides object{ gasLimit: 21000 }

Return Value

Returns a Promise that resolves to a BigNumber representing the estimated gas cost for the L1 to L2 operation.

Example Usage

Here's how you can use estimateL1ToL2Execute to estimate the gas for an L1 to L2 operation:

typescriptCopy codeimport { Provider } from "zksync-ethers";
import { BigNumber } from "ethers";

// Initialize a new Provider instance
const provider = new Provider("https://testnet.era.zksync.dev");

// Define the transaction details
const transaction = {
  contractAddress: "0xContractAddressHere",
  calldata: "0xSomeCallDataHere",
  caller: "0xCallerAddressHere",
  l2Value: BigNumber.from("1000"),
  factoryDeps: ["0xByteCode1", "0xByteCode2"],
  gasPerPubdataByte: BigNumber.from("100"),
  overrides: { gasLimit: 21000 }
};

// Estimate the gas
const estimate = await provider.estimateL1ToL2Execute(transaction);

// Log the estimated gas
console.log(`Estimated gas for L1 to L2 operation: ${estimate}`);

Try out gas estimation