0

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
                .createSector(
              { value: web3.utils.fromWei(price, 'ether') }
            )
            .encodeABI(),
        },
        privateKey
      );

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?

Joey Yi Zhao
  • 284
  • 1
  • 11

1 Answers1

0

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

CT95
  • 388
  • 1
  • 9
  • 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? – Joey Yi Zhao Feb 27 '24 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 Feb 27 '24 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? – Joey Yi Zhao Feb 27 '24 at 10:45
  • Gas and gasPrice are the not same unit of measure.

    You can see here: https://ethereum.stackexchange.com/questions/54606/what-is-difference-between-gas-gas-price-and-fee

    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 Feb 28 '24 at 02: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 Feb 28 '24 at 02:50
  • Thanks for the explaination. just enough is something very hard to hit – Joey Yi Zhao Feb 28 '24 at 02:57
  • My english is not good to explain too much, but I'll give a try.

    very hard to hit, in my opinion it's not.

    I will give you an example in hardhat: your tx is contractA.deposit(). You can get estimation by contractA.estimateGas.deposit() => 80.000 gas

    You can use that value in gas. But for safety, you don't want your tx fail because of over gas limit, you can increase 80.000 * 3% = 82400.

    You can see in every single tx on etherscan, see the value at Gas Limit & Usage by Txn: Gas Limit is 82400 and usage by txn may 80020 (or any value)

    – CT95 Feb 28 '24 at 03:06
  • Your a% is not too much, because if you want to set too much, your client address must adapt that balance. So you have to optimize that value due to your expectation. Like if your common transaction is around 2$, 3$. 10% 20% is not too much,

    If your common transaction is around 1000$, 1500$. 10% 20% is too much, 3%, 5% is reasonable

    – CT95 Feb 28 '24 at 03:11
  • Your english is pretty straight forward to understand. Based on my understanding, the real gas I pay is between the gasPrice and gas limit right? – Joey Yi Zhao Feb 28 '24 at 11:20
  • No. your real gas you used is a number around your estimation and must <= gas limit

    And your real fee you pay in ETH is: gas used * gas price = ... ETH

    The unit gas is: wei or gwei (1e9 wei) The unit gas fee is: ETH

    – CT95 Feb 29 '24 at 04:31