Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pragma solidity ^0.6.0;
import "github.com/provable-things/ethereum-api/provableAPI_0.6.sol";
contract RecursiveRandomSource is usingProvable {
uint256 constant ROUND_TIME = 60;
uint256 constant MAX_INT_FROM_BYTE = 256;
uint256 constant NUM_RANDOM_BYTES_REQUESTED = 7;
uint public random;
event Error(string message);
event Log(string message);
event LogNewProvableQuery(bytes32 queryId);
constructor()
public
payable
{
provable_setProof(proofType_Ledger);
update();
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function __callback(
bytes32 _queryId,
string memory _result,
bytes memory _proof
)
public
override
{
require(msg.sender == provable_cbAddress());
if (
provable_randomDS_proofVerify__returnCode(
_queryId,
_result,
_proof
) != 0
) {
emit Error("Callback verification failed");
} else {
uint256 ceiling = (MAX_INT_FROM_BYTE ** NUM_RANDOM_BYTES_REQUESTED) - 1;
random = uint(keccak256(abi.encodePacked(_result))) % ceiling;
update();
}
}
function update()
payable
public
{
if (address(this).balance >= provable_getPrice("random")) {
uint256 GAS_FOR_CALLBACK = 300000;
bytes32 queryId = provable_newRandomDSQuery(
ROUND_TIME,
NUM_RANDOM_BYTES_REQUESTED,
GAS_FOR_CALLBACK
);
emit LogNewProvableQuery(queryId);
} else {
emit Error("Not enough Ether");
}
}
}
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.