Last active
July 15, 2020 17:45
-
-
Save PatrickAlphaC/7392ca79167d9be0ad5fe8ea1c88af46 to your computer and use it in GitHub Desktop.
Chainlink VRF Sample deployed with: https://remix.ethereum.org/#version=soljson-v0.6.6+commit.d19bba13.js&optimize=false&gist=7392ca79167d9be0ad5fe8ea1c88af46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity 0.6.6; | |
| import "https://raw.githubusercontent.com/smartcontractkit/chainlink/7a4e19a8ff07db1be0b397465d38d175bc0bb5b5/evm-contracts/src/v0.6/VRFConsumerBase.sol"; | |
| contract VRFTestnetD20 is VRFConsumerBase { | |
| uint256[] public d20Results; | |
| bytes32 internal keyHash; | |
| uint256 internal fee; | |
| /** | |
| * @notice Constructor inherits VRFConsumerBase | |
| * @dev Ropsten deployment params: | |
| * @dev _vrfCoordinator: 0xf720CF1B963e0e7bE9F58fd471EFa67e7bF00cfb | |
| * @dev _link: 0x20fE562d797A42Dcb3399062AE9546cd06f63280 | |
| * | |
| * @dev After deploying your contract, remember to fill it with Testnet LINK | |
| * @dev Ropsten faucet: https://ropsten.chain.link/ | |
| */ | |
| constructor(address _vrfCoordinator, address _link) | |
| VRFConsumerBase(_vrfCoordinator, _link) public | |
| { | |
| vrfCoordinator = _vrfCoordinator; | |
| LINK = LinkTokenInterface(_link); | |
| keyHash = 0xced103054e349b8dfb51352f0f8fa9b5d20dde3d06f9f43cb2b85bc64b238205; | |
| fee = 0.1 * 10 ** 18; // 0.1 LINK | |
| } | |
| /** | |
| * @notice Requests randomness from a user-provided seed | |
| * @dev This is only an example implementation and not necessarily suitable for mainnet. | |
| * @dev You must review your implementation details with extreme care. | |
| */ | |
| function rollDice(uint256 userProvidedSeed) public returns (bytes32 requestId) { | |
| require(LINK.balanceOf(address(this)) > fee, "Not enough LINK - fill contract with faucet"); | |
| bytes32 _requestId = requestRandomness(keyHash, fee, userProvidedSeed); | |
| return _requestId; | |
| } | |
| /** | |
| * @notice Modifier to only allow updates by the VRFCoordinator contract | |
| */ | |
| modifier onlyVRFCoordinator { | |
| require(msg.sender == vrfCoordinator, 'Fulfillment only allowed by VRFCoordinator'); | |
| _; | |
| } | |
| /** | |
| * @notice Callback function used by VRF Coordinator | |
| * @dev Important! Add a modifier to only allow this function to be called by the VRFCoordinator | |
| * @dev This is where you do something with randomness! | |
| * @dev The VRF Coordinator will only send this function verified responses. | |
| */ | |
| function fulfillRandomness(bytes32 requestId, uint256 randomness) external override onlyVRFCoordinator { | |
| uint256 d20Result = randomness.mod(20).add(1); // Simplified example | |
| d20Results.push(d20Result); | |
| } | |
| /** | |
| * @notice Convenience function to show the latest roll | |
| */ | |
| function latestRoll() public view returns (uint256 d20result) { | |
| return d20Results[d20Results.length - 1]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment