Skip to content

A solana program for creating and participating in decentralized lotteries using switchboard vrf

Notifications You must be signed in to change notification settings

jongan69/lotteryprogram

Repository files navigation

Switchboard Logo

Switchboard On-Demand: Lottery Example

This example demonstrates how to build a decentralized lottery using Switchboard's On-Demand Randomness functionality.

Getting Started

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

PLEASE ENSURE YOU USE ANCHOR VERSION 0.30.0

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.

About the Lottery

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

Test Setup & Execution

Prerequisites

  1. Install Solana Tool Suite (1.17.0 or later)
  2. 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
  3. Install Node.js
  4. Configure Solana CLI for devnet:
    solana config set --url devnet
  5. Create a test wallet and fund it:
    solana-keygen new -o test-wallet.json
    solana airdrop 2 test-wallet.json --url devnet
  6. Set wallet=/path/to/test-wallet.json in Anchor.toml

Note: You can get the cli config by using the solana config get command.

Setup Steps

  1. Clone the repository:

    git clone <repository-url>
    cd lottery
  2. Build the program:

    anchor clean  # Clean any existing build artifacts
    anchor build
  3. Get your program ID:

    anchor keys list
  4. 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"
  5. Build and deploy:

    anchor build
    anchor deploy
  6. Initialize IDL:

    anchor idl init --filepath target/idl/lottery.json YOUR_PROGRAM_ID

Running Tests

  1. Ensure you have sufficient SOL for testing:

    solana balance  # Should show at least 1 SOL
  2. Run the test suite:

    anchor test

Troubleshooting Common Test Issues

  • If tests fail with account errors:

    anchor clean
    anchor build
    anchor deploy
  • If you see program ID mismatches:

    1. Get the correct program ID:
      anchor keys list
    2. Update both locations mentioned in Setup Step 5
    3. Rebuild and redeploy
  • 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.

Debugging

Common Issues

  1. 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
  2. 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
  3. 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

Build System Cleaning

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.

RPC Config

solana config set --url devnet
solana config set --url devnet

RPC Providers:

About

A solana program for creating and participating in decentralized lotteries using switchboard vrf

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages