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:
We call the function
setGreeting
and store the transaction in a variabletx
We then pass the
tx
along with contract detailsThe 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:
web3.eth.getGasPrice()
gets the latest gas priceweb3.eth.getFeeHistory()
gets the gas price using custom parameters, such as:No. of blocks (the number of previous blocks it searches)
String/BN value to request the newest block in the requested range
Percentile options (1-99th percentile)
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.
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.