Skip to content

SGDlab67/foundry-smart-contract-lottery-sgd

Repository files navigation

🎰 Foundry Smart Contract Lottery - SGDlab67

Course Completion: This repository marks the successful completion of Lesson 9: Smart Contract Lottery from the Cyfrin Foundry Solidity Course, concluding the Foundry Fundamentals section.

📝 Updates from Original Course

  1. V2.5 of Chainlink VRF uses a uint256 as a subId instead of a uint64 - this repo reflects that change with appropriate mocks for version 2.5.
  2. Uses 0.1.0 of the foundry-devops package which doesn't require ffi=true


About

A provably fair, decentralized lottery smart contract built with Foundry. This project implements:

  • Chainlink VRF V2.5 for verifiable random number generation
  • Chainlink Automation for trustless upkeep execution
  • Comprehensive unit and fork testing
  • Deployment scripts for local and testnet environments

Getting Started

Requirements

  • git
    • Verify: git --versiongit version x.x.x
  • foundry
    • Verify: forge --versionforge 0.2.0 (...)

Quickstart

git clone https://github.com/SGDlab67/foundry-smart-contract-lottery-sgd
cd foundry-smart-contract-lottery-sgd
forge build

Usage

Start a local node

make anvil

Library

If you encounter issues installing the Chainlink library:

forge install smartcontractkit/chainlink-brownie-contracts@0.6.1 --no-commit

Deploy

Deploy to local node (must be running in another terminal):

make deploy

Deploy - Other Network

See Deployment to a testnet or mainnet

Testing

This repo covers the following test tiers:

Tier Type Covered
1 Unit
2 Integration
3 Forked
4 Staging
# Run all tests
forge test

# Run with fork
forge test --fork-url $SEPOLIA_RPC_URL

Test Coverage

forge coverage

Deployment to a testnet or mainnet

1. Setup environment variables

Create a .env file (see .env.example):

  • PRIVATE_KEY: Your wallet private key (use a dev wallet with no real funds)
  • SEPOLIA_RPC_URL: Sepolia testnet RPC URL (free from Alchemy)
  • ETHERSCAN_API_KEY: (Optional) For contract verification

2. Get testnet ETH

Visit faucets.chain.link to get Sepolia ETH.

3. Deploy

make deploy ARGS="--network sepolia"

This automatically:

  • Creates a Chainlink VRF Subscription
  • Adds your contract as a consumer

4. Register Chainlink Automation Upkeep

  1. Go to automation.chain.link
  2. Register new upkeep with Custom logic trigger
  3. Documentation

Automation

Scripts

# Enter raffle
cast send <RAFFLE_CONTRACT_ADDRESS> "enterRaffle()" --value 0.1ether --private-key <PRIVATE_KEY> --rpc-url $SEPOLIA_RPC_URL

# Create VRF Subscription
make createSubscription ARGS="--network sepolia"

Estimate gas

forge snapshot

Output: .gas-snapshot


Formatting

forge fmt

Additional Info

About chainlink-brownie-contracts

The chainlink-brownie-contracts repository is official - owned and maintained by the Chainlink team (smartcontractkit org).

Why use it?

  • Official releases come through npm
  • This repo packages npm releases for Foundry compatibility
  • Avoids using unreleased code from the main chainlink repo

Acknowledgments

This project was built following the Cyfrin Updraft Foundry Fundamentals course by Patrick Collins.

Course Progress: ✅ Foundry Fundamentals Complete!


Author

SGDlab67

Built with 🔨 Foundry | Powered by ⛓️ Chainlink


If you found this helpful, feel free to star the repo!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors