From 508aeec863ef9d701316ee6812440a3cfc2ff431 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Mon, 15 May 2023 23:24:02 -0400 Subject: [PATCH] Update code to use Mock ChainLink contracts It doesn't actually compile since some dependencies require v4 of the compiler, while others depend on v8. [This issue](https://github.com/PatrickAlphaC/smartcontract-lottery/issues/64), which turned out to be for the same idea that I'm implementing, is about the same issue, and is still unresolved. --- contracts/LotterySystem.sol | 10 ++++++++-- hardhat.config.js | 11 +++++++++- scripts/test-lottery.js | 40 +++++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/contracts/LotterySystem.sol b/contracts/LotterySystem.sol index 53aa09a..e8110b8 100644 --- a/contracts/LotterySystem.sol +++ b/contracts/LotterySystem.sol @@ -4,9 +4,15 @@ pragma solidity ^0.8.0; import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol"; -// import "@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol"; import "@chainlink/contracts/src/v0.8/VRFV2WrapperConsumerBase.sol"; +// We are not using this in here, but we need to have it so that HardHat will generator an artifact for it. +// See https://ethereum.stackexchange.com/questions/114376/how-to-compile-external-contracts-using-hardhat +import "@chainlink/contracts/src/v0.8/mocks/VRFCoordinatorV2Mock.sol"; +import "@chainlink/contracts/src/v0.8/tests/MockV3Aggregator.sol"; +import "@chainlink/contracts/src/v0.4/LinkToken.sol"; +import "@chainlink/contracts/src/v0.8/VRFV2Wrapper.sol"; + // Switch this to your own contract address once deployed, for bookkeeping! // Example Contract Address on Goerli: 0xDBa03676a2fBb6711CB652beF5B7416A53c1421D @@ -82,7 +88,7 @@ contract LotterySystem is VRFV2WrapperConsumerBase { // while a winner is actively being chosen. bool lotteryInitiated; - constructor() VRFV2WrapperConsumerBase(linkToken, vrfCoordinatorWrapper) { + constructor(address _linkToken, address _vrfCoordinatorWrapper) VRFV2WrapperConsumerBase(_linkToken, _vrfCoordinatorWrapper) { // Store the address of the deployer as a payable address. // When we withdraw funds, we'll withdraw here. owner = payable(msg.sender); diff --git a/hardhat.config.js b/hardhat.config.js index 3763afa..eb95820 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -14,7 +14,16 @@ const PRIVATE_KEY = process.env.PRIVATE_KEY; * @type import('hardhat/config').HardhatUserConfig */ module.exports = { - solidity: "0.8.4", + solidity: { + compilers: [ + { + version: "0.4.24" + }, + { + version: "0.8.6" + } + ] + }, networks: { goerli: { url: GOERLI_URL, diff --git a/scripts/test-lottery.js b/scripts/test-lottery.js index 2653dbc..6a205d6 100644 --- a/scripts/test-lottery.js +++ b/scripts/test-lottery.js @@ -30,40 +30,58 @@ async function main() { // Get the example accounts we'll be working with. const [owner, tipper, tipper2, tipper3] = await hre.ethers.getSigners(); + // let's deploy our mock vrf coordinator contract + const VRFCoordinatorV2Mock = await hre.ethers.getContractFactory("VRFCoordinatorV2Mock"); + // https://docs.chain.link/vrf/v2/subscription/examples/test-locally#deploy-vrfcoordinatorv2mock + const vrfCoordinatorV2Mock = await VRFCoordinatorV2Mock.deploy(1000000000000000, 1000000000); + + // let's deploy our mock oracle price feed + const MockV3Aggregator = await hre.ethers.getContractFactory("MockV3Aggregator"); + const mockV3Aggregator = await MockV3Aggregator.deploy(18, 3000000000000000); + + // let's deploy the link token contract, which allows us to directly fund our VRF requests + const LinkToken = await hre.ethers.getContractFactory("LinkToken"); + const linkToken = await LinkToken.deploy(); + + const VRFV2Wrapper = await hre.ethers.getContractFactory("VRFV2Wrapper"); + const vrfV2Wrapper = await VRFV2Wrapper.deploy(linkToken.address, mockV3Aggregator.address, vrfCoordinatorV2Mock.address); + // We get the contract to deploy. const LotterySystem = await hre.ethers.getContractFactory("LotterySystem"); - const lotterySystem = await LotterySystem.deploy(); + const lotterySystemInstance = await LotterySystem.deploy(linkToken.address, vrfV2Wrapper.address); // Deploy the contract. - await lotterySystem.deployed(); - console.log("LotterySystem deployed to:", lotterySystem.address); + await lotterySystemInstance.deployed(); + console.log("LotterySystem deployed to:", lotterySystemInstance.address); // Check balances before the coffee purchase. - const addresses = [owner.address, tipper.address, lotterySystem.address]; + const addresses = [owner.address, tipper.address, lotterySystemInstance.address]; console.log("== start =="); await printBalances(addresses); + // const lotterySystemInstance = await lotterySystem.connect(owner).CreateNewLotterySystem(mockLinkToken.address, VRFCoordinatorV2Mock.address); + // Buy the owner a few coffees. const tip = {value: hre.ethers.utils.parseEther("1")}; - await lotterySystem.connect(tipper).buyTicketShares(tip); - await lotterySystem.connect(tipper2).buyTicketShares(tip); - await lotterySystem.connect(tipper3).buyTicketShares(tip); + await lotterySystemInstance.connect(tipper).buyTicketShares(tip); + await lotterySystemInstance.connect(tipper2).buyTicketShares(tip); + await lotterySystemInstance.connect(tipper3).buyTicketShares(tip); // Check balances after the coffee purchase. console.log("== bought coffee =="); await printBalances(addresses); // Withdraw. - await lotterySystem.connect(owner).initiateLottery(); + await lotterySystemInstance.connect(owner).initiateLottery(); // Check balances after withdrawal. console.log("== withdrawTips =="); await printBalances(addresses); // Check out the memos. - console.log("== memos =="); - const memos = await lotterySystem.getMemos(); - printMemos(memos); + // console.log("== memos =="); + // const memos = await lotterySystemInstance.getMemos(); + // printMemos(memos); } // We recommend this pattern to be able to use async/await everywhere