Lesson 9 ending: 16:30:00 #5608
-
I run into the problem of not able to "staging test" the contract at the end of this lessonthis is the link to my github: https://github.com/Alanle1011/hardhat-lottery-smartcontract.git Raffle.staging.test,js const { assert, expect } = require("chai");
const { getNamedAccounts, ethers, network } = require("hardhat");
const { developmentChains } = require("../../helper-hardhat-config");
developmentChains.includes(network.name)
? describe.skip
: describe("Raffle Staging Tests", function () {
let raffle, raffleEntranceFee, deployer;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
raffle = await ethers.getContract("Raffle", deployer);
raffleEntranceFee = await raffle.getEntranceFee();
});
describe("fulfillRandomWords", function () {
it("works with live Chainlink Keepers and Chainlink VRF, we get a random winner", async function () {
// enter the raffle
console.log("Setting up test...");
const startingTimeStamp = await raffle.getLatestTimeStamp();
const accounts = await ethers.getSigners();
console.log("Setting up Listener...");
await new Promise(async (resolve, reject) => {
// setup listener before we enter the raffle
// Just in case the blockchain moves REALLY fast
raffle.once("WinnerPicked", async () => {
console.log("WinnerPicked event fired!");
try {
// add our asserts here
const recentWinner = await raffle.getRecentWinner();
const raffleState = await raffle.getRaffleState();
const winnerEndingBalance = await accounts[0].getBalance();
const endingTimeStamp = await raffle.getLatestTimeStamp();
await expect(raffle.getPlayer(0)).to.be.reverted;
assert.equal(recentWinner.toString(), accounts[0].address);
assert.equal(raffleState, 0);
assert.equal(
winnerEndingBalance.toString(),
winnerStartingBalance.add(raffleEntranceFee).toString()
);
assert(endingTimeStamp > startingTimeStamp);
resolve();
} catch (error) {
console.log(error);
reject(error);
}
});
// Then entering the raffle
console.log("Entering Raffle...");
const tx = await raffle.enterRaffle({ value: raffleEntranceFee });
await tx.wait(1);
console.log("Ok, time to wait...");
const winnerStartingBalance = await accounts[0].getBalance();
// and this code WONT complete until our listener has finished listening!
});
});
});
}); Raffle.sol // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AutomationCompatibleInterface.sol";
error Raffle__NotEnoughEthEntered();
error Raffle__TransferFail();
error Raffle__NotOpen();
error Raffle_UpkeepNotNeeded(
uint256 currentBalance,
uint256 currnetPlayer,
uint256 raffleState
);
/**
* @title Lottery contract
* @author Alanle1011
* @notice This contract is for creating an lettory system for player to all
* donate money to and the system will give it to a random player
* @dev this contract use VRFConsumerBaseV2 and AutomationCompatibleInterface
*/
contract Raffle is VRFConsumerBaseV2, AutomationCompatibleInterface {
/* Type Declerations */
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;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private immutable i_callbackGasLimit;
uint32 private constant NUM_WORDS = 1;
/* Lottry Variables */
address private s_recentWinner;
RaffleState private s_raffleState;
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
/* Event */
event RaffleEnter(address indexed player);
event RequestedRaffleWinner(uint256 indexed requestId);
event WinnerPicked(address indexed winner);
/* Functions */
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 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__TransferFail();
}
emit WinnerPicked(recentWinner);
}
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, //gasLane
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
emit RequestedRaffleWinner(requestId);
}
/**
* @dev This is the function that ChainLink Keeper nodes call
* they look for the `upkeepNeeded` to return true
* The following should be true in order to return true
* 1. Our time interval should have passes
* 2. The lottery should have at least 1 player, and have some ETH
* 3. Our subcription is funded with LINK
* 4. The lottery should be an "open" state.
*/
function checkUpkeep(
bytes memory /*checkData*/
)
public
view
override
returns (bool upkeepNeeded, bytes memory /* performData */)
{
bool isOpen = RaffleState.OPEN == s_raffleState;
bool timePasses = (block.timestamp - s_lastTimeStamp) > i_interval;
bool hasPlayers = (s_players.length > 0);
bool hasBalance = address(this).balance > 0;
upkeepNeeded = (isOpen && timePasses && hasPlayers && hasBalance);
}
/* View / Pure functions */
function getEntranceFee() public view returns (uint256) {
return i_entranceFee;
}
function getPlayer(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 getNumWord() public pure returns (uint256) {
return NUM_WORDS;
}
function getNumberOfPlayer() public view returns (uint256) {
return s_players.length;
}
function getLatestTimeStamp() public view returns (uint256) {
return s_lastTimeStamp;
}
function getRequestConfirmations() public pure returns (uint256) {
return REQUEST_CONFIRMATIONS;
}
function getInterval()public view returns(uint256){
return i_interval;
}
}
|
Beta Was this translation helpful? Give feedback.
Answered by
Alanle1011
Jun 4, 2023
Replies: 2 comments 1 reply
-
I re-deploy the new contract and it work |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Alanle1011
-
Hi @Alanle1011, how did you solve this error. Could you explain me? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I re-deploy the new contract and it work