|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::token::{self, Token, TokenAccount, Transfer}; |
| 3 | + |
| 4 | +declare_id!("Lottery1111111111111111111111111111"); |
| 5 | + |
| 6 | +#[account] |
| 7 | +pub struct LotteryTicket { |
| 8 | + pub player: Pubkey, |
| 9 | + pub numbers: [u8; 6], |
| 10 | + pub bet_amount: u64, |
| 11 | + pub draw_id: u64, |
| 12 | + pub timestamp: i64, |
| 13 | + pub bump: u8, |
| 14 | +} |
| 15 | + |
| 16 | +impl LotteryTicket { |
| 17 | + pub const LEN: usize = 8 + 32 + 6 + 8 + 8 + 8 + 1; |
| 18 | +} |
| 19 | + |
| 20 | +#[account] |
| 21 | +pub struct LotteryDraw { |
| 22 | + pub draw_id: u64, |
| 23 | + pub winning_numbers: [u8; 6], |
| 24 | + pub prize_pool: u64, |
| 25 | + pub tickets: u64, |
| 26 | + pub drawn: bool, |
| 27 | + pub timestamp: i64, |
| 28 | + pub bump: u8, |
| 29 | +} |
| 30 | + |
| 31 | +impl LotteryDraw { |
| 32 | + pub const LEN: usize = 8 + 8 + 6 + 8 + 8 + 1 + 8 + 1; |
| 33 | +} |
| 34 | + |
| 35 | +#[program] |
| 36 | +pub mod lottery { |
| 37 | + use super::*; |
| 38 | + |
| 39 | + pub fn initialize(ctx: Context<Initialize>, min_bet: u64, max_bet: u64) -> Result<()> { |
| 40 | + let config = &mut ctx.accounts.config; |
| 41 | + config.authority = ctx.accounts.authority.key(); |
| 42 | + config.treasury = ctx.accounts.treasury.key(); |
| 43 | + config.min_bet = min_bet; |
| 44 | + config.max_bet = max_bet; |
| 45 | + config.house_edge_bps = 0; // No house edge for lottery |
| 46 | + config.paused = false; |
| 47 | + config.bump = ctx.bumps.config; |
| 48 | + Ok(()) |
| 49 | + } |
| 50 | + |
| 51 | + pub fn buy_ticket(ctx: Context<BuyTicket>, bet_amount: u64, numbers: [u8; 6], draw_id: u64) -> Result<()> { |
| 52 | + let config = &ctx.accounts.config; |
| 53 | + require!(!config.paused, common::CasinoError::GameNotActive); |
| 54 | + common::validate_bet(bet_amount, config.min_bet, config.max_bet)?; |
| 55 | + |
| 56 | + // Validate numbers (1-49) |
| 57 | + for &num in &numbers { |
| 58 | + require!(num >= 1 && num <= 49, common::CasinoError::InvalidBetAmount); |
| 59 | + } |
| 60 | + |
| 61 | + // Transfer bet to prize pool |
| 62 | + let cpi_accounts = Transfer { |
| 63 | + from: ctx.accounts.player_token_account.to_account_info(), |
| 64 | + to: ctx.accounts.pool_token_account.to_account_info(), |
| 65 | + authority: ctx.accounts.player.to_account_info(), |
| 66 | + }; |
| 67 | + let cpi_program = ctx.accounts.token_program.to_account_info(); |
| 68 | + let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); |
| 69 | + token::transfer(cpi_ctx, bet_amount)?; |
| 70 | + |
| 71 | + // Update draw pool |
| 72 | + let draw = &mut ctx.accounts.draw; |
| 73 | + draw.prize_pool = draw.prize_pool |
| 74 | + .checked_add(bet_amount) |
| 75 | + .ok_or(common::CasinoError::MathOverflow)?; |
| 76 | + draw.tickets = draw.tickets |
| 77 | + .checked_add(1) |
| 78 | + .ok_or(common::CasinoError::MathOverflow)?; |
| 79 | + |
| 80 | + // Create ticket |
| 81 | + let ticket = &mut ctx.accounts.ticket; |
| 82 | + ticket.player = ctx.accounts.player.key(); |
| 83 | + ticket.numbers = numbers; |
| 84 | + ticket.bet_amount = bet_amount; |
| 85 | + ticket.draw_id = draw_id; |
| 86 | + ticket.timestamp = Clock::get()?.unix_timestamp; |
| 87 | + ticket.bump = ctx.bumps.ticket; |
| 88 | + |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + |
| 92 | + pub fn draw_numbers(ctx: Context<DrawNumbers>) -> Result<()> { |
| 93 | + let draw = &mut ctx.accounts.draw; |
| 94 | + require!(!draw.drawn, common::CasinoError::GameAlreadySettled); |
| 95 | + |
| 96 | + // Generate 6 random numbers (1-49) |
| 97 | + let seed = Clock::get()?.unix_timestamp.to_le_bytes(); |
| 98 | + let mut numbers = [0u8; 6]; |
| 99 | + let mut used = [false; 50]; |
| 100 | + |
| 101 | + for i in 0..6 { |
| 102 | + let mut num; |
| 103 | + loop { |
| 104 | + num = (common::generate_random_from_seed(&[seed[i]], 48) + 1) as u8; |
| 105 | + if !used[num as usize] { |
| 106 | + used[num as usize] = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + numbers[i] = num; |
| 111 | + } |
| 112 | + |
| 113 | + numbers.sort(); |
| 114 | + draw.winning_numbers = numbers; |
| 115 | + draw.drawn = true; |
| 116 | + draw.timestamp = Clock::get()?.unix_timestamp; |
| 117 | + |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + |
| 121 | + pub fn claim_prize(ctx: Context<ClaimPrize>, matching_numbers: u8) -> Result<()> { |
| 122 | + let ticket = &ctx.accounts.ticket; |
| 123 | + let draw = &ctx.accounts.draw; |
| 124 | + require!(draw.drawn, common::CasinoError::InvalidGameState); |
| 125 | + require!(ticket.player == ctx.accounts.player.key(), common::CasinoError::Unauthorized); |
| 126 | + |
| 127 | + // Calculate matches |
| 128 | + let mut matches = 0; |
| 129 | + for &num in &ticket.numbers { |
| 130 | + if draw.winning_numbers.contains(&num) { |
| 131 | + matches += 1; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + require!(matches == matching_numbers, common::CasinoError::InvalidBetAmount); |
| 136 | + |
| 137 | + // Calculate prize based on matches |
| 138 | + let prize = match matches { |
| 139 | + 6 => draw.prize_pool, // Jackpot |
| 140 | + 5 => draw.prize_pool / 10, |
| 141 | + 4 => draw.prize_pool / 100, |
| 142 | + _ => 0, |
| 143 | + }; |
| 144 | + |
| 145 | + if prize > 0 { |
| 146 | + let seeds = &[ |
| 147 | + b"pool", |
| 148 | + ctx.accounts.draw.to_account_info().key.as_ref(), |
| 149 | + &[draw.bump], |
| 150 | + ]; |
| 151 | + let signer = &[&seeds[..]]; |
| 152 | + |
| 153 | + let cpi_accounts = Transfer { |
| 154 | + from: ctx.accounts.pool_token_account.to_account_info(), |
| 155 | + to: ctx.accounts.player_token_account.to_account_info(), |
| 156 | + authority: ctx.accounts.draw.to_account_info(), |
| 157 | + }; |
| 158 | + let cpi_program = ctx.accounts.token_program.to_account_info(); |
| 159 | + let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); |
| 160 | + token::transfer(cpi_ctx, prize)?; |
| 161 | + } |
| 162 | + |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +#[derive(Accounts)] |
| 168 | +pub struct Initialize<'info> { |
| 169 | + #[account( |
| 170 | + init, |
| 171 | + payer = authority, |
| 172 | + space = common::GameConfig::LEN, |
| 173 | + seeds = [b"config", b"lottery"], |
| 174 | + bump |
| 175 | + )] |
| 176 | + pub config: Account<'info, common::GameConfig>, |
| 177 | + |
| 178 | + #[account(mut)] |
| 179 | + pub authority: Signer<'info>, |
| 180 | + |
| 181 | + /// CHECK: Treasury account |
| 182 | + pub treasury: UncheckedAccount<'info>, |
| 183 | + |
| 184 | + pub system_program: Program<'info, System>, |
| 185 | +} |
| 186 | + |
| 187 | +#[derive(Accounts)] |
| 188 | +pub struct BuyTicket<'info> { |
| 189 | + #[account( |
| 190 | + seeds = [b"config", b"lottery"], |
| 191 | + bump = config.bump |
| 192 | + )] |
| 193 | + pub config: Account<'info, common::GameConfig>, |
| 194 | + |
| 195 | + #[account(mut)] |
| 196 | + pub player: Signer<'info>, |
| 197 | + |
| 198 | + #[account(mut)] |
| 199 | + pub player_token_account: Account<'info, TokenAccount>, |
| 200 | + |
| 201 | + #[account(mut)] |
| 202 | + pub pool_token_account: Account<'info, TokenAccount>, |
| 203 | + |
| 204 | + #[account( |
| 205 | + init_if_needed, |
| 206 | + payer = player, |
| 207 | + space = LotteryDraw::LEN, |
| 208 | + seeds = [b"draw", &draw_id.to_le_bytes()], |
| 209 | + bump |
| 210 | + )] |
| 211 | + pub draw: Account<'info, LotteryDraw>, |
| 212 | + |
| 213 | + #[account( |
| 214 | + init, |
| 215 | + payer = player, |
| 216 | + space = LotteryTicket::LEN, |
| 217 | + seeds = [b"ticket", player.key().as_ref(), &Clock::get()?.unix_timestamp.to_le_bytes()], |
| 218 | + bump |
| 219 | + )] |
| 220 | + pub ticket: Account<'info, LotteryTicket>, |
| 221 | + |
| 222 | + pub token_program: Program<'info, Token>, |
| 223 | + pub system_program: Program<'info, System>, |
| 224 | + pub clock: Sysvar<'info, Clock>, |
| 225 | +} |
| 226 | + |
| 227 | +#[derive(Accounts)] |
| 228 | +pub struct DrawNumbers<'info> { |
| 229 | + #[account(mut)] |
| 230 | + pub draw: Account<'info, LotteryDraw>, |
| 231 | + pub authority: Signer<'info>, |
| 232 | + pub clock: Sysvar<'info, Clock>, |
| 233 | +} |
| 234 | + |
| 235 | +#[derive(Accounts)] |
| 236 | +pub struct ClaimPrize<'info> { |
| 237 | + #[account(mut)] |
| 238 | + pub ticket: Account<'info, LotteryTicket>, |
| 239 | + |
| 240 | + #[account(mut)] |
| 241 | + pub draw: Account<'info, LotteryDraw>, |
| 242 | + |
| 243 | + #[account(mut)] |
| 244 | + pub player: Signer<'info>, |
| 245 | + |
| 246 | + #[account(mut)] |
| 247 | + pub player_token_account: Account<'info, TokenAccount>, |
| 248 | + |
| 249 | + #[account(mut)] |
| 250 | + pub pool_token_account: Account<'info, TokenAccount>, |
| 251 | + |
| 252 | + pub token_program: Program<'info, Token>, |
| 253 | +} |
| 254 | + |
0 commit comments