Last active
June 1, 2022 00:57
-
-
Save Z-M-Huang/572d85252f6be1749aa4b6cec98640d3 to your computer and use it in GitHub Desktop.
[Rinkeby]Solidity Provable Random Number with Chainlink Any API
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
//SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.7; | |
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; | |
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
abstract contract RandomClient is ChainlinkClient, ConfirmedOwner { | |
using Chainlink for Chainlink.Request; | |
uint256 private fee; | |
address oracleAddress; | |
event GetRandomValueFulfilled(bytes32 indexed requestId, string serverSeed, string clientSeed, uint256 nonce, uint256 value); | |
event VerifyRandomValueFulfilled(bytes32 indexed requestId, bool value); | |
constructor() ConfirmedOwner(msg.sender) { | |
//Rinkeby | |
setChainlinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709); | |
oracleAddress = 0x73348Ff4EE038B4d90Ff8659Eb58C1F6Fd1fD64a; | |
setChainlinkOracle(oracleAddress); | |
fee = (1 * LINK_DIVISIBILITY) / 10; //(Varies by network and job) | |
} | |
modifier isOracle() { | |
require(oracleAddress == msg.sender); | |
_; | |
} | |
/** | |
* Allow withdraw of Link tokens from the contract | |
*/ | |
function withdrawLink() public onlyOwner { | |
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress()); | |
require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to transfer'); | |
} | |
function getRandomNumber(string memory seed) internal returns(bytes32) { | |
Chainlink.Request memory req = buildChainlinkRequest( | |
"923c80ed66a841a5905ccc340f48c857", | |
address(this), | |
this.fulfillRandomNumber.selector | |
); | |
req.add("clientSeed", seed); | |
return sendChainlinkRequest(req, fee); // Get Random Number returns requestId | |
} | |
function verifyRandomNumber(string memory serverSeed, string memory clientSeed, | |
uint64 nonce, uint64 value) public returns(bytes32) { | |
Chainlink.Request memory req = buildChainlinkRequest( | |
"2dbb84e91929404bb696f72042957f40", | |
address(this), | |
this.fulfillVerifyNumber.selector | |
); | |
req.add("serverSeed", serverSeed); | |
req.add("clientSeed", clientSeed); | |
req.add("nonce", Strings.toString(nonce)); | |
req.add("value", Strings.toString(value)); | |
return sendChainlinkRequest(req, fee); // Get Random Number returns requestId | |
} | |
/** | |
* Create override function to process additional logic after receiving new random number. | |
* Please do not put heavy logic in callback function. | |
* It will increase the gas price for callback and potentially fail the transaction. | |
*/ | |
function randomNumberReceivedCallback(bytes32 requestId, string memory serverSeed, string memory clientSeed, | |
uint64 nonce, uint64 value) internal virtual; | |
/** | |
* Create override function to process additional logic after verification received. | |
* Please do not put heavy logic in callback function. | |
* It will increase the gas price for callback and potentially fail the transaction. | |
*/ | |
function verifyRandomReceivedCallback(bytes32 requestId, bool value) internal virtual; | |
function fulfillRandomNumber( | |
bytes32 requestId, string memory serverSeed, string memory clientSeed, | |
uint64 nonce, uint64 value) public recordChainlinkFulfillment(requestId) isOracle { | |
emit GetRandomValueFulfilled(requestId, serverSeed, clientSeed, nonce, value); | |
randomNumberReceivedCallback(requestId, serverSeed, clientSeed, nonce, value); | |
} | |
function fulfillVerifyNumber( | |
bytes32 requestId, bool value) public recordChainlinkFulfillment(requestId) isOracle { | |
emit VerifyRandomValueFulfilled(requestId, value); | |
verifyRandomReceivedCallback(requestId, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please see example at https://gist.github.com/Z-M-Huang/3d36d4e48b0d340ac6e36ff7563028e6