0

I have a contract that uses Oraclize to query some bits of data. Basically, I have the following three functions:

  1. getData() - calls Oraclize contract to query data,
  2. __callback() - called automatically by Oraclize, when data received.
  3. setData() - function to get the queried value and do smth.

    contract functionChain is usingOraclize {
    
    string public globalKey;
    
    function getData() public payable {
        string memory query = 'json(http://QUERY_LINK)'; //returns string
        oraclize_query(0, "URL", query, 250000);
    }
    
    function setData(string text) internal {
        text = globalKey;
    }
    
    function __callback(bytes32 myId, string result) {
        if (msg.sender != oraclize_cbAddress()) revert();
    
        setData(result);
    }
    }
    

The logic I have now is the following: 1. getData() is called 2. __callback() { setData() } - setData() is called by the __callback() function.

The problem is, that even though I can successfully query the data via Oraclize, setData() function is never being called. I am getting the logs of success only for the getData() function.

Is this even a legit way to use Oraclize query result in a third function? If not, what would be the best way of linking those functions together, so that SetData() is called after the __callback()?

2

Try this:

pragma solidity ^0.4.24;
import './usingOraclize.sol';

contract functionChain is usingOraclize {
    string public globalKey;
    mapping (bytes32 => bool) validIds;

    event LogResult(string result);
    event LogNewOraclizeQuery(string description);
    event LogFundsReceived(address sender, uint amount);

    constructor() public payable {
        OAR = OraclizeAddrResolverI(your oraclize address);
    }

    function() payable {
        LogFundsReceived(msg.sender, msg.value);
    }

    function __callback(bytes32 myid, string result) public {
        require(validIds[myid]);
        require(msg.sender == oraclize_cbAddress());
        globalKey = result;
        LogResult(result);
        delete validIds[myid];
    }

    function setData() public payable {
        if (oraclize_getPrice("URL") > this.balance) {
            LogNewOraclizeQuery("Oraclize query was NOT sent");
        }
        else {
            LogNewOraclizeQuery("Oraclize query was sent");
            bytes32 queryId = oraclize_query("URL", "json(http://QUERY_LINK)");
            validIds[queryId] = true;
        }
    }
}
| improve this answer | |

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.