Skip to content

Instantly share code, notes, and snippets.

@a-deal a-deal/ChainFlipper.sol
Last active Oct 25, 2019

Embed
What would you like to do?
pragma solidity ^0.5.0;
import "./provableAPI_0.5.sol";
import "./SafeMath.sol";
/// @title A simulated coin flipper with rewards for winners!
/// @author Andrew Deal
/// @notice You can use this contract to win ETH if you guess heads or tails correctly!
contract ChainFlipper is usingProvable {
using SafeMath for uint;
address owner;
uint constant MAX_INT_FROM_BYTE = 256;
uint constant NUM_RANDOM_BYTES_REQUESTED = 7;
mapping (bytes32 => Flip) private flipsInProgress;
enum Outcomes {
Tails,
Heads
}
struct Flip {
Outcomes prediction;
address sender;
}
/// @param _prediction represents a guess by an account
/// @param _player represents a player's address
event LogPrediction(address indexed _player, Outcomes _prediction, bytes32 _queryId);
/// @param _result represents the coin flip result
/// @param _player represents a player's address
event LogResult(address indexed _player, Outcomes indexed _result);
constructor() public payable {
// require(msg.value > 1 ether, '1 ether initial funding required');
owner = msg.sender;
OAR = OracleAddrResolverI(0x5C545abBfeEacc0AcC74a81D5f07Ca99371617d2);
provable_setProof(proofType_Ledger);
}
function __callback(
bytes32 _queryId,
string memory _result,
bytes memory _proof
) public {
require(msg.sender == provable_cbAddress());
require(flipsInProgress[_queryId].sender != address(0), "Flip query not found");
if (provable_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
revert('Invalid proof while generating random number with Provable');
} else {
uint ceiling = (MAX_INT_FROM_BYTE ** NUM_RANDOM_BYTES_REQUESTED) - 1;
uint randomNumber = uint256(keccak256(abi.encodePacked(_result))) % ceiling;
uint flipResult = (randomNumber % 1);
Flip memory flip = flipsInProgress[_queryId];
emit LogResult(flip.sender, Outcomes(flipResult));
delete flipsInProgress[_queryId];
}
}
/// @notice Takes a player's guess and records it against a generated result
/// @param _prediction a player's prediction
function flip(uint _prediction) external {
uint QUERY_EXECUTION_DELAY = 0;
uint GAS_FOR_CALLBACK = 200000;
bytes32 queryId = provable_newRandomDSQuery(
QUERY_EXECUTION_DELAY,
NUM_RANDOM_BYTES_REQUESTED,
GAS_FOR_CALLBACK
);
emit LogPrediction(msg.sender, Outcomes(_prediction), queryId);
flipsInProgress[queryId] = Flip(Outcomes(_prediction), msg.sender);
}
}
Error: Returned error: VM Exception while processing transaction: revert at PromiEvent (/usr/local/lib/node_modules/truffle/build/webpack:/packages/contract/lib/promievent.js:6:1)
at TruffleContract.flip (/usr/local/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:157:1)
at evalmachine.<anonymous>:0:10
at sigintHandlersWrap (vm.js:257:15)
at Script.runInContext (vm.js:99:14)
at runScript (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:211:1)
at Console.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:226:1)
at ReplManager.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/repl.js:123:1)
at bound (domain.js:396:14)
at REPLServer.runBound [as eval] (domain.js:409:12)
at REPLServer.onLine (repl.js:619:10)
at REPLServer.emit (events.js:182:13)
at REPLServer.EventEmitter.emit (domain.js:442:20)
at REPLServer.Interface._onLine (readline.js:290:10)
at REPLServer.Interface._line (readline.js:638:8)
at REPLServer.Interface._ttyWrite (readline.js:919:14)
at REPLServer.self._ttyWrite (repl.js:692:7)
at ReadStream.onkeypress (readline.js:168:10)
at ReadStream.emit (events.js:182:13)
at ReadStream.EventEmitter.emit (domain.js:442:20)
at emitKeys (internal/readline.js:422:14)
at emitKeys.next (<anonymous>)
at ReadStream.onData (readline.js:1022:36)
at ReadStream.emit (events.js:182:13)
at ReadStream.EventEmitter.emit (domain.js:442:20)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at ReadStream.Readable.push (_stream_readable.js:219:10)
const ChainFlipper = artifacts.require("ChainFlipper");
module.exports = function(deployer, network, accounts) {
deployer.deploy(ChainFlipper, {
from: accounts[0],
gas: 6721975,
value: 500000000000000000
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.