Web3 API

Signing a transaction

There's an example for signing a transaction in case there's a need to update the state of blockchain by creating a transaction. Following is an example that updates the state variable of a deployed contract.

  1. In this case, we call the function setGreeting and store the transaction in a variable tx

  2. We then pass the tx along with contract details

  3. The function signTransaction creates a block of data containing all the necessary information like gas price, contract information, network information, etc. and calls the web3 method to sign the transaction.

Gas price

We have added two methods to fetch the gas price

  1. web3.eth.getGasPrice() gets the latest gas price

  2. web3.eth.getFeeHistory() gets the gas price using custom parameters like

    1. No. of blocks (the amount of previous blocks it searches)

    2. String/BN value to request the newest block in the requested range

    3. Percentile options (1-99th percentile)

    4. CallBack

The getFeeHistory() will calculate an average of the blocks (mentioned in first parameter) and figure out the best gas prices based on the percentile for slow/average/fast options. This API also returns the baseFeePerGas (introduced after EIP-1559) which is added to the gas prices outputted by the above call.

Rest of the Web3 API calls and examples on Fantom: https://github.com/Fantom-foundation/web3-methods/wiki


We have added two ways to sign a transaction with a simple example of transferring FTM between accounts.


The first method takes a simple approach and uses the predefined web3 method to determine the gas price required to send FTM between two accounts.


This method transfers with additional parameters in its transaction block.

// Change maxFeePerGas and maxPriorityFeePerGas as per the current gas prices
var transaction = {
    to: TO_ADDRESS,
    from: address,
    nonce: nonceVal,
    value: testWeb3.utils.toHex(testWeb3.utils.toWei('0.1', 'ether')),
    gas: testWeb3.utils.toHex(21000),
    maxFeePerGas: '0x174876E800', // 100 Gwei 
    maxPriorityFeePerGas: '0xBA43B7400', // 50 Gwei

The code snippet mentioned above employs the variables maxFeePerGas and maxPriorityFeePerGas. Following the London fork, each block now includes a baseFeePerGas. This base fee represents the minimum cost required for sending a transaction on the network. Unlike miners, the network itself determines the base fee. The base fee varies from block to block depending on the previous block's capacity.

When submitting a transaction, you must also provide a "tip" in the maxPriorityFeePerGas field. To ensure that the miner has an incentive to process your transaction, the minimum tip amount you should offer is 1 wei. The likelihood of your transaction being included in a block increases as you offer a higher tip. To initiate a transaction on the network, users have the option to specify the maximum amount they are willing to pay for their transaction to be executed. This parameter is called maxFeePerGas. However, for a transaction to be successfully executed, the max fee specified must be greater than or equal to the sum of the base fee and the maxPriorityFeePerGas.

Last updated

© 2024 Fantom Foundation