smart contract not returning random numbers. please help. #5111
Answered
by
alymurtazamemon
ayush28yadav
asked this question in
Q&A
-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AutomationCompatibleInterface.sol";
/**
* @title Roll a Dice
* @dev implements chainlink vrf 2
* @author Ayush Yadav
*/
contract RollaDice is VRFConsumerBaseV2, AutomationCompatibleInterface {
//global variables
// Chainlink VRF Variables
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
uint64 private immutable i_subscriptionId;
bytes32 private immutable i_gasLane;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 2;
//game variables
uint256 private immutable i_targetscore= 10;
uint256 private player1score = 0;
uint256 private computerscore = 0;
bool private clicked = false;
//events
event WinnerPicked(string indexed winner);
//functions
constructor(
) VRFConsumerBaseV2(0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D) {
i_vrfCoordinator = VRFCoordinatorV2Interface(0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D);
i_gasLane = 0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15;
i_subscriptionId = 10035;
i_callbackGasLimit = 100000;
}
/* @dev This is the function that the Chainlink Keeper nodes call
* they look for `upkeepNeeded` to return True.
* the following should be true for this to return true:
* 1. clicked should be true
*/
function checkUpkeep(
bytes memory /* checkData */
)
public
view
override
returns (bool upkeepNeeded, bytes memory /* performData */)
{
upkeepNeeded = (clicked);
return (upkeepNeeded, "0x0");
}
/**
* @dev Once `checkUpkeep` is returning `true`, this function is called
* and it kicks off a Chainlink VRF call to get a random winner.
*/
function performUpkeep(bytes calldata /* performData */) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
}
/**
* @dev This is the function that Chainlink VRF node
* calls to get numbers
*/
function fulfillRandomWords(
uint256 /* requestId */,
uint256[] memory randomWords
) internal override {
player1score += randomWords[0] % 6+1;
computerscore += randomWords[1] % 6+1;
clicked = false;
if (player1score >= i_targetscore) {
emit WinnerPicked("you win");
player1score=0;
computerscore=0;
} else if (computerscore >= i_targetscore) {
emit WinnerPicked("computerwins");
player1score=0;
computerscore=0;
}
}
//view functions
function gettargetscore() public pure returns (uint256) {
return i_targetscore;
}
function getplayer1score() public view returns(uint256){
return player1score;
}
function getcomputerscore() public view returns (uint256){
return computerscore;
}
function click() public{
clicked=true;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
alymurtazamemon
Mar 17, 2023
Replies: 1 comment 3 replies
-
@ayush28yadav You are using goerli configurations, please check out this announcement #5079 |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
alymurtazamemon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ayush28yadav You are using goerli configurations, please check out this announcement #5079