-
Hello! I finished coding the deploy scripts and run "yarn hardhat deploy" and this error appears: I tried downgrading all my code to VRFCoordinatorV2Mock but same error occurs. I also tried running "yarn hardhat deploy --tags mocks" first and this is displayed in the terminal: Raffle.sol: // Raffle
// Enter the lottery (paying some amount)
// Pick a random winner (verifiably random)
// Winner to be selected every X minutes -> completely automate
// Chainlink Oracle -> Randomness, Automated Execute, (Chainlink Keepers)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";
import {AutomationCompatibleInterface} from "@chainlink/contracts/src/v0.8/automation/interfaces/AutomationCompatibleInterface.sol";
error Raffle__NotEnoughETH();
error Raffle__TransferFailed();
error Raffle__NotOpen();
error Raffle__UpKeepNotNeeded(uint256 currentBalance, uint256 numPlayers, uint256 raffleState);
/** @title A sample Raffle Contract
* @author Louie
* @notice This is for creating an untamperable, decentrelized smart contract
* @dev This implements Chainlink VRF v2Plus and Chainlink Automation
*/
contract Raffle is VRFConsumerBaseV2Plus, AutomationCompatibleInterface {
/* Types */
enum RaffleState {
OPEN,
CALCULATING
}
/* State Variables */
uint256 private immutable i_entranceFee;
address payable[] private s_players;
bytes32 private immutable i_keyHash;
uint64 private immutable i_subscriptionId;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFORMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
/* Lottery Variables */
address private s_recentWinner;
RaffleState private s_raffleState;
/* Events */
event RaffleEnter(address indexed player);
event RequestedRaffleWinner(uint256 indexed requestId);
event WinnerPicked(address indexed winner);
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
/* Constructor */
constructor(
address vrfCoordinatorV2Plus,
uint256 entranceFee,
bytes32 keyHash,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
) VRFConsumerBaseV2Plus(vrfCoordinatorV2Plus) {
i_entranceFee = entranceFee;
i_keyHash = keyHash;
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__NotEnoughETH();
}
if (s_raffleState != RaffleState.OPEN) {
revert Raffle__NotOpen();
}
s_players.push(payable(msg.sender));
emit RaffleEnter(msg.sender);
}
// Conditions
function checkUpkeep(
bytes memory /* checkData */
) public view 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);
return (upkeepNeeded, "0x0");
}
// Execution
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 = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: i_keyHash,
subId: i_subscriptionId,
requestConfirmations: REQUEST_CONFORMATIONS,
callbackGasLimit: i_callbackGasLimit,
numWords: NUM_WORDS,
extraArgs: VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: false}))
})
);
emit RequestedRaffleWinner(requestId);
}
// Actions
function fulfillRandomWords(
uint256 /* requestId */,
uint256[] calldata randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable recentWinner = s_players[indexOfWinner];
s_recentWinner = recentWinner;
s_raffleState = RaffleState.OPEN;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Raffle__TransferFailed();
}
emit WinnerPicked(recentWinner);
s_players = new address payable[](0); // Reset players array
s_lastTimeStamp = block.timestamp;
}
/* Getters Function */
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 getNumWords() public pure returns (uint256) {
return NUM_WORDS;
}
function getNumOfPlayers() 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_CONFORMATIONS;
}
} VRFCoordinatorV2_5Mock.sol: // SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol"; 00-deploy-mocks.js: const { network, ethers } = require("hardhat")
const { developmentChains } = require("../helper-hardhat-config")
const BASE_FEE = ethers.parseEther("0.50") // 0.25 LINK in wei (BigInt)
const GAS_PRICE_LINK = ethers.toBigInt(1e9) // 0.000000001 LINK per gas (BigInt)
const WEI_PER_UNIT_LINK = ethers.parseEther("0.05") // 0.005 LINK in wei (BigInt)
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
if (developmentChains.includes(network.name)) {
log("Local network detected! Deploying mocks...")
try {
// If VRFCoordinatorV2_5Mock is from an external package, specify the contract field
await deploy("VRFCoordinatorV2_5Mock", {
from: deployer,
log: true,
args: [BASE_FEE, GAS_PRICE_LINK, WEI_PER_UNIT_LINK],
})
log("----------------------------------------------------------")
} catch (error) {
console.error("Deployment failed with error:", error)
}
}
}
module.exports.tags = ["all", "mocks"] 01-deploy-raffle.js: const { network, ethers } = require("hardhat")
const { developmentChains, networkConfig } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
const FUND_AMOUNT = ethers.parseEther("30")
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2_5Address, subscriptionId, vrfCoordinatorV2_5Mock
if (chainId == 31337) {
log("Deploying...")
vrfCoordinatorV2_5Mock = await ethers.getContract("VRFCoordinatorV2_5Mock")
vrfCoordinatorV2_5Address = vrfCoordinatorV2_5Mock.address
// Create a new subscription
const transactionResponse = await vrfCoordinatorV2_5Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait()
subscriptionId = transactionReceipt.events[0].args.subId
// Fund the subscription
// Our mock makes it so we don't actually have to worry about sending fund
await vrfCoordinatorV2_5Mock.fundSubscription(subscriptionId, FUND_AMOUNT)
} else {
vrfCoordinatorV2_5Address = networkConfig[chainId]["vrfCoordinatorV2_5"]
subscriptionId = networkConfig[chainId]["subscriptionId"]
}
const args = [
vrfCoordinatorV2_5Address,
networkConfig[chainId]["entranceFee"],
networkConfig[chainId]["keyHash"],
subscriptionId,
networkConfig[chainId]["callbackGasLimit"],
networkConfig[chainId]["interval"],
]
const raffle = await deploy("Raffle", {
from: deployer,
args: args,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
// If we are on a development chain, add the raffle contract as a consumer to the VRF mock
// if (developmentChains.includes(network.name)) {
// const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2_5Mock")
// await vrfCoordinatorV2Mock.addConsumer(subscriptionId, raffle.address)
// log("Consumer is added")
// }
if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
log("Verifying...")
await verify(raffle.address, args)
}
log("---------------------------------------------------------------")
}
module.exports.tags = ["all", "raffle"] helper-hardhat-config.js: const { ethers } = require("hardhat")
const networkConfig = {
11155111: {
name: "sepolia",
vrfCoordinatorV2_5: "0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B", // Updated to v2.5 coordinator address
entranceFee: ethers.parseEther("0.01"),
keyHash: "0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae", // Updated to v2.5 coordinator address
subscriptionId: "0",
callbackGasLimit: "500000",
interval: "30",
},
31337: {
name: "hardhat",
entranceFee: ethers.parseEther("0.01"),
keyHash: "0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae", // Updated to v2.5 coordinator address
callbackGasLimit: "500000",
interval: "30",
},
}
const developmentChains = ["hardhat", "localhost"]
module.exports = {
networkConfig,
developmentChains,
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey! I see different conditions:
I'd suggest to equalize this stuff (for example check chain ID in both places) and make sure the networks correspond: Also you may specify the network while running a script yarn hardhat deploy --tags mocks --network hardhat |
Beta Was this translation helpful? Give feedback.
Hey! I see different conditions:
developmentChains.includes(network.name)
chainId == 31337
I'd suggest to equalize this stuff (for example check chain ID in both places) and make sure the networks correspond:
sepolia
,hardhat
,localhost
.Also you may specify the network while running a script