I have a variable number in my Solidity contract which changes and I need to incorporate this into an oraclize query. My oraclize query looks like the following:

oraclize_query(60, "URL", "json(https://api.random.org/json-rpc/1/invoke).result.random.data.0", '\n{"jsonrpc":"2.0","method":"generateIntegers","params":{"apiKey":${[decrypt]BF8gEz7O4gvAdzdJK/FTWHheGo3TuV5VIFku1rAEG78AxzX6AFEbWJSLZ0sCdEwGzppPsPltUXvIvxNaV5Yvnh3qfmzndRy/nfGjlWESfvNL3S1Fl4y2zqGmGi9DA4D+UokY83Awrw6tZd918KtRAvKe2Bos},"n":1,"min":0,"max":10,"replacement":true,"base":10},"id":1}');

I want the value of max in the above oraclize_query to be number - 1 (instead of 10).

What is the simplest way to do this? I noticed this question How to put a variable inside a string in solidity but the answer is not at all clear to me (I get a lot of errors copy/pasting that code and am not sure how it is meant to be implemented).

I tried using the strConcat function in the Oraclize contract (line 601 at https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.4.sol) by converting my number uint into bytes32 and then a string, then putting it all together, like this:

    string string1 = '\n{"jsonrpc":"2.0","method":"generateIntegers","params":{"apiKey":${[decrypt]BF8gEz7O4gvAdzdJK/FTWHheGo3TuV5VIFku1rAEG78AxzX6AFEbWJSLZ0sCdEwGzppPsPltUXvIvxNaV5Yvnh3qfmzndRy/nfGjlWESfvNL3S1Fl4y2zqGmGi9DA4D+UokY83Awrw6tZd918KtRAvKe2Bos},"n":1,"min":0,"max":';

    string string3 = ',"replacement":true,"base":10},"id":1}';

    function update() payable {

         bytes32 data = bytes32(number - 1);

         strConcat(string1, bytes32ToString(data), string3);

        oraclize_query(60, "URL", "json(https://api.random.org/json-rpc/1/invoke).result.random.data.0", strConcat(string1, bytes32ToString(data2), string3));

This doesn't work (oraclize doesn't get called at all then).

up vote 2 down vote accepted

Marco from Oraclize here. You should use the function uint2string to convert a number to a string.

oraclize_query(60, "URL", "json(https://api.random.org/json-rpc/1/invoke).result.random.data.0", strConcat(string1, uint2str(5), string3));

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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