I am making a lottery contract and using Oraclize to select a winning number.

Here is what I have now:

The owner calls a finalize() function, which disallows new entries, and makes a call to Oraclize (using WolframAlpha as data source). The oracle then calls my __callback function with the result, and my contract uses that to find the winner(s), distribute the winnings, and reset the contract to accept entries again. This operation involves loops, and takes steps proportional to the number of entries. Since the number of entries is not limited, I believe this operation's gas usage is not bounded.

I have thought about these options:

  • Estimating the gas usage somehow, and specifying that in the oraclize call.
  • Removing logic from the callback function, and only using it to set the winning number. The owner then has to call a distribute function to do the calculation-heavy steps.

I find both options problematic. It seems to me that oraclize callback functions were intended to be kept simple. However, I would like to avoid the owner having to call the contract twice (this second option also raises the problem of a dishonest owner not calling the distribute function when he sees the winning number).

Is there a common pattern for this type of usage? What is the correct way to go about this?

Thank you in advance.

1 Answer 1


The correct and safer way, would quite honestly be the second option you have thought of.

You can also try to estimate the gas price ahead of time and then set that amount to be included in the callback. Oraclize allows you to set whatever amount you need, although you'll of course want to stay within the network gas limits, else the transaction will never be accepted. There's also a risk with unforeseeable/unbound loops always, although one way to mitigate it is that you could potentially have the loop watch for whether it's about to run out of gas, and store whatever it has done in state, for you to continue resolving manually.

Here's some documentation regarding setting the gas limit: https://docs.oraclize.it/#ethereum-quick-start-custom-gas-limit-and-gas-price

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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