-1
pragma solidity ^0.4.17;

import "installed_contracts/oraclize-api/contracts/usingOraclize.sol";

contract DevCoin is usingOraclize {

    mapping (address => uint256) public balances;

    event Transfer(address from, address to, uint256 value);

    uint256 public z;

    event newOraclizeQuery(string description);

    function __callback(bytes32 myid, string result) {
        if (msg.sender != oraclize_cbAddress()) throw;
         // z = result;
    }

    function DevCoin() payable public {
        balances[msg.sender] = z;
        OAR = OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475);
        //update();
    }

    function update() payable {
         newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
         oraclize_query("URL", "json(https://urbangold.localtunnel.me).z");
    }

    function sendCoin(address receiver, uint256 amount) payable public returns (bool) {
        if (balances[msg.sender] < amount) return false;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        Transfer(msg.sender, receiver, amount);
        return true;
    }

    function getBalance(address addr) public view returns (uint256) {
        return balances[addr];
    }
}

enter image description here

2
  • Could you clarify what it is you want to achieve? So far you have correctly implemented Oraclize though your callback does nothing. What network are you attempting to deploy this on? If it's a public network, any query to your local host will fail since the URL endpoint needs to be one that Oraclize can reach with an https call.
    – gskapka
    Dec 27, 2018 at 19:05
  • @GregKapka I use localhost 8545 on a private network with metamask. The Thing is I made a restfull API on my own on a localhost with a specific number for example 150 represented by the variable z. This variable should go into the DevCoin function balances[msg.sender] and should be displayed as: You have 150 DVC.
    – Jolosin
    Dec 29, 2018 at 12:50

1 Answer 1

1

You contract & Oraclize implementation looks good so far, however your __callback function isn't doing anything since you've commented out the line which sets the contract's global variable z.

Your query is attempting to parse a JSON, plucking the z key's value from it, and so your API must return a JSON that suits this format:

{
    // ...
    "z": 150
    // ...
}

Then, by uncommenting this line here:

// z = result;

and calling the update() function, you can make an Oraclize query which - when it calls back - will update your z varible to whatever is in the JSON your API returns.

 


 

Looking further at your query, you are using a local tunnel to expose your API. This means that Oraclize can only query that URL whilst you're running your server.

Another option you may wish to choose is to develop locally using Truffle & the Ethereum-Bridge (which can be installed via npm - details here). This way you can make queries directly from your local host for development purposes if needs be.

Oraclize have a bunch of example repos here where you can see how to use Truffle with Oraclize & the Ethereum-Bridge for developing your smart contracts in a local environment.

 


 

Edit:

To answer your further question of how to parse your result z into a uint, luckily the Oraclize contract you inherited into your own has a helper method to do just that! Make your __callback function look like so:

function __callback(bytes32 myid, string result) {
    if (msg.sender != oraclize_cbAddress()) throw;
    z = parseInt(result);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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