0

I've had an issue with my first solidity contract whereby sometimes transactions will work and other times I'll get "out of gas" errors. I believe after much research I've pinpointed this problem to the contract making extensive use of block.timestamp and "now" for pseudo-RNG purposes (which I'm aware of the limitations). Such function calls I hear make the exact gas required highly unpredictable.

Unfortunately during my testing on Ropstein, Metamask seems to fail about 30% of the time in predicting the correct amount of gas, and I run out of gas. I get no such errors when testing on testrpc or JavaVM (since obviously gas isn't an issue here).

I've thought that if I could somehow code in solidity a way to tell metamask that it should send a fixed amount "extra" gas, or if I could even define the exact gas amount to send, that these issues might be avoidable. If this isn't possible, how do other dapps get around using block.timestamp and metamask without running into these out of gas errors sometimes?

It's absolutely NOT a solution that the client must manually change their gas for my contract, since the dapp is designed to work flawlessly for every user. A lot of consumer engagement will no doubt be lost if I require my users to specify the gas limit in metamask themselves.

Is there any way around this issue whereby the user doesn't have to do anything?

2

I believe after much research I've pinpointed this problem to the contract making extensive use of block.timestamp and "now" for pseudo-RNG purposes (which I'm aware of the limitations). Such function calls I hear make the exact gas required highly unpredictable.

Indeed, any change in the code executed based on some external factor (timestamp, block hash, tx hash) can result in bad gas estimates, since the code run while estimating might differ from the code run while executing.

Is there any way around this issue whereby the user doesn't have to do anything?

solc will attempt to provide you with a max gas estimate when you compile your code. You should set this as the max gas explicitly before passing the tx to metamask. In some cases, solidity may not be able to estimate the max gas at all (such as when looping over arrays), and you must calculate the maximum possible gas yourself by looking at what the code does.

  • Thank you. How would I go about specifying the max gas with solc specifically? – user4779 Nov 3 '18 at 10:45
  • You can pass the --gas to solc when compiling to have it give gas estimates – Raghav Sood Nov 3 '18 at 11:11

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.