{"id":4599,"date":"2025-01-02T12:24:26","date_gmt":"2025-01-02T12:24:26","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=4599"},"modified":"2025-02-07T10:34:00","modified_gmt":"2025-02-07T10:34:00","slug":"how-does-web3-eth-getgasprice-work","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/how-does-web3-eth-getgasprice-work\/","title":{"rendered":"web3.eth.getGasPrice() Explained: How to Get Ethereum Gas Prices in Web3.js"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/how-does-web3-eth-getgasprice-work\/#What_is_Web3js\" title=\"What is Web3.js?\">What is Web3.js?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/how-does-web3-eth-getgasprice-work\/#Different_Aspects_of_the_Process\" title=\"Different Aspects of the Process\">Different Aspects of the Process<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/how-does-web3-eth-getgasprice-work\/#How_you_can_use_web3ethgasPrice\" title=\"How you can use web3.eth.gasPrice() \">How you can use web3.eth.gasPrice() <\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/how-does-web3-eth-getgasprice-work\/#Track_Ethereum_Gas_Prices_in_Real-Time_with_Metaschools_Gas_Tracker\" title=\"Track Ethereum Gas Prices in Real-Time with Metaschool\u2019s Gas Tracker\">Track Ethereum Gas Prices in Real-Time with Metaschool\u2019s Gas Tracker<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>web3.eth.getGasPrice() is a function provided by the web3.js library, which is a JavaScript library that allows you to interact with the <a href=\"https:\/\/metaschool.so\/blockchains\/ethereum?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum blockchain<\/a> using a web3 provider (such as an Ethereum node).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Web3js\"><\/span>What is Web3.js?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Web3.js is a powerful, open-source JavaScript library that essentially functions as the bridge between decentralized applications (dApps) and the Ethereum blockchain. It enables developers to interact with Ethereum nodes using HTTP, IPC, or WebSocket connections, which allows for a range of functionalities\u2014from querying blockchain data and sending transactions to interacting with smart contracts. <\/p>\n\n\n\n<p>By abstracting complex blockchain protocols into simple, developer-friendly methods, Web3.js simplifies the process of building and integrating dApps. With robust documentation and a supportive community, it is a go-to tool for anyone looking to develop applications that leverage Ethereum&#8217;s decentralized network, making blockchain interactions more accessible and efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"different-aspects-of-the-process\"><span class=\"ez-toc-section\" id=\"Different_Aspects_of_the_Process\"><\/span>Different Aspects of the Process<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-determination-of-gas-price\">1. Determination of gas price<\/h3>\n\n\n\n<p>Gas price is determined by the Ethereum network and is based on supply and demand. When there are more transactions than available block space, the gas price goes up. When there is more block space than transactions, the gas price goes down. Gas price is set by the Ethereum miners and they can choose to mine transactions with higher gas prices first because they will be more profitable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-gas-limit\">2. Gas limit<\/h3>\n\n\n\n<p>Each transaction on the Ethereum network requires a certain amount of gas. Gas is used to pay for the computational resources used to process a transaction or execute a <a href=\"https:\/\/metaschool.so\/courses\/writing-your-first-hello-world-contract-in-solidity?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract<\/a>. The <code>web3.eth.getGasPrice()<\/code> function returns the price of one unit of gas in wei. The total cost of a transaction is the gas price multiplied by the gas limit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-wei-unit\">3. Wei unit<\/h3>\n\n\n\n<p><code>web3.eth.getGasPrice()<\/code> returns the current gas price in wei, which is the smallest unit of ether. It is important to note that the gas price is returned in wei because the gas limit is also expressed in wei. The gas price is set by the Ethereum network and is used to determine how much Ether a user needs to spend in order to execute a given transaction on the Ethereum blockchain. The higher the gas price, the more priority a transaction is given by miners when it is included in a <a href=\"https:\/\/metaschool.so\/articles\/block-meaning-blockchain\/\" target=\"_blank\" rel=\"noreferrer noopener\">block<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-nonce\">4. Nonce<\/h3>\n\n\n\n<p><a href=\"https:\/\/metaschool.so\/articles\/nonce-crypto-meaning\/\" target=\"_blank\" rel=\"noreferrer noopener\">Nonce<\/a> is a number that is unique per account. It is incremented with every transaction from an account. <code>web3.eth.getGasPrice()<\/code> does not take into account the nonce, it only returns the current gas price.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-network-dependent\">5. Network dependent<\/h3>\n\n\n\n<p>The gas price returned by <code>web3.eth.getGasPrice()<\/code> is dependent on the network you are connected to via the web3 provider. If you are connected to the Ethereum <a href=\"https:\/\/metaschool.so\/articles\/mainnet-meaning\/\" target=\"_blank\" rel=\"noreferrer noopener\">mainnet<\/a>, you will get the gas price for the mainnet. If you are connected to a <a href=\"https:\/\/metaschool.so\/articles\/testnet-meaning\/\" target=\"_blank\" rel=\"noreferrer noopener\">testnet<\/a>, you will get the gas price for that testnet.<\/p>\n\n\n\n<p>It is worth noting that the returned promise from <code>web3.eth.getGasPrice()<\/code> will resolve to a string representing the gas price in wei. If you need to convert it to <a href=\"https:\/\/metaschool.so\/articles\/gwei-meaning\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gwei<\/a> (another unit of gas) or ether, you can use the <code>web3.utils.fromWei()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-you-can-use-web3-eth-gasprice\"><span class=\"ez-toc-section\" id=\"How_you_can_use_web3ethgasPrice\"><\/span>How you can use <code>web3.eth.gasPrice()<\/code> <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is an example in JavaScript:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"const Web3 = require('web3');\nconst web3 = new Web3(new Web3.providers.HttpProvider(&quot;https:\/\/mainnet.infura.io\/v3\/YOUR-PROJECT-ID&quot;));\n\nweb3.eth.getGasPrice().then(console.log);\n\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">const Web3 = <\/span><span style=\"color: #C586C0\">require<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;web3&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const web3 = <\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">Web3<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> Web3.providers.<\/span><span style=\"color: #DCDCAA\">HttpProvider<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;https:\/\/mainnet.infura.io\/v3\/YOUR-PROJECT-ID&quot;<\/span><span style=\"color: #D4D4D4\">));<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">web3.eth.<\/span><span style=\"color: #DCDCAA\">getGasPrice<\/span><span style=\"color: #D4D4D4\">().<\/span><span style=\"color: #DCDCAA\">then<\/span><span style=\"color: #D4D4D4\">(console.log);<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In the example above, we first import the web3.js library and create a new instance of the web3 object, which is configured to connect to the Ethereum mainnet using an Infura node. <\/p>\n\n\n\n<p>Then we call the <code>web3.eth.getGasPrice()<\/code> function, which returns a promise that resolves to the current gas price in wei. Finally, we log the gas price to the console.<\/p>\n\n\n\n<p>In this example, The HttpProvider is connecting to the Ethereum node via HTTP and therefore can access the JSON-RPC API of the node.<\/p>\n\n\n\n<p>Please keep in mind, this is a simple and general example and the actual implementation can be more complex and might differ according to use case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"use-of-web3-eth-gasprice-in-a-more-complete-use-case\">Use of <code>web3.eth.gasPrice()<\/code> in a more complete use case<\/h3>\n\n\n\n<p>Let&#8217;s say you have a simple smart contract that allows users to transfer ether to other users. You want to create a front-end application that lets users interact with this contract using <a href=\"https:\/\/www.javascript.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">JavaScript<\/a>.<\/p>\n\n\n\n<p id=\"here-s-some-example-code-that-demonstrates-how-you-might-use-web3-eth-gasprice-when-calling-the-transfer-function-of-the-contract\">Here&#8217;s some example code that demonstrates how you might use <code>web3.eth.gasPrice()<\/code> when calling the <code>transfer()<\/code> function of the contract.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"const Web3 = require('web3');\nconst web3 = new Web3(new Web3.providers.HttpProvider(&quot;https:\/\/mainnet.infura.io\/v3\/YOUR-PROJECT-ID&quot;));\n\nconst contractAddress = &quot;0x...&quot;; \/\/ address of the deployed smart contract\nconst contractAbi = [\/* ABI of the contract *\/];\nconst contract = new web3.eth.Contract(contractAbi, contractAddress);\n\nconst fromAddress = &quot;0x...&quot;; \/\/ address of the sender\nconst privateKey = &quot;0x...&quot;; \/\/ private key of the sender\nconst toAddress = &quot;0x...&quot;; \/\/ address of the recipient\nconst amount = &quot;1&quot;; \/\/ amount of ether to send\n\n\/\/ Get the current gas price in wei\nweb3.eth.getGasPrice().then(gasPrice =&gt; {\n    \/\/ Estimate the gas needed for the transfer transaction\n    contract.methods.transfer(toAddress, web3.utils.toWei(amount, 'ether')).estimateGas({from: fromAddress}).then(estimatedGas =&gt; {\n        \/\/ Build the raw transaction\n        const rawTransaction = {\n            &quot;from&quot;: fromAddress,\n            &quot;to&quot;: contractAddress,\n            &quot;value&quot;: &quot;0x0&quot;,\n            &quot;gasPrice&quot;: web3.utils.toHex(gasPrice),\n            &quot;gas&quot;: web3.utils.toHex(estimatedGas),\n            &quot;data&quot;: contract.methods.transfer(toAddress, web3.utils.toWei(amount, 'ether')).encodeABI()\n        };\n\n        \/\/ Sign and send the transaction\n        web3.eth.accounts.signTransaction(rawTransaction, privateKey).then(signedTransaction =&gt; {\n            web3.eth.sendSignedTransaction(signedTransaction.rawTransaction).then(console.log);\n        });\n    });\n});\n\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">const Web3 = <\/span><span style=\"color: #C586C0\">require<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;web3&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const web3 = <\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">Web3<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> Web3.providers.<\/span><span style=\"color: #DCDCAA\">HttpProvider<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;https:\/\/mainnet.infura.io\/v3\/YOUR-PROJECT-ID&quot;<\/span><span style=\"color: #D4D4D4\">));<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const contractAddress = <\/span><span style=\"color: #CE9178\">&quot;0x...&quot;<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ address of the deployed smart contract<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const contractAbi = [<\/span><span style=\"color: #6A9955\">\/* ABI of the contract *\/<\/span><span style=\"color: #D4D4D4\">];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const <\/span><span style=\"color: #569CD6\">contract<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> web3.eth.<\/span><span style=\"color: #DCDCAA\">Contract<\/span><span style=\"color: #D4D4D4\">(contractAbi, contractAddress);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const fromAddress = <\/span><span style=\"color: #CE9178\">&quot;0x...&quot;<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ address of the sender<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const privateKey = <\/span><span style=\"color: #CE9178\">&quot;0x...&quot;<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ private key of the sender<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const toAddress = <\/span><span style=\"color: #CE9178\">&quot;0x...&quot;<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ address of the recipient<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">const amount = <\/span><span style=\"color: #CE9178\">&quot;1&quot;<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ amount of ether to send<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Get the current gas price in wei<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">web3.eth.<\/span><span style=\"color: #DCDCAA\">getGasPrice<\/span><span style=\"color: #D4D4D4\">().<\/span><span style=\"color: #DCDCAA\">then<\/span><span style=\"color: #D4D4D4\">(gasPrice =&gt; {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Estimate the gas needed for the transfer transaction<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">contract<\/span><span style=\"color: #D4D4D4\">.methods.<\/span><span style=\"color: #DCDCAA\">transfer<\/span><span style=\"color: #D4D4D4\">(toAddress, web3.utils.<\/span><span style=\"color: #DCDCAA\">toWei<\/span><span style=\"color: #D4D4D4\">(amount, <\/span><span style=\"color: #CE9178\">&#39;ether&#39;<\/span><span style=\"color: #D4D4D4\">)).<\/span><span style=\"color: #DCDCAA\">estimateGas<\/span><span style=\"color: #D4D4D4\">({from: fromAddress}).<\/span><span style=\"color: #DCDCAA\">then<\/span><span style=\"color: #D4D4D4\">(estimatedGas =&gt; {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">\/\/ Build the raw transaction<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        const rawTransaction = {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;from&quot;<\/span><span style=\"color: #D4D4D4\">: fromAddress,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;to&quot;<\/span><span style=\"color: #D4D4D4\">: contractAddress,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;value&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #CE9178\">&quot;0x0&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;gasPrice&quot;<\/span><span style=\"color: #D4D4D4\">: web3.utils.<\/span><span style=\"color: #DCDCAA\">toHex<\/span><span style=\"color: #D4D4D4\">(gasPrice),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;gas&quot;<\/span><span style=\"color: #D4D4D4\">: web3.utils.<\/span><span style=\"color: #DCDCAA\">toHex<\/span><span style=\"color: #D4D4D4\">(estimatedGas),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #CE9178\">&quot;data&quot;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #569CD6\">contract<\/span><span style=\"color: #D4D4D4\">.methods.<\/span><span style=\"color: #DCDCAA\">transfer<\/span><span style=\"color: #D4D4D4\">(toAddress, web3.utils.<\/span><span style=\"color: #DCDCAA\">toWei<\/span><span style=\"color: #D4D4D4\">(amount, <\/span><span style=\"color: #CE9178\">&#39;ether&#39;<\/span><span style=\"color: #D4D4D4\">)).<\/span><span style=\"color: #DCDCAA\">encodeABI<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        };<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">\/\/ Sign and send the transaction<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        web3.eth.accounts.<\/span><span style=\"color: #DCDCAA\">signTransaction<\/span><span style=\"color: #D4D4D4\">(rawTransaction, privateKey).<\/span><span style=\"color: #DCDCAA\">then<\/span><span style=\"color: #D4D4D4\">(signedTransaction =&gt; {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            web3.eth.<\/span><span style=\"color: #DCDCAA\">sendSignedTransaction<\/span><span style=\"color: #D4D4D4\">(signedTransaction.rawTransaction).<\/span><span style=\"color: #DCDCAA\">then<\/span><span style=\"color: #D4D4D4\">(console.log);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">});<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-1\">Step 1. <\/h4>\n\n\n\n<p>In this example, we first create a new instance of the web3 object and connect it to the Ethereum mainnet using an Infura node. Then we create a new instance of the contract object using the ABI and <a href=\"https:\/\/metaschool.so\/articles\/wallet-address-blockchain\/\">address<\/a> of the deployed contract.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-2\">Step 2. <\/h4>\n\n\n\n<p>After that, we define the addresses, private key and the amount of ether to be transferred.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-3\">Step 3. <\/h4>\n\n\n\n<p>Then we call <code>web3.eth.getGasPrice()<\/code> to get the current gas price in wei. We pass the gas price, the address to be transferred to, the amount of ether and the address of the sender, to the <code>contract.methods.transfer(toAddress, web3.utils.toWei(amount, 'ether')).estimateGas({from: fromAddress})<\/code> to estimate the gas required for the transaction.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-4\">Step 4. <\/h4>\n\n\n\n<p>After that we can now build the rawTransaction object, where we put all the details required by Ethereum to execute the transaction.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-5\">Step 5. <\/h4>\n\n\n\n<p>Finally, we can sign the transaction using the private key of the sender and send it using <code>web3.eth.sendSignedTransaction(signedTransaction.rawTransaction)<\/code> which returns the transaction receipt and logs it to the console.<\/p>\n\n\n\n<p>We have covered the basic flow of using <code>web3.eth.getGasPrice()<\/code> in a more complete use case. But here are some further notes.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"additional-notes-that-might-be-useful-to-you\">\u26a1\ufe0f Additional notes that might be useful to you<\/h4>\n\n\n\n<div class=\"wp-block-columns has-nv-site-bg-background-color has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>Keep in mind that the above example is simplified and doesn&#8217;t include any error handling or input validation. In a real-world application, you should always handle errors and validate user inputs to ensure that the transactions are executed as expected.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>Also, keep in mind that this example is for sending transactions on the Ethereum mainnet which incurs real cost, and testnet should be used for testing.<\/p>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>The returned promise from <code>web3.eth.getGasPrice()<\/code> will resolve to a string representing the gas price in wei. If you need to convert it to Gwei (another unit of gas) or ether, you can use the <code>web3.utils.fromWei()<\/code> function.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>The <code>estimateGas()<\/code> method returns the estimated gas cost of the given transaction in the form of a promise, which can be useful to make sure that you set the gas limit for the transaction correctly, otherwise the transaction will fail.<\/p>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p><code>web3.eth.accounts.signTransaction(rawTransaction, privateKey)<\/code> method will sign the transaction using the private key and returns a promise that will resolve to a <code>SignedTransaction<\/code> object that you can use to send the transaction via <code>web3.eth.sendSignedTransaction(signedTransaction.rawTransaction)<\/code>.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:50%\">\n<p>The <code>web3.eth.sendSignedTransaction()<\/code> method returns a promise that will resolve to the transaction receipt object which contains the details of the executed transaction.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Track_Ethereum_Gas_Prices_in_Real-Time_with_Metaschools_Gas_Tracker\"><\/span>Track Ethereum Gas Prices in Real-Time with Metaschool\u2019s Gas Tracker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Use Metaschool&#8217;s <a href=\"https:\/\/metaschool.so\/gastracker\">Gas Tracker<\/a> to keep an eye on gas prices in real time across multiple blockchain networks. This tool helps developers and users make informed decisions when interacting with the blockchain by giving them the most recent data on gas prices, transaction fees, and network congestion levels. You can improve the efficiency of your decentralized applications (dApps) by optimizing transaction costs and timing by being aware of current gas prices.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Your Own Gas Price Tracker<\/h3>\n\n\n\n<p>Metaschool offers an open-source <strong>Ethereum Gas Tracker<\/strong> template, enabling developers to create real-time gas tracking platforms with ease. Built using React.js for the frontend, Node.js for the backend, and integrating the Etherscan API for blockchain data, this template provides a responsive and SEO-optimized solution for monitoring Ethereum gas prices.<\/p>\n\n\n\n<p><strong>Key Features:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Responsive Design:<\/strong> Ensures optimal performance across various devices and screen sizes.<\/li>\n\n\n\n<li><strong>Real-Time Gas Prices:<\/strong> Displays current gas prices categorized into low, average, and high tiers.<\/li>\n\n\n\n<li><strong>Transaction Cost Estimation:<\/strong> Calculates costs for common Ethereum transactions, aiding users in financial planning.<\/li>\n\n\n\n<li><strong>Gas Trends Graphs:<\/strong> Visualizes gas price trends over the past hour and 24 hours, assisting in strategic decision-making.<\/li>\n<\/ul>\n\n\n\n<p>For a live demonstration, visit the <a href=\"https:\/\/ethereum-gas-tracker-metaschool.vercel.app\/\" target=\"_blank\" rel=\"noopener\">Ethereum Gas Tracker Demo<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1894\" height=\"865\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941.png\" alt=\"Ethereum Gas Tracker - Metaschool\" class=\"wp-image-11645\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941.png 1894w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941-300x137.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941-1024x468.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941-768x351.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-02-180941-1536x701.png 1536w\" sizes=\"auto, (max-width: 1894px) 100vw, 1894px\" \/><\/figure>\n<\/div>\n\n\n<p>This template serves as an excellent starting point for developers aiming to build efficient and user-friendly Ethereum gas tracking applications. For more details and to access the source code, visit the <a href=\"https:\/\/metaschool.so\/templates\/web3\/ethereum-gas-tracker\">Ethereum Gas Tracker AI Template<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":6,"featured_media":11641,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[],"class_list":["post-4599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=4599"}],"version-history":[{"count":15,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4599\/revisions"}],"predecessor-version":[{"id":12262,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4599\/revisions\/12262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11641"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=4599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=4599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=4599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}