I'm writing a little roulette contract that requires a random number to work. I set up a ChainlinkVRF subscription and am using a slightly modified version of Chainlink's example random number oracle contract (I removed the onlyOwner modifier). The way I set it up, I put out 2 contracts: the random number generator and the roulette game. The roulette game requests random numbers from the random number contract. It isn't working. At first it wasn't working because the transferOwnership() function wasn't working, so I took that out and wrote my own, but it still isn't working. Here's the important parts of the two contracts:
RandomNumber:
modifier verified {
require(msg.sender == owner || msg.sender == approvedContract);
_;
}
function requestRandomWords() verified external returns (uint256 requestId) {
// Will revert if subscription is not set and funded.
requestId = COORDINATOR.requestRandomWords(
keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
s_requests[requestId] = RequestStatus({randomWords: new uint256[](0), exists: true, fulfilled: false});
requestIds.push(requestId);
lastRequestId = requestId;
return requestId;
}
function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
require(s_requests[_requestId].exists, 'request not found');
s_requests[_requestId].fulfilled = true;
s_requests[_requestId].randomWords = _randomWords;
}
function getRequestStatus(uint256 _requestId) external view returns (bool fulfilled, uint256[] memory randomWords) {
require(s_requests[_requestId].exists, 'request not found');
RequestStatus memory request = s_requests[_requestId];
return (request.fulfilled, request.randomWords);
}
And this is the roulette functions:
function setOracleAddress(address _oracle) public {
oracle = _oracle;
o = VRFv2Consumer(oracle);
}
function getRandNum() private {
uint256 id = o.requestRandomWords();
uint num = o.lastRequestId();
require(id == num, "id does not match last request");
(bool fulfilled, uint[] memory randomNumber) = o.getRequestStatus(id);
while(fulfilled != true) {
(fulfilled, randomNumber) = o.getRequestStatus(id);
}
oracleValue = randomNumber[0];
randNumFulfilled = true;
}
There's a few errors I'm getting. The most recent is a gas estimation error. Any ideas on how to fix this?