1

I have a problem when I call the function selectWinner(), send some ether with it to pay for Oraclize. I am using Rinkeby with Infura and metamask for the test accounts. I get an error in the meta mask that says:

Transaction Error. Exception thrown in contract code.

And then the transaction fails.

pragma solidity ^0.4.23;

contract Lottery is usingOraclize {

  uint lotteryFee = 0.1 ether;
  uint8 counter = 0;
  address[10] participants;
  mapping (address => bool) checkParticipant;
  address public owner;
  bytes32 public oraclizeID;
  uint public winnerNumber;


  modifier onlyOnce() {
      require(checkParticipant[msg.sender] == false);
      _;
  }


  function participate() external payable onlyOnce returns(address) {
    require(msg.value == lotteryFee);
    checkParticipant[msg.sender] = true;
    counter = counter + 1;  
    participants[counter] = msg.sender;
    return(participants[counter]);
  }

  function getBalance() public view returns (uint) { 
    uint contractBalance = address(this).balance;
    return(contractBalance);
  }

  function selectWinner() payable public {
    oraclizeID = oraclize_query("WolframAlpha", "random number between 1 and 10");
  }

  function __callback(bytes32 _oraclizeID, string _result){
    if(msg.sender != oraclize_cbAddress()) throw;
    testString = _result;
    winnerNumber = parseInt(_result);
    uint maxRange = counter; 
    uint randomNumber = uint(sha3(_result)) % maxRange; 
  } 

  function payTheReward() external returns(address){
    address winnerAddress = participants[winnerNumber]; 
    uint currentBalance = getBalance();
    winnerAddress.transfer(currentBalance);
    deleteParticipants();
    return (winnerAddress);
  }

  function deleteParticipants() internal {
    uint8 a = counter;
    for(uint8 i = 0; i < a; i++) {
      checkParticipant[participants[i]] = false;
      participants[i] = 0;
    }
    counter = 0;
  }
}

I am using Javascript to call it:

selectTheWinner: function(){
 App.contractInstance.selectWinner(
   { from: App.account,
     value: App.web3.toWei(0.1, "ether")
   }
   ).then(function(addr){ console.log(addr)})
     .catch(function(error){ console.log(error); });

My contract number is: 0xFD05EE7F1660fcaDF5BB618cA5b09e6C564F99cF

2 Answers 2

0

Your contract as-is won't even compile. You first need to remove the undeclared identifier testString under the __callback function.

Secondly, Oraclize only supports up to solc 0.4.20, as there are/may be contract breaking bugs in versions greater than that, that is another change you will want to do.

After having done the following changes, your contract worked just fine for me, having 2 users participate, and then calling selectWinner, which return 1 as a random number from WA.

You will probably want additional checks/modifiers on selectWinner, as someone could drain the contract's funds using that function.

3
  • Thank you for your answer @DenisM, i removed the momentarily to check that was not the problem
    – bhalgalix
    Commented Jun 1, 2018 at 17:31
  • have you tried the lower suggested solc version?
    – DenisM
    Commented Jun 2, 2018 at 18:11
  • yeah.. 1 of the problems was the version of solidity doesn't work with oraclize
    – bhalgalix
    Commented Jun 4, 2018 at 19:43
1

When you say

I have a problem when I call the function,

Which function are you calling and sending value to? function selectWinner() will use address(this).balance to pay for the query.

function participate() will add msg.value to address(this).balance.

Maybe try to add some logs prior to the query as described in the Oraclize docs:

        if (oraclize_getPrice("URL") > this.balance) {
          LogNewOraclizeQuery("Oraclize query was NOT sent, please add 
          some ETH to cover for the query fee");
        } else {
          LogNewOraclizeQuery("Oraclize query was sent, standing by for 
          the answer..");
          oraclize_query(60, "URL", "json(http://api.fixer.io/latest?symbols=USD,GBP).rates.GBP");
        }
1
  • I edited the question to be more specific. Sorry for that
    – bhalgalix
    Commented May 31, 2018 at 19:13

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.