Skip to content

Instantly share code, notes, and snippets.

@pabloruiz55
Created October 27, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloruiz55/ab79a27f5969576d69b96be58989079a to your computer and use it in GitHub Desktop.
Save pabloruiz55/ab79a27f5969576d69b96be58989079a to your computer and use it in GitHub Desktop.
Oraclize problem
pragma solidity ^0.4.4;
import "./usingOraclize.sol";
contract Raffle is usingOraclize{
function Raffle(){
scheduleRandomNumOnCreation(1000);
}
function() payable {}
function joinRaffle(address _participant){
}
function chooseWinner(uint _chosenNum) internal{
}
//Oraclize random number functions
// the callback function is called by Oraclize when the result is ready
// the oraclize_randomDS_proofVerify modifier prevents an invalid proof to execute this function code:
// the proof validity is fully verified on-chain
function __callback(bytes32 _queryId, string _result, bytes _proof)
{
require (msg.sender == oraclize_cbAddress());
//Before using the random number we have to check if the minimum amount of participants was reached.
//If not, we should abort. LATER, when dealing with money, we should also return funds to the participants.
if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
// the proof verification has failed, do we need to take any action here? (depends on the use case)
} else {
// the proof verification has passed
// for simplicity of use, let's also convert the random bytes to uint if we need
uint maxRange = 10; // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return
uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range
chooseWinner(randomNumber);
}
}
function scheduleRandomNumOnCreation(uint _timeFromNow) internal{
//Called on raffle constructor only
oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof in the constructor
uint N = 4; // number of random bytes we want the datasource to return
uint delay = _timeFromNow; // number of seconds to wait before the execution takes place
uint callbackGas = 200000; // amount of gas we want Oraclize to set for the callback function
bytes32 queryId = oraclize_newRandomDSQuery(delay, N, callbackGas); // this function internally generates the correct oraclize_query and returns its queryId
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment