0

I'm trying to do a simple coin flip using WolframAlpha and Oraclize, this is my smart contract

pragma solidity ^0.4.18;
import './usingOraclize.sol';

contract CoinFlipOracle is usingOraclize {

  string public result;
  bytes32 public oraclizeID;

  event Log(string text);

  function CoinFlipOracle() {
    Log("Contract created");
  }

  function flipCoin() payable {
    Log("Oraclize query was sent, waiting for response");
    oraclizeID = oraclize_query("WolframAlpha", "flip a coin", 5000000);
  }

  function __callback(bytes32 _oraclizeID, string _result) {
    require(msg.sender == oraclize_cbAddress());
    Log(_result);
    result = _result;
  }

  function getResult() view returns(string) {
    return result;
  }
}

On my frontend, I deploy contract, then I call flipCoin(), and finally I call getResult()

async flipCoin() {
  let contractInstance = await CoinFlipContract.at(this.state.contractAddress);
  await contractInstance.flipCoin();
  let result = await contractInstance.getResult();
  console.log(result);
}

I'm trying to return either "heads" or "tails", but all it returns is the txhash plus some other info. Am I using Oraclize correctly?

1 Answer 1

0

Riccardo from Oraclize here. Hope it is not too late.

I just tested your contract and the method flipCoin() works.

pragma solidity ^0.4.18;

import "./oraclize/usingOraclize.sol";


contract CoinFlipOracle is usingOraclize {

    string public result;
    bytes32 public oraclizeID;

    event Log(string text);

    function CoinFlipOracle() {
        Log("Contract created");
    }

    function flipCoin() payable {
        Log("Oraclize query was sent, waiting for response");
        oraclizeID = oraclize_query("WolframAlpha", "flip a coin", 5000000);
    }

    function __callback(bytes32 _oraclizeID, string _result) {
        require(msg.sender == oraclize_cbAddress());
        Log(_result);
        result = _result;
    }

    function getResult() view returns(string) {
        return result;
    }
}

I tested it in .js, and the test is returning me "heads" as it should. You have to be completely sure that your callback returns.

contract('CoinFlipOracle.flipCoin()', accounts => {
    const owner = accounts[0]
    const issuer1 = accounts[1]

    it("Should check if flipCoin() works", async () => {
        console.log("owner: ", owner);
        console.log("issuer1: ", issuer1);
        const coinFlipOracle = await CoinFlipOracle.deployed()
        let result = await coinFlipOracle.result.call({ from: issuer1 })
        console.log("result: ", result);
        await coinFlipOracle.flipCoin({ from: issuer1, value: web3.toWei(2, 'ether') })
        wait(20000); // **** WAIT FOR THE CALLBACK TO RETURN ***
        result = await coinFlipOracle.result.call({ from: issuer1 })
        console.log("result: ", result);
    })

})

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.