I am using below code to call a smart contract method on Ethereum network.

      const w = new Web3(url);
      await w.eth.accounts.signTransaction(
              from: account.address,
              to: contractAddress,
              gas: 1000000,
              gasPrice: 1000000000,
              nonce: await w.eth.getTransactionCount(account.address),
              data: await contract.methods
                  { value: web3.utils.fromWei(price, 'ether') }

In above code, it requires gas and gasPrice parameters. I need to set the value when calling the contract method. My question is how I know what the right value to set? I thought those values are not static which can be set during runtime when Metamask is open. Do I misunderstand anything on gas?

1 Answer 1


gas: Mean how much your transaction will cost in gas if it executes. You can estimate your transaction before broadcast it to get estimation, and for safety you can increase a% from your estimation

gasPrice: try this https://www.quicknode.com/docs/ethereum/eth_gasPrice

Or easier, use some library like hardhat, viem, ... , it can auto set for you

  • I mean those value is not static that could change anytime. If I put some estimate algorithm plus a% in the code, what happen if the gas is changed by the network? Commented Feb 27 at 10:29
  • gasPrice changed by the network. gas changed by your transaction execution. Estimation mean you run it virtually on evm => it will return how much gas cost for that execution
    – CT95
    Commented Feb 27 at 10:31
  • If I set the gas price a little higher than the getPrice returned from the network, will the real gas charged by the network be lower than the gas set in the transaction? Will there be a return from the network to the charged account? Commented Feb 27 at 10:45
  • Gas and gasPrice are the not same unit of measure. You can see here: ethereum.stackexchange.com/questions/54606/… Or I can give a simply example in life: gas => how much gasoline for your car gasPrice => price of gasoline Today, you want to drive to London, you will need 1000L gas, and gasPrice today is 5000¥/L Nextday, you want to drive to Paris, 500L gas, and because of war gasPrice now is 7000¥/L
    – CT95
    Commented Feb 28 at 2:47
  • You can see, how much gas you cost belongs to how long you want to drive, mean how much code you will execute in evm. And gasPrice will be changed by the network, how much previous txs pay for miner. And about your question, you can set any value for gasPrice if lower than normal, your tx will be stuck, wait until gasPrice decrease to your value if greater than normal, your tx will be broadcast asap About gas, you must set the true value, if lower than it your tx will failed, if too much your balance can't adapt. Just enough
    – CT95
    Commented Feb 28 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.