Skip to content

An advanced Raffle Smart Contract which uses Chainlink Keepers for automation and Chainlink VRF to generate a random winner.

Notifications You must be signed in to change notification settings

byte14/advanced-raffle-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raffle Contract

Table of Contents

About The Project

This is an advanced Raffle Smart Contract which uses Chainlink Keepers for automatically kicking off Chainlink VRF to generate a random winner after the given time interval has passed. It allows dynamic subscription of Chainlink Keepers Upkeep and Chainlink VRF.

Built With

  • Solidity
  • Ethers
  • Hardhat
  • Chainlink

Getting Started

Prerequisites

  • git
    • You'll know you did it right if you can run git --version and you see a response like git version x.x.x
  • Nodejs
    • You'll know you've installed nodejs right if you can run:
      • node --version and get an ouput like: vx.x.x
  • Yarn instead of npm
    • You'll know you've installed yarn right if you can run:
      • yarn --version and get an output like: x.x.x
      • You might need to install it with npm or corepack

Installation

  1. Clone the repo

    git clone https://github.com/github_username/repo_name.git
    
  2. Change the current working directory

    cd raffle-contract
    
  3. Install yarn packages

    yarn install
    

Usage

Deploy:

yarn hardhat deploy

Testing:

yarn hardhat test

Test Coverage:

yarn hardhat coverage

Deployment to a testet

1. Setup environment variabltes

You'll want to set your GOERLI_RPC_URL and PRIVATE_KEY as environment variables. You can add them to a .env file.

  • PRIVATE_KEY: The private key of your account (like from metamask).
  • GOERLI_RPC_URL: This is url of the goerli testnet node you're working with. You can get setup with one for free from Alchemy

2. Get testnet ETH and LINK

Head over to faucets.chain.link and get some tesnet ETH & LINK. You should see the ETH and LINK show up in your metamask.

3. Deploy to goerli testnet

Now you can run:

yarn hardhat deploy --network goerli

NOTE: You don't need to setup a Chainlink VRF Subscription and Chainlink Keepers Upkeep. This will be automatically done when you deploy the contract. Your Upkeep will be funded with 5 LINK and VRF subscription with 2 LINK. You can fund with different amount by changing value of fundUpkeepAmount and fundVRFAmount in your helper-hardhat-config.js.

4. Enter the raffle

Your contract is now setup and you can enter the lottery by running:

yarn hardhat run scripts/enterRaffle.js --network goerli

Estimate gas cost in USD

To get a USD estimation of gas cost, you'll need a COINMARKETCAP_API_KEY environment variable and add it into your .env file. You can get one for free from CoinMarketCap.

Verify on etherscan

If you deploy to a testnet or mainnet, you can verify it if you get an API Key from Etherscan and set it as an environemnt variable named ETHERSCAN_API_KEY. You can add it into your .env file.

In it's current state, if you have your api key set, it will auto verify goerli contracts.

However, you can manual verify with:

yarn hardhat verify --network goerli --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS

Check out hardhat-etherscan for reference.

Scripts for interacting with contract on testnet

Enter Raffle:

yarn hardhat run scripts/enterRaffle.js --network goerli

Deposit Link into the contract:

yarn hardhat run scripts/depositLink.js --network goerli

Withdraw Link from the contract:

yarn hardhat run scripts/withdrawLink.js --network goerli

Register Chainlink Keepers Upkeep:

yarn hardhat run scripts/registerUpkeep.js --network goerli

Create Chainlink VRF subscription:

yarn hardhat run scripts/createVRFSubscription.js --network goerli

Fund Chainlink VRF subscription:

yarn hardhat run scripts/fundVRFSubscription.js --network goerli

Add consumer for Chainlink VRF subscription:

yarn hardhat run scripts/addVRFConsumer.js --network goerli

Get Chainlink VRF subscription balance:

yarn hardhat run scripts/getVRFSubscriptionBalance.js --network goerli

Cancel Chainlink VRF subscription:

yarn hardhat run scripts/cancelVRFSubscription.js --network goerli

About

An advanced Raffle Smart Contract which uses Chainlink Keepers for automation and Chainlink VRF to generate a random winner.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published