- This code creates a provably random lottery using Chainlink VRF and Automation
- Huge shout out to Patrick Collins for making all this!
- Users can pay for a ticket
- The ticket fees are going to go to the winner
- After some amount of time, the lottery will auto pick a winner
- Programatically
- Use Chainlink VRF (Randomness) & Chainlink Automation (Time-Based Trigger)
-
doesn't make sense to use require any more bc of gas
-
use revert
-
Created custom gas efficient errors, 1 that takes multiple parameters 2. error My__Error(uint256 someVariable, uint256 anotherVariable); 3. revert My_Error(address(this.balance), anotherVariable.length);
-
name error messages right under contract declaration
- error Raffle__NotEnoughEthSent as an example (Two underscores after contract name then error)
- An enum (short for "enumeration") is a user-defined data type that consists of a set of named values, called elements or members, representing distinct constants. It is used to define variables that can only hold one of the predefined values, improving code readability and reducing errors.
- Raffle.sol has a robust constructor ready to go for any chain deployment, simply add a new config function in HelperConfig
https://vrf.chain.link/sepolia/new
- Connect Wallet and approve transaction
- Get test LINK and ETH
- Create Subscription using Metamask
- Fund Subscription
https://automation.chain.link/
- Register New Upkeep
- Use custom logic
- Fill in blanks
- cheaper to make all upper case (goes right under error message or contract name), add getter functions
- It works more like take the first set of numbers and see what is left over 2334502 % 10 is just the 2 at the end bc it's what is left over
- do checks (require if--> error) early in function (more gas efficient)
- do effect after checks
- interactions with other contracts come later
- events come before interactions
- we don't have to call the pick winner function bc Chainlink nodes will do that using automation.
- Designed to deploy the raffle.sol for any chain setup in the config file
- Assess, Act, Assert
This is a section from the Cyfrin Foundry Solidity Course.
⭐️ (3:04:09) | Lesson 9: Foundry Smart Contract Lottery
- Foundry Smart Contract Lottery
- Personal notes
- What we want it to do?
- Learning to create NatSpec Section in contract (Goes above contract, below pragma)
- Error handling
- Enums
- Constructor and HelperConfig script
- Create Chainlink VRF Subscription
- Create Chainlink Automation Account
- State Variables
- Modulo function is goofy
- CEI: Checks Effects and Interactions
- Chainlink upKeep
- Script
- Testing
- Cyfrin ReadMe
- Getting Started
- Usage
- Deployment to a testnet or mainnet
- Formatting
- git
- You'll know you did it right if you can run
git --version
and you see a response likegit version x.x.x
- You'll know you did it right if you can run
- foundry
- You'll know you did it right if you can run
forge --version
and you see a response likeforge 0.2.0 (816e00b 2023-03-16T00:05:26.396218Z)
- You'll know you did it right if you can run
git clone https://github.com/CPix18/FoundrySmartLottery
cd FoundrySmartLottery
forge build
make anvil
If you're having a hard time installing the chainlink library, you can optionally run this command.
forge install smartcontractkit/chainlink-brownie-contracts@0.6.1 --no-commit
This will default to your local node. You need to have it running in another terminal in order for it to deploy.
make deploy
We talk about 4 test tiers in the video.
- Unit
- Integration
- Forked
- Staging
This repo we cover #1 and #3.
forge test
or
forge test --fork-url $SEPOLIA_RPC_URL
forge coverage
- Setup environment variables
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.- You can learn how to export it here.
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
Optionally, add your ETHERSCAN_API_KEY
if you want to verify your contract on Etherscan.
- Get testnet ETH
Head over to faucets.chain.link and get some testnet ETH. You should see the ETH show up in your metamask.
- Deploy
make deploy ARGS="--network sepolia"
This will setup a ChainlinkVRF Subscription for you. If you already have one, update it in the scripts/HelperConfig.s.sol
file. It will also automatically add your contract as a consumer.
- Register a Chainlink Automation Upkeep
You can follow the documentation if you get lost.
Go to automation.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:
After deploying to a testnet or local net, you can run the scripts.
Using cast deployed locally example:
cast send <RAFFLE_CONTRACT_ADDRESS> "enterRaffle()" --value 0.1ether --private-key <PRIVATE_KEY> --rpc-url $SEPOLIA_RPC_URL
or, to create a ChainlinkVRF Subscription:
make createSubscription ARGS="--network sepolia"
You can estimate how much gas things cost by running:
forge snapshot
And you'll see an output file called .gas-snapshot
To run code formatting:
forge fmt
If you appreciated this, feel free to follow me or donate!
ETH/Arbitrum/Optimism/Polygon/etc Address: 0x75C875c4b81D792797c6ccCe724c03FE8d31FE0e