Skip to content

Instantly share code, notes, and snippets.

@akshatamohanty
Last active December 9, 2022 19:21
Show Gist options
  • Save akshatamohanty/b0e9b4aebaabfd37266c047f4fbaa4d1 to your computer and use it in GitHub Desktop.
Save akshatamohanty/b0e9b4aebaabfd37266c047f4fbaa4d1 to your computer and use it in GitHub Desktop.
[Practical Blockchain] Oracles 101: Lottery Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol";
contract Lottery is VRFConsumerBaseV2, ConfirmedOwner {
event RequestSent(uint256 requestId, uint32 numWords);
event RequestFulfilled(uint256 requestId, uint256[] randomWords);
VRFCoordinatorV2Interface COORDINATOR;
// Your subscription ID.
uint64 s_subscriptionId;
// 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/v2/subscription/supported-networks/#configurations
bytes32 keyHash =
0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15;
// Depends on the number of requested values that you want sent to the
// fulfillRandomWords() function. Storing each word costs about 20,000 gas,
// so 100,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 = 100000;
// The default is 3, but you can set this higher.
uint16 requestConfirmations = 3;
// For this example, retrieve 2 random values in one request.
// Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS.
uint32 numWords = 1;
// past requests Id.
uint256 public lastRequestId;
uint256 randomNumber;
// Lottery-related
address public manager;
address payable public winner;
address[] public players;
/**
* HARDCODED FOR GOERLI
* COORDINATOR: 0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D
*/
constructor(
uint64 subscriptionId
)
VRFConsumerBaseV2(0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D)
ConfirmedOwner(msg.sender)
{
COORDINATOR = VRFCoordinatorV2Interface(
0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D
);
s_subscriptionId = subscriptionId;
manager = msg.sender;
}
// Assumes the subscription is funded sufficiently.
function requestRandomness()
private
returns (uint256 requestId)
{
// Will revert if subscription is not set and funded.
requestId = COORDINATOR.requestRandomWords(
keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
lastRequestId = requestId;
emit RequestSent(requestId, numWords);
return requestId;
}
function fulfillRandomWords(
uint256 _requestId,
uint256[] memory _randomWords
) internal override {
// transform the result to a number between 1 and 20 inclusively
randomNumber = (_randomWords[0] % players.length);
payWinner();
emit RequestFulfilled(_requestId, _randomWords);
}
function payWinner() private {
winner = payable(players[randomNumber]);
winner.transfer(address(this).balance);
}
// function random() private view returns (uint) {
// return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
// }
function pickWinner() public returns (uint256 requestId) {
// uint index = random() % players.length;
// winner = payable(players[index]);
// winner.transfer(address(this).balance);
return requestRandomness();
}
function enter() public payable {
require(msg.value >= .001 ether);
players.push(msg.sender);
}
function getPlayers() public view returns (address[] memory) {
return players;
}
function getWinner() public view returns (address) {
return winner;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment