• Stars
    star
    113
  • Rank 300,478 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hardhat Smartcontract Lottery (Raffle) FCC

This repo has been updated to work with Sepolia over Goerli.

This is a section of the Javascript Blockchain/Smart Contract FreeCodeCamp Course.

⌨️ (13:41:02) Lesson 9: Hardhat Smart Contract Lottery

Full Repo

This project is apart of the Hardhat FreeCodeCamp video.

Checkout the full blockchain course video here.

Getting Started

Requirements

  • 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

Quickstart

git clone https://github.com/PatrickAlphaC/hardhat-smartcontract-lottery-fcc
cd hardhat-smartcontract-lottery-fcc
yarn

Typescript

If you want to get to typescript and you cloned the javascript version, just run:

git checkout typescript
yarn 

Usage

Deploy:

yarn hardhat deploy

Testing

yarn hardhat test

Test Coverage

yarn hardhat coverage

Deployment to a testnet or mainnet

  1. Setup environment variabltes

You'll want to set your SEPOLIA_RPC_URL and PRIVATE_KEY as environment variables. You can add them to a .env file, similar to what you see in .env.example.

  • PRIVATE_KEY: The private key of your account (like from metamask). NOTE: FOR DEVELOPMENT, PLEASE USE A KEY THAT DOESN'T HAVE ANY REAL FUNDS ASSOCIATED WITH IT.
  • SEPOLIA_RPC_URL: This is url of the sepolia testnet node you're working with. You can get setup with one for free from Alchemy
  1. Get testnet ETH

Head over to faucets.chain.link and get some tesnet ETH & LINK. You should see the ETH and LINK show up in your metamask. You can read more on setting up your wallet with LINK.

  1. Setup a Chainlink VRF Subscription ID

Head over to vrf.chain.link and setup a new subscription, and get a subscriptionId. You can reuse an old subscription if you already have one.

You can follow the instructions if you get lost. You should leave this step with:

  1. A subscription ID

  2. Your subscription should be funded with LINK

  3. Deploy

In your helper-hardhat-config.js add your subscriptionId under the section of the chainId you're using (aka, if you're deploying to sepolia, add your subscriptionId in the subscriptionId field under the 11155111 section.)

Then run:

yarn hardhat deploy --network sepolia

And copy / remember the contract address.

  1. Add your contract address as a Chainlink VRF Consumer

Go back to vrf.chain.link and under your subscription add Add consumer and add your contract address. You should also fund the contract with a minimum of 1 LINK.

  1. Register a Chainlink Keepers Upkeep

You can follow the documentation if you get lost.

Go to keepers.chain.link and register a new upkeep. Choose Custom logic as your trigger mechanism for automation. Your UI will look something like this once completed:

Keepers

  1. Enter your raffle!

You're contract is now setup to be a tamper proof autonomous verifiably random lottery. Enter the lottery by running:

yarn hardhat run scripts/enter.js --network sepolia

Estimate gas cost in USD

To get a USD estimation of gas cost, you'll need a COINMARKETCAP_API_KEY environment variable. You can get one for free from CoinMarketCap.

Then, uncomment the line coinmarketcap: COINMARKETCAP_API_KEY, in hardhat.config.js to get the USD estimation. Just note, everytime you run your tests it will use an API call, so it might make sense to have using coinmarketcap disabled until you need it. You can disable it by just commenting the line back out.

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 pop it into your .env file as seen in the .env.example.

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

However, you can manual verify with:

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

Typescript differences

  1. .js files are now .ts
  2. We added a bunch of typescript and typing packages to our package.json. They can be installed with:
    1. yarn add @typechain/ethers-v5 @typechain/hardhat @types/chai @types/node ts-node typechain typescript
  3. The biggest one being typechain
    1. This gives your contracts static typing, meaning you'll always know exactly what functions a contract can call.
    2. This gives us factories that are specific to the contracts they are factories of. See the tests folder for a version of how this is implemented.
  4. We use imports instead of require. Confusing to you? Watch this video
  5. Add tsconfig.json

Linting

To check linting / code formatting:

yarn lint

or, to fix:

yarn lint:fix

Thank you!

If you appreciated this, feel free to follow me or donate!

ETH/Polygon/Avalanche/etc Address: 0x9680201d9c93d65a3603d2088d125e955c73BD65

Patrick Collins Twitter Patrick Collins YouTube Patrick Collins Linkedin Patrick Collins Medium

More Repositories

1

nft-mix

Solidity
803
star
2

dungeons-and-dragons-nft

#chainlink #nft
JavaScript
603
star
3

hardhat-security-fcc

Solidity
400
star
4

all-on-chain-generated-nft

A repo for generating random NFTs with metadata 100% on chain!
JavaScript
349
star
5

dao-template

TypeScript
239
star
6

full-stack-web3-metamask-connectors

177
star
7

chainlink_defi

Build a defi yield farmable dApp. Get started here.
JavaScript
157
star
8

smart-contract-frameworks

A list of smart contract frameworks
C
119
star
9

hardhat-nft-marketplace-fcc

JavaScript
112
star
10

hardhat-nft-fcc

JavaScript
99
star
11

simple-storage-fcc

Solidity
93
star
12

unstoppable-ui

JavaScript
88
star
13

async-python

Shows how to use async requests vs requests
Python
87
star
14

hardhat-fund-me-fcc

JavaScript
82
star
15

ethers-simple-storage-fcc

JavaScript
82
star
16

defi-stake-yield-brownie

Solidity
78
star
17

smartcontract-lottery

Solidity
75
star
18

PatrickAlphaC

75
star
19

hardhat-simple-storage-fcc

JavaScript
68
star
20

aave_brownie_py

Solidity
61
star
21

foundry-play

Solidity
58
star
22

nextjs-smartcontract-lottery-fcc

JavaScript
57
star
23

pokemon-nft

PokΓ©mon NFTs
Solidity
54
star
24

defi-stake-yield-brownie-freecode

Solidity
53
star
25

fund-me-fcc

Solidity
52
star
26

simple_storage

Solidity
51
star
27

erc20-brownie

Solidity
50
star
28

fund_me

Solidity
48
star
29

web3_py_simple_storage

Python
45
star
30

storage-factory-fcc

Solidity
44
star
31

nextjs-nft-marketplace-moralis-fcc

JavaScript
44
star
32

lens-blog

A minimal example of using Lens Protocol to build a blog
JavaScript
40
star
33

nextjs-nft-marketplace-thegraph-fcc

JavaScript
39
star
34

nft-demo

Python
38
star
35

hardhat-erc20-fcc

JavaScript
37
star
36

html-js-ethers-connect

JavaScript
36
star
37

defi_py_mix

Solidity
36
star
38

storage_factory

Solidity
35
star
39

aave_web3_py

web3.py way to interact with aave
Python
35
star
40

rwa-creator

Solidity
35
star
41

hardhat-defi-fcc

Solidity
34
star
42

graph-nft-marketplace-fcc

TypeScript
32
star
43

sc-language-comparison

Solidity
30
star
44

denver-security

Solidity
28
star
45

smartcontract-upgrades-example

JavaScript
28
star
46

html-fund-me-fcc

JavaScript
27
star
47

foundry-smart-contract-lottery-f23

Solidity
27
star
48

brownie_fund_me

Solidity
25
star
49

nextjs-ethers-metamask-connect

JavaScript
24
star
50

weather-nft

chainlink weather nfts
Solidity
24
star
51

brownie_simple_storage

Python
23
star
52

chainlink-the-graph

JavaScript
22
star
53

flashloan-forta-py

Python
22
star
54

chainlink-hardhat

JavaScript
20
star
55

upgrades-mix

Solidity
19
star
56

openweathermap_cl_ea

JavaScript
17
star
57

multicall-js

JavaScript
17
star
58

hardhat-upgrades-fcc

JavaScript
17
star
59

denver-security-challenges

Solidity
16
star
60

ai-stablecoin

Solidity
15
star
61

multicall

Python
15
star
62

staking-ui-demo

JavaScript
15
star
63

decentralized-raffle

TypeScript
15
star
64

ipfs_cl_ea

IPFS Chainlink External Adapter
JavaScript
14
star
65

hardhat-dao-fcc

TypeScript
14
star
66

get-all-transactions

Python
14
star
67

signatureVerification

Solidity
14
star
68

aave_brownie_py_freecode

Solidity
13
star
69

hardhat-events-logs

JavaScript
12
star
70

hardhat-simple-storage

JavaScript
12
star
71

nextjs-moralis-metamask-connect

JavaScript
12
star
72

hardhat-metamorphic-upgrades-fcc

JavaScript
12
star
73

defi-dapp

TypeScript
12
star
74

battle-game

JavaScript
11
star
75

foundry-vyper

A package for deploying and compiling vyper code in foundry
Solidity
11
star
76

vyper-chain-info

Python
10
star
77

vrf_pizza

lol
Solidity
10
star
78

dapptools-demo

Solidity
10
star
79

nextjs-web3-react-metamask-connect

JavaScript
10
star
80

filecoin_cl_ea

JavaScript
9
star
81

ape-fund-me-v23

Python
8
star
82

fuzzing-example

Solidity
8
star
83

eth-global-live-audit

Solidity
8
star
84

erc20-brownie-py

Python
8
star
85

puppy-raffle-smartcon

Solidity
7
star
86

brownie-events-logs

Python
7
star
87

unfinished-defi-workshop

JavaScript
6
star
88

vrf_pizza_front_end

lol
CSS
6
star
89

patrickalphac.com

TypeScript
6
star
90

nextjs-web3modal-metamask-connect

JavaScript
6
star
91

smartcon-hacker-house-hunt-rules

6
star
92

hardhat-fund-me-forked-fcc

JavaScript
5
star
93

geo_db_brownie

Solidity
5
star
94

clever-lottery-web3

JavaScript
5
star
95

scavenger-hunt

TypeScript
5
star
96

nextjs-ethers-introduction-fcc

JavaScript
5
star
97

nextjs-usedapp-metamask-connect

JavaScript
5
star
98

gcp-weather

Solidity
4
star
99

hackathon_scraper

Python
4
star
100

pizza_cl_ea

JavaScript
4
star