gnosis / pm-js

Gnosis Prediction Markets JavaScript Library
MIT License
65 stars 22 forks source link

Gas estimation problem buying all tokens #99

Closed anxolin closed 6 years ago

anxolin commented 6 years ago

Hi!

I've followed the SDK tutorial and I think there might be an issue with gas estimation for the butAllTokens operation.

The tutorial I was following was: https://gnosis.github.io/gnosis.js/tutorial-events-oracles-and-markets.html

So I've made a demo web app out of the tutorial:

To reproduce the issue, you can use the demo with metamask connected to Kovan network.

Then follow the steps:

  1. Publish event
  2. Create oracle
  3. Create categorical event
  4. Buy all outcomes

In the buy all outcomes will invoke 3 transactions (deposit, approve, buyAllTokens)

The "buyAllTokens" will fail if you don't increase the gasLimit a little.

Let me know if you need any further information!

Best regards, Anxo

cag commented 6 years ago

Anxo,

Thank you for reporting this! I have reproduced the error, and it is as you have guessed: the gas limit estimation from the core contract layer does not match what is expected at least by MetaMask. With respect to your demo app, unsetting the gas limit default in the console via:

App.gnosis.contracts.CategoricalEvent.defaults({gas:undefined})

delegates the gas estimation to MetaMask, which produces a limit of 278437 if the account has not done this before and 300937 if it has.

After the buyAllOutcomes transaction goes through, Ganache says the following about it:

(for an account that has done this flow before ???)

  Transaction: 0x0a7be339a0708f06dbc7b36ed4be1573d66f0ccc68d7ba7eba118d15dbdc3028
  Gas usage: 200625
  Block Number: 49
  Block Time: Mon Feb 26 2018 14:12:52 GMT-0600 (CST)

(for a system that is completely uninitialized ?????? I really don't know for sure why there are three cases here to be completely honest)

  Transaction: 0xa0abac14cb99e4a8000dfc8930e8e9099321f616edc9aba1c644b11f59623c20
  Gas usage: 140625
  Block Number: 39
  Block Time: Mon Feb 26 2018 13:59:33 GMT-0600 (CST)

(for an account that is new to this flow)

  Transaction: 0x9efb0a26a6fb10217cd76068ab144eb8159a3452eec973a6ff93352516ae4527
  Gas usage: 185625
  Block Number: 54
  Block Time: Mon Feb 26 2018 14:27:34 GMT-0600 (CST)

All of these are more than the built-in default of 136569, which is calculated as 1.5 times the maximum gas cost as reported by the core contracts gas measurements layer found in App.gnosis.contracts.CategoricalEvent.gasStats.

cag commented 6 years ago

Okay, this doesn't explain everything, but fixing this would somewhat alleviate this issue (except for the highest value of ~300k):

https://github.com/gnosis/gnosis-contracts/issues/90

cag commented 6 years ago

Feel free to reopen this if the issue was not solved by #100 and has nothing to do with #101