Skip to content

web3pavlou/foundry-smart-contract-lottery-f23

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foundry Smart Contract Lottery

License: MIT


Description

A sample Ethereum smart contract lottery built with Foundry, featuring robust testing, Chainlink VRF v2.5 support, mock infrastructure, and modular, maintainable scripts.

Chainlink VRF integration ensures cryptographically secure randomness for winner selection.


Getting Started

Requirements

Confirm your tools are installed:

git --version
forge --version
anvil --version

Quickstart

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

Usage

Start a Local Node

Start Anvil in a separate terminal:

anvil
# or with Makefile:
make anvil

⚠️ Common Issue: “You seem to be using Foundry's default sender”

Block Underflow Error When Deploying Locally

When running locally, you might see:

[Revert] panic: arithmetic underflow or overflow (0x11)

Why?

  • Some Chainlink mocks use blockhash(block.number - 1). If the local chain is new (block.number == 0), this will underflow.

Solution:

  • Open a second terminal and run:

    cast rpc evm_mine

    This increments the block number so block.number - 1 is safe.

  • Then re-run your deployment script.


If you see:

Error: You seem to be using Foundry's default sender. Be sure to set your own --sender.
  • This appears if you don’t provide --sender explicitly.

  • If you use --private-key, your script broadcasts from the correct account—the warning can be safely ignored if everything else works.

  • To silence, use:

    forge script script/DeployRaffle.s.sol:DeployRaffle \
      --rpc-url http://localhost:8545 \
      --private-key <YOUR_PRIVATE_KEY> \
      --sender <YOUR_ADDRESS> \
      --broadcast

Deployment to Testnet or Mainnet

  1. Setup Environment Variables

    Create a .env file based on .env.example:

    • PRIVATE_KEY: your dev wallet (never use real funds!)
    • SEPOLIA_RPC_URL: Sepolia testnet RPC URL
    • ETHERSCAN_API_KEY: (optional) for contract verification
  2. Get Testnet ETH

    Use faucets.chain.link to get Sepolia ETH.

  3. Deploy

    make deploy ARGS="--network sepolia"
    • Sets up Chainlink VRF subscription
    • Adds your contract as VRF consumer
    • If you have an existing sub, update in script/HelperConfig.s.sol
  4. Register Chainlink Automation Upkeep


Testing

Covers all four Foundry test tiers:

  • Unit
  • Integration
  • Forked (mainnet/testnet fork)
  • Staging (public testnets)

Running Tests

Unit & Integration:

forge test
forge test -vvv  # with traces

Forked:

forge test --fork-url $SEPOLIA_RPC_URL

Staging (on testnet):

forge script script/StagingTest.s.sol --rpc-url $SEPOLIA_RPC_URL --broadcast

Test Coverage

forge coverage

Scripts

Enter the Raffle

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

Create a Chainlink VRF Subscription

make createSubscription ARGS="--network sepolia"

Formatting

forge fmt

Security

  • Solidity >=0.8.x (overflow protection)
  • Chainlink VRF/Upkeep best practices
  • Comprehensive tests, access control, events
  • Never use mainnet private keys for dev/test!

Further reading: Smart Contract Security Best Practices


Additional Resources


License

This project is licensed under the MIT License.


Acknowledgements

  • Cyfrin Updraft – Thanks to @patrickalphaC
  • Chainlink – For the VRF service
  • Foundry – For the dev tools


About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published