0

I execute below code and everything works fine until the point in which oraclize has to call __callback function. It doesn't get triggered. I check the status var to understand if it is triggered but without success. Is there something wrong with the code or usage of oraclize?

Thanks.

pragma solidity ^0.4.20;
import "./oraclize.sol";

contract Casino is usingOraclize {
   address owner;
   uint public betAmount = 30 finney; //Equal to 0.01 Ether
   uint public totalBets; //Amount of Ether bet for current game
   uint public numberOfBets; //Total number of current bets
   //uint public maxAmountOfBets = 1 //Maximum amount of bets can be made for each game by each user
   uint public limitAmountOfBets = 3; //Maximum amount of bets for each game
   uint public numberWinner; //Number that won last game
   bool public gameStatus;
   uint public stage = 0;
   mapping(uint => address[]) numberBetPlayers; //Who bet for which number
   mapping(address => uint) playerBetsNumber; //Number bet by each player

   modifier onEndGame() {
       if(numberOfBets >= limitAmountOfBets) _;
   }

   //constructor - set bet amount and max bets in each game
   function Casino(uint _betAmount, uint _limitAmountOfBets) {
       owner = msg.sender; //set creator of contract to owner

       if(_betAmount > 0) betAmount = _betAmount;
       if(_limitAmountOfBets > 0) limitAmountOfBets = _limitAmountOfBets;

       oraclize_setProof(proofType_Ledger); //arge
   }

   //check if player exists in current game
   function checkPlayerExists(address player) constant public returns(bool) {
       if(playerBetsNumber[player] > 0) return true;
       else return false;
   }

   //bet function
   function bet(uint numberToBet) payable {
       require(numberOfBets <= limitAmountOfBets); //check if limit has been reached
       require(!checkPlayerExists(msg.sender)); //check that player hasn't bet before
       require(numberToBet >=1 && numberToBet <= 10); //check that the bet is between 1 and 10
       require(msg.value == betAmount); //check that ether send by sender is equal to bet amount
       gameStatus = true;

       playerBetsNumber[msg.sender] = numberToBet;
       numberBetPlayers[numberToBet].push(msg.sender);

       numberOfBets += 1;
       totalBets += msg.value;

       if(numberOfBets >= limitAmountOfBets) generateNumberWinner();
   }

   //generate random numbers using oraclize_newRandomDSQuery function
   function generateNumberWinner() payable onEndGame {
       gameStatus = false;
       stage = 1;
       uint numberRandomBytes = 7;
       uint delay = 0;
       uint callbackGas = 200000;

       oraclize_newRandomDSQuery(delay, numberRandomBytes, callbackGas);
   }

   function __callback(bytes32 _queryId, string _result, bytes _proof) {
       stage = 2;
       require(msg.sender == oraclize_cbAddress());
       if(oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) { throw; }
       else {
           numberWinner = (uint(keccak256(_result))%10+1);
           distributePrizes();
       }
   }

   function distributePrizes() internal onEndGame {
       stage = 3;
       uint winnerEtherAmount = totalBets / numberBetPlayers[numberWinner].length; //how much each winner will get

       for(uint i = 0; i < numberBetPlayers[numberWinner].length; i++) { //sent ether to addresses
           numberBetPlayers[numberWinner][i].transfer(winnerEtherAmount);
       }

       for(uint j = 1; j <= 10; j++) {
           numberBetPlayers[j].length = 0;
       }

       totalBets = 0;
       numberOfBets = 0;
   }
}
0

There was a typo in __callback :( Problem solved.

0

First,you should check there is no typo mistake.It's a easy mistake to type _callback for __callback function.There is double _,not only single _before callback.It's a Easily neglected!

Second,when you are in Remix, navigate to Settings,look at the Plugin area and click on Oraclize plugin.Only bind remix with the module Oraclize that you can use oraclize. if you are using web3/injected provider,which means you are using Rospten/Kovan testnets or your local private network, you will get the following message:

This Oraclize plugin is currently NOT working in remix via web3/injected provider. This is available for the in-memory mode only (JavaScript VM). If you want to use Oraclize-based contracts, please verify first that Oraclize is integrated with the blockchain you are using - if not you can enable it via a dedicated ethereum-bridge instance

So you can only use Oraclize in JavaScript VM, or you need to use ethereum-bridge for your local private netwokr and testnets like Ropsten.

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.