curl --request POST \
--url https://nd-418-459-126.p2pify.com/8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc \
--header 'Content-Type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_gasPrice",
"params": []
}
'{
"jsonrpc": "<string>",
"id": 123,
"result": "<string>"
}curl --request POST \
--url https://nd-418-459-126.p2pify.com/8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc \
--header 'Content-Type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_gasPrice",
"params": []
}
'{
"jsonrpc": "<string>",
"id": 123,
"result": "<string>"
}nonequantity — the integer value of the current gas base fee, returned in Weieth_gasPrice code examplesconst Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);
async function getGasPrice() {
const baseFee = await web3.eth.getGasPrice();
console.log(`the base gas fee is: ${baseFee} Wei`);
}
getGasPrice()
eth_gasPrice to calculate the total gas value to send with a transaction based on the base and priority fee system. This concept was implemented with EIP-1559 in Ethereum’s London hard fork.
EIP-1559 aimed to solve the problem of network congestion by implementing a dynamic fee market mechanism, which adjusts the fee required to process a transaction based on network demand. with the current system, the total gas price is composed of a base fee determined by the network’s load and a priority fee added by the user.
eth_gasPrice method returns the base fee.const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);
async function getGas(priorityFee) {
const priorityFeeWei = web3.utils.toWei(String(priorityFee), 'gwei');
const baseFee = await web3.eth.getGasPrice();
const totalFee = Number(priorityFeeWei) + Number(baseFee)
return [totalFee, baseFee]
}
async function main() {
// The priority fee is constant at 2 nAVAX, the user can change it.
const PRIORITY_FEE = 2;
const [totalFee, baseFee] = await getGas(PRIORITY_FEE)
const baseFeeGwei = web3.utils.fromWei(String(baseFee), 'gwei')
console.log(`The priority fee: ${PRIORITY_FEE} nAVAX.`)
console.log(`The Avalanche base fee at this moment is: ${Number(baseFeeGwei).toFixed(2)} nAVAX.`)
const gweiFee = web3.utils.fromWei(String(totalFee), 'gwei')
console.log(`The total gas fee should be at least: ${Number(gweiFee).toFixed(2)} nAVAX.`)
}
main()
getGas function calculates the total fee required to process a transaction. The function takes in a priorityFee as input representing the fee a user is willing to pay to prioritize their transaction processing. The function first converts the priorityFee from Gwei to Wei (the smallest unit of ether) using web3.utils.toWei. Then, it uses the web3.eth.getGasPrice method to retrieve the current base fee set by the network. The function finally adds the priority fee to the base fee and returns the total fee along with the base fee as an array.
The main function is the entry point of the code. It first sets a constant PRIORITY_FEE to 2 Gwei. Then calls the getGas function with PRIORITY_FEE as an argument and destructures the returned array into two variables totalFee and baseFee. The code then converts baseFee and totalFee from Wei to nAVAX using web3.utils.fromWei for better readability, and logs the results to the console.
The main function logs the priority fee, the base fee at that moment, and the minimum recommended total fee that should be paid to process a transaction.Was this page helpful?