In my contract I am just doing a basic query to WolframAlpha

 function testOracle(){
       oraclize_query("WolframAlpha", "random number between 0 and 100");}

I also have the __callback function:

 function __callback(bytes32 myid, uint256 result) {
    if (msg.sender != oraclize_cbAddress()) {throw;}
    randomNum = result;

I have also gained my localhost OAR

 OAR = OraclizeAddrResolverI(.......);

When I do:

 node bridge
 truffle migrate --reset

I get the results from the oracle query in the bridge console:


But whenever I am in the truffle console and do: (randomNum is public)

 var contract = contract.deployed()

I get 0 as the result even though I know the result is 12 in the bridge console as I've seen it. How am i able to retrieve the results from the contract using truffle?


Marco from Oraclize here. There is a mistake in the __callback function declaration. The correct way is the following:

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

I have included as well how you can parse the string to the int, which is a function already included in the oraclizeApi.sol.

If you want a more detail technical explanation you can keep reading :)

When you send a transaction to a contract with the aim of interacting with a function, the function arguments are encoded and send as data in the transaction data field. But before that, you need to specify the function you are going to use, and that is done by prefixing the function signature, also known as methodID. The function signature is constituted by the hex-encoded first 4-bytes of the Keccak-256 hash of the function name and the list of its argument types. Changing one of the argument type results in a different function signature.

You can read more about it here: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI

| improve this answer | |

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.