2

I have very strange experience with Oracle. When I try to add functionality inside of callback, callback is not called, but when I delete functionality callback works without problem.

I tried to increase gas amount using oraclize_setCustomGasPrice, use mapping, arrays and anything I know in functionality, nothing works.

Here are example of callback which is never called:

function __callback(bytes32 myid, string result) {
  if (msg.sender != oraclize_cbAddress()) revert();
  emit LogPriceUpdated(result);
 for (uint i=0; i < betId + 1; i++) {
    if (compareStrings("up", result)) {
        gameInfo[currentPrizePeriodTime][i].choice = result;
  }
 }
}

And here is example of code which is called all the time:

function __callback(bytes32 myid, string result) {
  if (msg.sender != oraclize_cbAddress()) revert();
    emit LogPriceUpdated(result);
  }
}

Does any of you why this happens, is it solvable or I should look for workarounds?

2

Oraclize employee here, and looking at your code with a __callback I'm pretty sure the Oracle you are referring to is indeed Oraclize, so hopefully I can help! :p

 

Without seeing the rest of your smart-contract I can't diagnose for sure what is stopping your __callback from running, though I can certainly make some educated guesses. It is almost certainly failing due to out-of-gas errors since you're using both strings & a loop inside it. Any string manipulation is very expensive in solidity in terms of gas, and looping over some form of string manipulation is will multiply that cost for each time the loop turns.

 

When you call an Oraclize query you have the option of supplying an amount of gas to the query, which amount is taken from your contract at the time you make the call. (This is different to the setCustomGasPrice that you've mentioned) Oraclize will then supply this amount to power the transaction that runs your __callback. If you don't supply this parameter, it defaults to 200,000. That's plenty to run your __callback with most of the logic removed, but unlikely to be enough depending on how long your string result is & how many times you iterate over it with in the loop.

 

To supply more gas to the Oraclize query, add the number as a param thusly:

     oraclize_query("URL", queryString, gasAmount);

where gasAmount is a uint of how much gas you wish to supply & the queryString is whatever you're currently using to make your queries. This way you can add more gas to the transaction and get the __callback to go through when it includes your logic.

 

However, the bigger issue is that you're trying to do a lot of expensive stuff in your __callback, and the name of the game in Solidity is tying to make everything as efficient as possible since it all costs money to run. It's probably worth you having a look at your current architecture & see where you can make gas-cost savings in what you do with the results of your Oraclize query.

| improve this answer | |
  • Also for new developers using Oraclize like me, firstly make sure you really do not have nulls in your callback, like I did ^^ – Tomas Maksimavicius Sep 9 '18 at 17:58
  • 1
    @TomasMaksimavicius yes! :P It's often a good idea to "mock" the callback by pulling it out into it's own function and using say the Truffle testing framework to call your mock with the result you expect to receive from Oraclize. Decoupling it this way lets you test that the logic is working correctly separate to the Oraclize query itself. – gskapka Sep 10 '18 at 8:10
  • Have the same issue... Will it make any difference to put all the logic in an internal function and call it from the __callback()? – Ruham Oct 15 '18 at 19:20
  • @Ruham - it makes no difference whatsoever. So long as the logic inside the internal function has no errors, and you provide necessary gas price to the Oraclize call to power that callback, you can arrange your logic the way you describe with no problems. – gskapka Oct 17 '18 at 8:00

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.