Lesson 9: One Unit Test Failing (Doesn't allow entering raffle when Calculating) #3545
-
Hi, I am facing an issue with getting one unit test passed (Doesn't allow entering raffle when Calculating) let raffle, vrfCoordinatorV2Mock, chainId, raffleEntranceFee, deployer, interval
chainId = network.config.chainId
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer
await deployments.fixture("all")
raffle = await ethers.getContract("Raffle", deployer)
vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock", deployer)
raffleEntranceFee = await raffle.getEntranceFee()
interval = await raffle.getInterval()
})
describe("enterRaffle", async function () {
it("doesn't allow entrance when raffle is calculating", async () => {
await raffle.enterRaffle({ value: raffleEntranceFee })
// for a documentation of the methods below, go here: https://hardhat.org/hardhat-network/reference
await network.provider.send({
method: "evm_increaseTime",
params: [interval.toNumber() + 1],
})
await network.provider.send({ method: "evm_mine", params: [] })
// we pretend to be a keeper for a second
await raffle.performUpkeep([]) // changes the state to calculating for our comparison below
await expect(raffle.enterRaffle({ value: raffleEntranceFee })).to.be.revertedWith(
"Raffle__NotOpen"
)
})
}) Raffle.sol // 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/interfaces/KeeperCompatibleInterface.sol";
error Raffle__NotEnoughETHEntered();
error Raffle__TransferFailed();
error Raffle__NotOpen();
error Raffle__UpkeepNotNeeded(uint256 currentBalance, uint256 numPlayers, uint256 raffleState);
/**
* @title Raffle Contract
* @author Manan Bedi
* @notice This comtract is for creating an untamerable decentralized smart contract
@dev This contract is a raffle that uses Chainlink Keepers to draw a winner.
The raffle is open for desired time interval, and then the Keeper network will draw a winner.
The winner will receive all of the ETH in the contract.
*/
contract Raffle is VRFConsumerBaseV2, KeeperCompatibleInterface {
/* Type Declarations */
enum RaffleState {
OPEN,
CALCULATING
}
/* State Variables */
uint256 private immutable i_entranceFee;
address payable[] private s_players;
VRFCoordinatorV2Interface private immutable i_vrfCoordinator;
bytes32 private immutable i_gasLane;
uint64 private immutable i_subscriptionId;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
// Lottery Variables
address private s_recentWinner;
RaffleState private s_raffleState;
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
/* Events */
event RaffleEnter(address indexed player);
event RequestedRaffleWinner(uint256 indexed requestId);
event WinnerPicked(address indexed winner);
constructor(
address vrfCoordinatorV2,
uint256 entranceFee,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
) VRFConsumerBaseV2(vrfCoordinatorV2) {
i_entranceFee = entranceFee;
i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2);
i_gasLane = gasLane;
i_subscriptionId = subscriptionId;
i_callbackGasLimit = callbackGasLimit;
s_raffleState = RaffleState.OPEN;
s_lastTimeStamp = block.timestamp;
i_interval = interval;
}
function enterRaffle() public payable {
if (msg.value < i_entranceFee) {
revert Raffle__NotEnoughETHEntered();
}
if (s_raffleState != RaffleState.OPEN) {
revert Raffle__NotOpen();
}
s_players.push(payable(msg.sender));
emit RaffleEnter(msg.sender);
}
function checkUpkeep(
bytes memory /* checkData */
)
public
override
returns (
bool upkeepNeeded,
bytes memory /* performData */
)
{
bool isOpen = (RaffleState.OPEN == s_raffleState);
bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
bool hasPlayers = (s_players.length > 0);
bool hasBalance = (address(this).balance > 0);
upkeepNeeded = (isOpen && timePassed && hasPlayers && hasBalance);
}
function performUpkeep(
bytes calldata /* performData */
) external override {
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Raffle__UpkeepNotNeeded(
address(this).balance,
s_players.length,
uint256(s_raffleState)
);
}
s_raffleState = RaffleState.CALCULATING;
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedRaffleWinner(requestId);
}
function fulfillRandomWords(
uint256, /* requestId */
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable recentWinner = s_players[indexOfWinner];
s_recentWinner = recentWinner;
s_raffleState = RaffleState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Raffle__TransferFailed();
}
emit WinnerPicked(recentWinner);
}
function getEntranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayers(uint256 index) public view returns (address) {
return s_players[index];
}
function getRecentWinner() public view returns (address) {
return s_recentWinner;
}
function getRaffleState() public view returns (RaffleState) {
return s_raffleState;
}
function getNumWords() public pure returns (uint256) {
return NUM_WORDS;
}
function getInterval() public view returns (uint256) {
return i_interval;
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@manan-bedi2908 : It should me Correct this line in your test from this : To this : Let me know, If the issue persists.. |
Beta Was this translation helpful? Give feedback.
-
I met the same error and found 2 solutions:
additional info: #1375 (comment) , https://ethereum.stackexchange.com/questions/131426/chainlink-keepers-getting-invalidconsumer |
Beta Was this translation helpful? Give feedback.
@manan-bedi2908 : It should me
request
notsend
:Correct this line in your test from this :
await network.provider.send({ method: "evm_mine", params: [] })
To this :
await network.provider.request({ method: "evm_mine", params: [] })
Let me know, If the issue persists..