Dynamic Gas Fees
Learn how to send transactions with dynamic fees using JavaScript.
The objective of this document is to provide and explain sending transactions with dynamic fees using JavaScript. Make sure you have followed the tutorial on adjusting the dynamic fees using MetaMask. There, we have explained the key concepts related to dynamic fees and EIP1559 type of transactions.
Prerequisites
- Basic familiarity with JavaScript.
- Basic familiarity with Node.js and npm.
- Basic familiarity with the Avalanche C-Chain network and EVM compatibility
- Basic understanding of dynamic fee transactions
Installing Dependencies
Open the terminal and install the following dependencies in a new folder.
- Ethers
- avalanche
- dotenv
Setting up Environment and Project
To send a transaction we need to sign it using our private key. But private key should not be hard coded in the code, rather must be fetched through some environment variables. Make a .env
file in the root folder with the following content.
Now make a new file app.js
in the root folder, which will be our main and only file with the sendAvax()
function. Follow the rest of the tutorial by understanding and pasting the provided snippets sequentially in the app.js
file.
Importing Dependencies and Private Key
Setting up HTTP Provider
Using the HTTP provider, we will connect to one of the nodes on the Fuji network. Using this provider we will send the signed transaction to the network. You can also connect to Mainnet using the URL - https://api.avax.network/ext/bc/C/rpc
Setup C-Chain APIs to Estimate Fees
To estimate the max fee and max priority fee on the network, we will be using C-Chain APIs. We can use the C-Chain through an AvalancheJS instance connected to the network as shown below.
Function for Estimating Max Fee and Max Priority Fee
The function calcFeeData()
estimates the max fee and max priority fee per gas according to network activity using the C-Chain APIs. This function returns max fee and max priority fee per gas in units of nAVAX
or gwei
(1 AVAX = 10^9 gwei).
Actual API returns base fee and priority fee in units of wei
which is one-billionth of a billionth of AVAX
(1 AVAX = 10^18 wei).
Setting up Wallet
A wallet is required for signing transactions with your private key and thus making it valid.
Function to Create, Sign and Send Transaction
The function sendAvax()
takes 4 arguments -
amount
- Amount of AVAX to send in the transactionaddress
- Destination address to which we want to send AVAXmaxFeePerGas
- Desired maximum fee per gas you want to pay in nAVAXmaxPriorityFeePerGas
- Desired maximum priority fee per gas you want to pay in nAVAXnonce
- Used as a differentiator for more than 1 transaction with same signer
The last 3 arguments are optional, and if undefined
is passed, then it will use the calcFeeData()
function to estimate them. Each transaction with the same data and parameters is differentiated by a nonce value. If there are more than 1 transactions with the same nonce signed by the same address, then only 1 of them with the highest effective priority fee will be accepted. nonce
parameter should only be used when you are either re-issuing or cancelling a stuck transaction.
This function calculates transaction hash from the signed transaction and logs on the console, the URL for transaction status on the Snowtrace explorer.
Calling the sendAVAX()
Function
There are various ways to call this function. We may or may not pass the optional arguments like max fee and max priority fee. It is recommended to set the max fee as the maximum price per gas that you are willing to pay for a transaction, no matter how high or low the base fee will be, as at max you will only be charged the provided max fee, along with a small priority fee above the base fee.
If you do not pass these arguments, then it will automatically estimate the max fee and priority fee from the network. For example, let's say, I want to pay 100 nAVAX per gas for a transaction and a small tip of 2 nAVAX, then we will call the following function.
This function should not be used without a max fee per gas. As you will have to pay the estimated price, even if it is higher than your budget. There could be the following cases:
Max Fee | Max Priority Fee | Comment |
---|---|---|
undefined | 2 | It will calculate the max fee by adding the provided priority fee with the estimated base fee. Take extra precaution here as the max fee will now be capped by baseFee + priorityFee , which can consume all the provided priority fees. |
100 | *undefined- | It will estimate the priority fee and use the provided max fee. If the estimated priority fee is more than the provided max fee, then it throws an error. |
undefined | *undefined- | It will estimate the base fee and priority fee from the network, and will add both the values to calculate the max fee per gas. Again, you have to pay whatever will be estimated. |
You will get the following output on the successful submission of the signed transactions. Using this URL you can view the status of your transaction on Snowtrace.
Reissuance of Stuck Transaction
Sometimes during high network activity, all transactions couldn't make it to the latest blocks for a long time, due to relatively lower effective tip than the other transactions in the pool. We can either re-issue the same transaction with a higher priority fee or cancel the transaction. To re-issue the stuck transaction, you can send a new one with same amount and data but higher priority fee and same nonce value as the stuck transaction. The transaction with lower effective tip will automatically be rejected (due to same nonce), and you do not need to worry about it. You can also cancel the stuck transaction, by keeping the amount to 0, with a higher priority fee and same nonce. Let's say, the above transaction with a nonce value of 25 has stuck. You can then re-issue a new transaction with same nonce, but higher priority fee this time.
Conclusion
You have learned about creating, signing, and sending transactions with dynamic fee parameters to the C-Chain of Avalanche network using JavaScript. It also explained, how to re-issue or cancel a stuck transaction, by sending a transaction with the same nonce. This tutorial points out the recommended way for choosing max fee cap and max priority fee cap for transactions and can also work as a general guide for all the EVM-based chains.
Last updated on