Get you own node endpoint todayStart for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.
Parameters
none
Response
quantity— the integer value of the current gas base fee, returned in Wei
eth_gasPrice code examples
Use case
You can useeth_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.
NoteThe
eth_gasPrice method returns the base fee.index.js
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 10 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 Gwei 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.