Skip to main content

estimateGas

Callable

  • estimateGas<ReturnFormat>(web3Context: Web3Context<EthExecutionAPI, any>, transaction: Transaction, blockNumber?: BlockNumberOrTag, returnFormat: ReturnFormat): Promise<NumberTypes[ReturnFormat[number]]>

  • Simulates the transaction within the EVM to estimate the amount of gas to be used by the transaction. The transaction will not be added to the blockchain, and actual gas usage can vary when interacting with a contract as a result of updating the contract’s state.


    Type parameters

    • ReturnFormat: DataFormat

    Parameters

    • web3Context: Web3Context<EthExecutionAPI, any>

      (Web3Context) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.

    • transaction: Transaction

      The Transaction object to estimate the gas for.

    • blockNumber: BlockNumberOrTag = web3Context.defaultBlock

      (BlockNumberOrTag defaults to Web3Eth.defaultBlock) - Specifies what block to use as the current state of the blockchain while processing the gas estimation.

    • returnFormat: ReturnFormat

      (DataFormat defaults to DEFAULT_RETURN_FORMAT) - Specifies how the return data from the call should be formatted.

    Returns Promise<NumberTypes[ReturnFormat[number]]>

    The used gas for the simulated transaction execution.

    const transaction = {
    from: '0xe899f0130FD099c0b896B2cE4E5E15A25b23139a',
    to: '0xe899f0130FD099c0b896B2cE4E5E15A25b23139a',
    value: '0x1',
    nonce: '0x1',
    type: '0x0'
    }

    web3.eth.estimateGas(transaction).then(console.log);
    > 21000n

    web3.eth.estimateGas(transaction, { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }).then(console.log);
    > 21000