I'm looking forward to get transaction cost for a particular function call in solidity onChain. I found methods using the web3.estimateGasCost(). But i need is, i should be able to calculate this on-chain. Is there anyway that i can calculate this on chain.

My purpose is like, If someone deposit a amount into my contract, the gas price should be less than 1% of the amount he is depositing, otherwise it should skip some steps.

Thank you


As far as I know - you can't actually estimate the gas from solidity.

Two Related pieces of functionality that might be helpful:

  1. You can potentially use gasLeft(); Global Vars

You can call this function to understand how much gas is left in the transaction execution. You can also call this before and after a function call to understand how much gas that function used.

  1. tx.gasprice is also available as a global variable
  • ,Thank you for replying, but This actually doesn't solve my problem, i want to estimate gas price of a particular function before calling it Mar 16 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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