This project is a smart contract for Solana, developed with Anchor, that implements an NFT lottery. It allows you to create lotteries, buy tickets, select winners randomly, and claim prizes.
The program is located in /programs/nft-lottery/ and is composed of the following modules:
- constants.rs: Defines seeds and constants used to derive accounts and PDAs.
- errors.rs: Lists custom dApp errors, such as ticket limit, expired time, winner not selected, etc.
- instructions/: Contains the logic for each instruction:
initialize_config.rs: Initializes global configuration and treasury.create_lottery.rs: Creates a new lottery and its NFT collection.buy_ticket.rs: Allows users to buy tickets for a lottery.commit_randomness_account.rs: Links the Switchboard randomness account.select_winner.rs: Selects the winner using Switchboard randomness.claim_prize.rs: Allows the winner to claim the prize.
- states/: Defines data structures:
GlobalConfig: Global program configuration (authority, treasury, fee, etc).Lottery: State of each lottery (id, collection, prize, winner, tickets, etc).
- Install JS dependencies
npm install
- Build the Anchor program
anchor build
- Set Solana to local environment
solana config set --url localhost - Run the setup scripts
- Navigate to the
/setupdirectory:cd setup - Make the setup scripts executable:
chmod +x ./setup-local.sh chmod +x ./start-validator.sh
- First, initialize the local environment:
./setup-local.sh
- Then, start the local validator:
./start-validator.sh
- Navigate to the
- Run Anchor tests in another terminal (without starting the local validator)
anchor test --skip-local-validator
- The project uses Switchboard for randomness.
- The scripts in
/setupprepare the environment and validator with required accounts and oracles. - The contract is written in Rust and uses Anchor to facilitate Solana development.
/programs/nft-lottery/: Contract source code./setup/: Scripts and files to set up the local environment./tests/: Automated tests.
franRappazzini
For technical questions, check the source files in /programs/nft-lottery/src/ and the scripts in /setup/.