4

I am trying to set up a contract that gets an ethereum address from a json file using oraclize, and in turn i'd like to send ether to that address.

function __callback(bytes32 myid, address result) {
  if (msg.sender != oraclize_cbAddress()) throw;
  _address = result;
  if (!_address.send(_withdrawAmount)) throw;
  Withdraw(now, _withdrawAmount, this, _owner);
 }

function withdraw(uint value) {
  _withdrawAmount = value;
  oraclize_query("URL", url);
}

And here is the json

{
  "address" : "0xf519f8bf364fb6bf0185f0bacf19fb8e5c3f134a"
}

I havent been able to send ether in the __callback function and i am not sure how to resolve it.

I am guessing it is because it returns a string, so in the __callback function the type of 'result' would be a string. But how would i convert a string to an address i'd be able to send ether to?

EDIT: In addition to Xavier's answer, I found that I can't send transactions from within the callback function. So i added an executeWithdrawal function that the user would have to call to execute the transaction once the callback has been made from oraclize.

4

You need to look at other answers where parsing JSON strings is discussed. For instance here Parse JSON in Solidity

Then, when you have your string, you can use Oraclize's own helper method:

contract usingOraclize {
    function parseAddr(string _a) internal returns (address);
    ...
}

There https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_pre0.4.sol#L149

  • Hey thanks, that doesnt seem to work either. Whenever i output the result as a string I get the correct Address ("0xf519f8bf364fb6bf0185f0bacf19fb8e5c3f134a") But after doing parseAddr it goes to 0x0000000000000 – TheBreakthrough Jan 30 '17 at 5:36
  • When I try in Browser Solidity, it returns me "0x000000000000000000000000f519f8bf364fb6bf0185f0bacf19fb8e5c3f134a". Which is correct. – Xavier Leprêtre B9lab Jan 30 '17 at 11:25
  • Are you doing something different than this: _address = parseAddr("0xf519f8bf364fb6bf0185f0bacf19fb8e5c3f134a"); – TheBreakthrough Jan 30 '17 at 14:15
  • I am doing the same thing as what you wrote. – Xavier Leprêtre B9lab Jan 30 '17 at 14:20
  • Okay i'm guessing there must be something else somewhere in my code causing that, Ill go ahead and accept this since it solves the original question. Thanks. – TheBreakthrough Jan 30 '17 at 21:34
3

Marco from Oraclize here.

You could also use the parsing helpers we offer, it would reduce the execution gas cost of the callback transaction considerably. An example:

 oraclize_query("URL", json(yourURL).address, callbackGasLimit)

You can do anything you want from the callback transaction, even sending value to other smart contracts or accounts. Just make sure that the callback transaction has enough gas. By changing the callbackGasLimit parameter in oraclize_query, you can specify the amount of gas which will be used by Oraclize when sending the callback transaction. The default value is 200,000 gas.

Also I have noticed a mistake in the arguments declaration of your callback function. The correct ones are:

function __callback(bytes32 myid, string result)

If you have more questions, feel free to contact us on our gitter channel.

  • Thats perfect. Its working how i want it to now. Much appreciated! – TheBreakthrough Mar 9 '17 at 3:49
0

Converting from bytes (or string) to address:

  function bytesToAddress(bytes _address) public returns (address) {
    uint160 m = 0;
    uint160 b = 0;

    for (uint8 i = 0; i < 20; i++) {
      m *= 256;
      b = uint160(_address[i]);
      m += (b);
    }

    return address(m);
  }

Usage

address addr = bytesToAddress("0xa462d983B4b8C855e1876e8c24889CBa466A67EB");

If calling from Remix IDE, pass bytes like this:

["0xa4", "0x62", "0xd9", "0x83", "0xB4", "0xb8", "0xC8", "0x55", "0xe1", "0x87", "0x6e", "0x8c", "0x24", "0x88", "0x9C", "0xBa", "0x46", "0x6A", "0x67", "0xEB"]

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.