Skip to content

Commit

Permalink
Update code to use Mock ChainLink contracts
Browse files Browse the repository at this point in the history
It doesn't actually compile since some dependencies require v4 of the
compiler, while others depend on v8. [This issue](PatrickAlphaC/smartcontract-lottery#64),
which turned out to be for the same idea that I'm implementing, is about
the same issue, and is still unresolved.
  • Loading branch information
srithon committed May 16, 2023
1 parent 1f0d561 commit 508aeec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
10 changes: 8 additions & 2 deletions contracts/LotterySystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 29 additions & 11 deletions scripts/test-lottery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 508aeec

Please sign in to comment.