This example demonstrates how to build a decentralized lottery using Switchboard's On-Demand Randomness functionality.
Welcome to the Switchboard Lottery example. This project showcases how to build a secure and fair lottery system using Switchboard's on-demand randomness solution on Solana.
To read more about the security guarantees that Switchboard Randomness On-Demand provides, please see: https://docs.switchboard.xyz/docs/switchboard/switchboard-randomness
Configure the anchor.toml
file to point to your solana wallet and the Solana cluster of your choice - Devnet, Mainnet, etc.
Then, build the program:
anchor build
After building, take note of your program address and insert it in your program lib.rs
file:
Note: You can view your program address with anchor keys list
declare_id!("[YOUR_PROGRAM_ADDRESS]");
Rebuild your program and deploy:
anchor build
anchor deploy
anchor idl init --filepath target/idl/lottery.json YOUR_PROGRAM_ADDRESS
Note: You may need to use anchor idl upgrade --filepath target/idl/lottery.json YOUR_PROGRAM_ADDRESS
if you are upgrading the program.
This example implements a decentralized lottery with the following features:
- Multiple concurrent lotteries identified by unique IDs
- Configurable entry fees and end times
- Fair winner selection using Switchboard's verifiable randomness
- 90% prize pool distribution to winner, 10% to developer
- Automatic prize claiming system
- Install Solana Tool Suite (1.17.0 or later)
- Install Anchor (0.30.0):
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force avm install 0.30.1 avm use 0.30.1
- Install Node.js
- Configure Solana CLI for devnet:
solana config set --url devnet
- Create a test wallet and fund it:
solana-keygen new -o test-wallet.json solana airdrop 2 test-wallet.json --url devnet
- Set
wallet=/path/to/test-wallet.json
inAnchor.toml
Note: You can get the cli config by using the solana config get
command.
-
Clone the repository:
git clone <repository-url> cd lottery
-
Build the program:
anchor clean # Clean any existing build artifacts anchor build
-
Get your program ID:
anchor keys list
-
Update program ID in two places:
- In
programs/lottery/src/lib.rs
:declare_id!("YOUR_PROGRAM_ID");
- In
Anchor.toml
:[programs.devnet] lottery = "YOUR_PROGRAM_ID"
- In
-
Build and deploy:
anchor build anchor deploy
-
Initialize IDL:
anchor idl init --filepath target/idl/lottery.json YOUR_PROGRAM_ID
-
Ensure you have sufficient SOL for testing:
solana balance # Should show at least 1 SOL
-
Run the test suite:
anchor test
-
If tests fail with account errors:
anchor clean anchor build anchor deploy
-
If you see program ID mismatches:
- Get the correct program ID:
anchor keys list
- Update both locations mentioned in Setup Step 5
- Rebuild and redeploy
- Get the correct program ID:
-
If you get IDL errors:
anchor idl upgrade --filepath target/idl/lottery.json YOUR_PROGRAM_ID
Note: Always ensure you're on devnet and have sufficient SOL before running tests.
-
Account Size Errors / Program Changes When making significant changes to the program's state or instruction parameters, you may need to:
cargo clean anchor build anchor keys list # Update program ID in lib.rs and Anchor.toml anchor build (again for safe measure) anchor deploy
This is necessary when:
- Adding/removing fields in account structs
- Changing account sizes
- Modifying instruction parameters
- Getting program/account size mismatch errors
- Encountering IDL inconsistencies
-
Program ID Mismatches If you see errors related to program IDs:
anchor keys list # View your program address # Update declare_id!() in lib.rs # Update Anchor.toml anchor build anchor deploy
-
IDL Updates After making program changes:
anchor idl init --filepath target/idl/lottery.json YOUR_PROGRAM_ADDRESS # Or if upgrading: anchor idl upgrade --filepath target/idl/lottery.json YOUR_PROGRAM_ADDRESS
anchor clean
vs cargo clean
:
-
anchor clean
: Cleans Anchor-specific build artifacts including:- Program keypairs
- IDL files
- Program deployment info
- TypeScript bindings
- All Cargo build artifacts
-
cargo clean
: Only cleans Rust/Cargo build artifacts:- Compiled program files
- Dependencies
- Does not touch Anchor-specific files
Use anchor clean
when you need a complete reset of your program's build state, especially after structural changes. Use cargo clean
when you only need to rebuild the Rust code.
solana config set --url devnet
solana config set --url devnet
RPC Providers: