1

I want to create a dynamic JSONpath in an Oraclize query. For that I wrote a function that concentrates three strings (_a = "json(http...",_b = some variable ,_c = ")].Number") to one. Calling the function and getting the concentrated string as a return works fine. How can I now use my concentrated string in the Oraclize query as a variable ?

function strConcat()public view returns (string){

bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
bytes memory _bc = bytes(_c);

string memory abc = new string(_ba.length + _bb.length + _bc.length);
bytes memory babc = bytes(abc);
uint k = 0;

for (uint i = 0; i < _ba.length; i++) babc[k++] = _ba[i];
for (i = 0; i < _bb.length; i++) babc[k++] = _bb[i];
for (i = 0; i < _bc.length; i++) babc[k++] = _bc[i];
return string(babc);
}

Now I want to bring the function return in an Oraclize query to create a dynamic query:

function update() payable {
newOraclizeQuery("Oraclize query was sent...");
oraclize_query('URL', **return** );
}
0

You're concatenating your string correctly, all you need to do now is call that function wherever it is you need it:

 

function update() payable {
    newOraclizeQuery("Oraclize query was sent...");
    oraclize_query('URL', strConcat());
}

 

Please note however that the string concat helper function in the Oraclize API is also called strConcat, which your function name shadows. I would write a differently named function in order to get around that issue.

 


 

Working example:

Here is a rejigged, working version of one of the Oraclize examples to show you that method in use:

 

    pragma solidity ^0.5.0;

    import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

    contract DieselPrice is usingOraclize {

            uint public dieselPriceUSD;

            event LogNewDieselPrice(string price);
            event LogNewOraclizeQuery(string description);

            function __callback(bytes32 myid, string memory result) public {
                    require(msg.sender == oraclize_cbAddress());
                    emit LogNewDieselPrice(result);
                    dieselPriceUSD = parseInt(result, 2); // Let's save it as cents...
                    // Now do something with the USD Diesel price...
            }

            function getQueryString() public pure returns (string memory) {
                    string memory x = "xml(https://www.fueleconomy.gov/ws/rest/fuelprices).";
                    string memory y = "fuelPrices.diesel";
                    return strConcat(x,y);
            }

            function update() public payable {
                    emit LogNewOraclizeQuery("Oraclize query was sent, standing by for the answer...");
                    oraclize_query("URL", getQueryString());
            }
    }

Paste the above code into the Remix IDE, compile it with any compiler version greater than 0.5.0 and deploy the contract. Browse to the settings tab and fire up the Oraclize plugin, before clicking the update function in the run tab once your contract has deployed.

 


 

There are of course many ways to achieve the same thing as I have above - you could also for example set the return of your concatenating function as a variable and then use that in the oraclize_query(...) instead. Feel free to choose whichever method makes most sense to you!

  • 1
    Sometimes it can be so easy haha....thank you a lot. – Max.Velt Dec 21 '18 at 16:16
  • Hahah yes! If only it was always so! – gskapka Dec 21 '18 at 17:53
0

Maybe I'm understanding you wrong. If you mean to store a the return just set a variable.

function update() payable {

    newOraclizeQuery("Oraclize query was sent.");

    string memory x = strConcat();

    oraclize_query('URL', x);

}

It looks to me as long as _b has the value you want, this query would be dynamic.

  • doing it like that gives me the following error: "Variable is declared as storage pointer...". using the storage keyword does not help. – Max.Velt Dec 21 '18 at 13:35
  • For this solution you'd have to declare x as string memory x = //... – gskapka Dec 21 '18 at 17:54

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.