2

I am learning to use Oraclize to connect it to random.org to generate random numbers.

Here's my test contract code:

pragma solidity ^0.4.25;

import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
import "github.com/willitscale/solidity-util/lib/Strings.sol";
import "github.com/willitscale/solidity-util/lib/Integers.sol";
import "github.com/willitscale/solidity-util/lib/Addresses.sol";

contract Oraclize is usingOraclize {

    using Strings for string;
    using Integers for uint;
    using Addresses for address;

    string public randomNumbers;
    uint[] public randomNumbersArray;

    event newOraclizeQuery(string description);
    event randomNumbersGenerated(string randomNumbers);

    constructor() public {
        update();
    }

    function __callback(bytes32, string result) public {
        if (msg.sender != oraclize_cbAddress()) revert();
        randomNumbers = result;

        string[] storage split = result.split(" ");

        for (uint i = 0; i < split.length; i++) {
            randomNumbersArray.push(parseInt(split[i]));
        }

        emit randomNumbersGenerated(randomNumbers);
    }

    function update() public payable {
        emit newOraclizeQuery("Loading new set of random numbers, standing by for the answer...");
        oraclize_query('URL', '...');
    }

}

I've tested the above on http://remix.ethereum.org via Rinkeyby testnet. I deployed the contract and then called the update() function and then checked on both of the public fields and they were empty / 0:

enter image description here

What am I doing wrong here?

When I take out the random number splitting by space part of the code, the callback then start working.

enter image description here

Any ideas?

0

I've used a different solidity utils library + the WolframAlpha alpha api to solve it like this:

pragma solidity ^0.4.25;

import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract OraclizeTest is usingOraclize {

    using strings for *;

    string public randomNumbers;
    bytes32 public loadNewRandomNumbersQueryId;

    event NewRandomNumbers(string _randomNumbers);

    function loadNewRandomNumbers() public payable {
        loadNewRandomNumbersQueryId = oraclize_query("WolframAlpha", "10 unique random numbers between 0 and 53");
    }

    function __callback(bytes32 _queryId, string _result) public {
        require(_queryId == loadNewRandomNumbersQueryId, "Oraclize Query Id Does Not Match");
        require(msg.sender == oraclize_cbAddress(), "Invalid Oraclize Callback Address");

        if (_queryId == loadNewRandomNumbersQueryId) {
            randomNumbers = _result;
            emit NewRandomNumbers(randomNumbers);
        }
    }

    function parseRandomNumbers() public view returns (uint[] _randomNumbersArray) {
        strings.slice memory s = randomNumbers.toSlice();
        strings.slice memory delim = ",".toSlice();
        uint[] memory parts = new uint[](s.count(delim) + 1);
        for (uint i = 0; i < parts.length; i++) {
            parts[i] = parseInt(s.split(delim).toString());
        }
        return parts;
    }

}

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.