Web3 API

Signing a Transaction

Here’s an example of signing a transaction to update the blockchain state by creating a transaction. This example demonstrates updating a state variable in a deployed contract:

  1. 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, such as:

    1. No. of blocks (the number 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

getFeeHistory() will calculate an average of the blocks (mentioned in the 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 Opera:

Transfer

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

transfer

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.

transferWithParams

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
    chainId
};

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.

© 2024 Fantom Foundation