Random Number Generator
interface ThunderRandomLibraryInterface {
function rand() external returns (uint256);
}pragma solidity ^0.8.9;
interface ThunderRandomLibraryInterface {
function rand() external returns (uint256);
}
contract RandomExampleInterfaceVersion {
event didWin(bool);
uint256 public contractBalance;
// Random precompiled contract address is 0x8cC9C2e145d3AA946502964B1B69CE3cD066A9C7
address constant public randomNumberContractAddress = 0x8cC9C2e145d3AA946502964B1B69CE3cD066A9C7;
ThunderRandomLibraryInterface internal RNGLibrary;
constructor() payable {
contractBalance = uint256(msg.value);
RNGLibrary = ThunderRandomLibraryInterface(randomNumberContractAddress);
}
function betNumber(uint256 bet) payable external returns (bool) {
// block calls from other contracts to prevent "revert transaction unless I won" attacks
require(msg.sender == tx.origin);
if (msg.value < 5) {
contractBalance = contractBalance + msg.value;
emit didWin(false);
return false;
}
uint256 randomNumber = RNGLibrary.rand();
address payable sender = payable(msg.sender);
if (bet < randomNumber) {
sender.transfer(msg.value+1);
emit didWin(true);
contractBalance = contractBalance - 1;
return true;
}
contractBalance = contractBalance + msg.value;
emit didWin(false);
return false;
}
}Last updated