-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from ltfschoen/chainlink
Chainlink
- Loading branch information
Showing
13 changed files
with
636 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.7; | ||
|
||
// Reference: https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFD20.sol&lang=en&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.18+commit.87f61d96.js | ||
import "../vrf/interfaces/VRFCoordinatorV2Interface.sol"; | ||
import "../vrf/VRFConsumerBaseV2.sol"; | ||
|
||
/** | ||
* @notice A Chainlink VRF consumer which uses randomness to mimic the rolling | ||
* of a 20 sided dice | ||
*/ | ||
|
||
/** | ||
* Request testnet LINK and ETH here: https://faucets.chain.link/ | ||
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/ | ||
*/ | ||
|
||
/** | ||
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. | ||
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE. | ||
* DO NOT USE THIS CODE IN PRODUCTION. | ||
*/ | ||
|
||
contract VRFD20 is VRFConsumerBaseV2 { | ||
uint256 private constant ROLL_IN_PROGRESS = 42; | ||
|
||
VRFCoordinatorV2Interface COORDINATOR; | ||
|
||
// Your subscription ID. | ||
uint64 public s_subscriptionId; | ||
|
||
// Sepolia coordinator. For other networks, | ||
// see https://docs.chain.link/docs/vrf-contracts/#configurations | ||
address vrfCoordinator = 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625; | ||
|
||
// The gas lane to use, which specifies the maximum gas price to bump to. | ||
// For a list of available gas lanes on each network, | ||
// see https://docs.chain.link/docs/vrf-contracts/#configurations | ||
bytes32 s_keyHash = | ||
0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c; | ||
|
||
// Depends on the number of requested values that you want sent to the | ||
// fulfillRandomWords() function. Storing each word costs about 20,000 gas, | ||
// so 40,000 is a safe default for this example contract. Test and adjust | ||
// this limit based on the network that you select, the size of the request, | ||
// and the processing of the callback request in the fulfillRandomWords() | ||
// function. | ||
uint32 callbackGasLimit = 40000; | ||
|
||
// The default is 3, but you can set this higher. | ||
uint16 requestConfirmations = 3; | ||
|
||
// For this example, retrieve 1 random value in one request. | ||
// Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS. | ||
uint32 numWords = 1; | ||
address public s_owner; | ||
|
||
// map rollers to requestIds | ||
mapping(uint256 => address) private s_rollers; | ||
// map vrf results to rollers | ||
mapping(address => uint256) private s_results; | ||
|
||
event DiceRolled(uint256 indexed requestId, address indexed roller); | ||
event DiceLanded(uint256 indexed requestId, uint256 indexed result); | ||
|
||
/** | ||
* @notice Constructor inherits VRFConsumerBaseV2 | ||
* | ||
* @dev NETWORK: Sepolia | ||
* | ||
* @param subscriptionId subscription id that this consumer contract can use | ||
*/ | ||
constructor(uint64 subscriptionId) payable VRFConsumerBaseV2(vrfCoordinator) { | ||
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator); | ||
// require(msg.value > 0); | ||
s_owner = msg.sender; | ||
s_subscriptionId = subscriptionId; | ||
} | ||
|
||
/** | ||
* @notice Requests randomness | ||
* @dev Warning: if the VRF response is delayed, avoid calling requestRandomness repeatedly | ||
* as that would give miners/VRF operators latitude about which VRF response arrives first. | ||
* @dev You must review your implementation details with extreme care. | ||
* | ||
* @param roller address of the roller | ||
*/ | ||
function rollDice( | ||
address roller | ||
) public onlyOwner returns (uint256 requestId) { | ||
require(s_results[roller] == 0, "Already rolled"); | ||
// Will revert if subscription is not set and funded. | ||
requestId = COORDINATOR.requestRandomWords( | ||
s_keyHash, | ||
s_subscriptionId, | ||
requestConfirmations, | ||
callbackGasLimit, | ||
numWords | ||
); | ||
|
||
s_rollers[requestId] = roller; | ||
s_results[roller] = ROLL_IN_PROGRESS; | ||
emit DiceRolled(requestId, roller); | ||
} | ||
|
||
/** | ||
* @notice Callback function used by VRF Coordinator to return the random number to this contract. | ||
* | ||
* @dev Some action on the contract state should be taken here, like storing the result. | ||
* @dev WARNING: take care to avoid having multiple VRF requests in flight if their order of arrival would result | ||
* in contract states with different outcomes. Otherwise miners or the VRF operator would could take advantage | ||
* by controlling the order. | ||
* @dev The VRF Coordinator will only send this function verified responses, and the parent VRFConsumerBaseV2 | ||
* contract ensures that this method only receives randomness from the designated VRFCoordinator. | ||
* | ||
* @param requestId uint256 | ||
* @param randomWords uint256[] The random result returned by the oracle. | ||
*/ | ||
function fulfillRandomWords( | ||
uint256 requestId, | ||
uint256[] memory randomWords | ||
) internal override { | ||
uint256 d20Value = (randomWords[0] % 20) + 1; | ||
s_results[s_rollers[requestId]] = d20Value; | ||
emit DiceLanded(requestId, d20Value); | ||
} | ||
|
||
/** | ||
* @notice Get the value rolled by a player once the address has rolled | ||
* @param player address | ||
* @return id as a string | ||
*/ | ||
function getRolledValueForPlayer(address player) public view returns (uint256) { | ||
require(s_results[player] != 0, "Dice not rolled"); | ||
require(s_results[player] != ROLL_IN_PROGRESS, "Roll in progress"); | ||
return s_results[player]; | ||
} | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == s_owner); | ||
_; | ||
} | ||
} |
Oops, something went wrong.