Skip to content

Instantly share code, notes, and snippets.

@ttpro1995
Created November 13, 2021 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttpro1995/0c88fab30319308da5c1e5fee54ad081 to your computer and use it in GitHub Desktop.
Save ttpro1995/0c88fab30319308da5c1e5fee54ad081 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.10+commit.fc410830.js&optimize=true&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
/**
* 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/
*/
contract RandomNumberConsumer is VRFConsumerBase {
bytes32 internal keyHash;
uint256 internal fee;
// uint256 public randomResult;
mapping(uint => bytes32) public mapIncrementToRequestId;
mapping(bytes32 => uint256) public mapRequestIdToResult;
uint public currentIncrement = 1;
/**
* Constructor inherits VRFConsumerBase
*
* Network: Kovan
* Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
* LINK token address: 0xa36085F69e2889c224210F603D836748e7dC0088
* Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
*/
constructor()
VRFConsumerBase(
0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
0xa36085F69e2889c224210F603D836748e7dC0088 // LINK Token
)
{
keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network)
}
/**
* Requests randomness
*/
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
requestId = requestRandomness(keyHash, fee);
mapIncrementToRequestId[currentIncrement] = requestId;
currentIncrement += 1;
return requestId;
}
/**
* Callback function used by VRF Coordinator
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
mapRequestIdToResult[requestId] = randomness;
}
function withdrawLink() public {
address chainlinkTokenAddress = 0xa36085F69e2889c224210F603D836748e7dC0088;
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress);
link.transfer(msg.sender, link.balanceOf(address(this)));
}
function viewRawGenerateResult(uint incrementId) public view returns(uint256 random) {
bytes32 requestId = mapIncrementToRequestId[incrementId];
random = mapRequestIdToResult[requestId];
return random;
}
function viewCoinToss(uint incrementId) public view returns(string memory resultString) {
uint256 rawRandom = viewRawGenerateResult(incrementId);
resultString = "TAIL";
if (rawRandom % 2 == 0){
resultString = "HEAD";
}
return resultString;
}
function viewAllCoinToss() public view returns(string memory resultString){
resultString = "";
for (uint i = 1; i < currentIncrement; i++) {
string memory tmp = string(bytes.concat(bytes(resultString), "-", bytes(viewCoinToss(i))));
resultString = tmp;
}
return resultString;
}
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment