[Ethereum] How to estimate gas limit for “payable” smart contract

gas-limitweb3js

We all know that gas limit for EOA to EOA transfer is 21000.
I am got stuck at a point and unable to get dynamic gas limit estimation for "payable" smart contract using Web3.

When sending Ether from EOA to some external smart contract then facing "out of gas" error, because Web3's function estimateGas()

web3.eth.estimateGas({ 
  to: EOA/Smart Contract address,
  value: web3.utils.toHex(ethAmount)
})

always return 21000, regardless of the "to" address whether its EOA or Smart contract.

Could you please help me figure out this issue.

Thanks.

Best Answer

Someone correct me if I'm wrong, but 21000 is not the gas limit. Remember that the gas limit is what the sender is willing to pay at most for the execution of the transaction. On the other hand, each opcode carries a gas price as you can see in the picture. Then the cost of performing a simple transaction is 21000, but if more opcodes are executed, the gas price of the transaction will increase (the prices of each opcode vary over time).

opcode prices

Related Topic