diff --git a/programs/bets/src/error.rs b/programs/bets/src/error.rs index ef4aef7..2202edb 100644 --- a/programs/bets/src/error.rs +++ b/programs/bets/src/error.rs @@ -11,4 +11,10 @@ pub enum ErrorCode { pub enum BettingError { #[msg("Bet is not filled yet!")] BetNotFilled, +} + +#[error_code] +pub enum BetError { + #[msg("The winner must be either the owner or the joiner of the bet.")] + InvalidWinner, } \ No newline at end of file diff --git a/programs/bets/src/instructions/close_bet.rs b/programs/bets/src/instructions/close_bet.rs new file mode 100644 index 0000000..4338799 --- /dev/null +++ b/programs/bets/src/instructions/close_bet.rs @@ -0,0 +1,36 @@ +use anchor_lang::prelude::*; +use crate::{error::BetError, *}; + +pub fn close(ctx: Context, winner:Pubkey)->Result<()>{ + let bet_vault = &mut ctx.accounts.bet_vault; + require!( + bet_vault.owner == winner || bet_vault.joiner == winner, + BetError::InvalidWinner + ); + + let bets_list = &mut ctx.accounts.bets_list; + // Remove the bet_vault public key from the list + bets_list.bets.retain(|&bet| bet != bet_vault.key()); + + msg!("Bet {} closed by {}", bet_vault.key(), winner); + + + Ok(()) +} + + +#[derive(Accounts)] +pub struct CloseBet<'info>{ + #[account(mut)] + pub bets_list: Account<'info, BetsList>, + + #[account(mut, close=winner)] + pub bet_vault: Account<'info, BetVault>, + + #[account(mut)] + pub winner: SystemAccount<'info>, + + #[account(mut)] + pub payer: Signer<'info>, + pub system_program: Program<'info, System> +} \ No newline at end of file diff --git a/programs/bets/src/instructions/join_bet.rs b/programs/bets/src/instructions/join_bet.rs index c9755e5..c1a67b4 100644 --- a/programs/bets/src/instructions/join_bet.rs +++ b/programs/bets/src/instructions/join_bet.rs @@ -24,6 +24,7 @@ pub fn join(ctx: Context, _game_id:String) ->Result<()>{ msg!("Joined bet {}!", bet_vault.key()); Ok(()) + } #[derive(Accounts)] diff --git a/programs/bets/src/instructions/mod.rs b/programs/bets/src/instructions/mod.rs index 48aa8c3..29cd29f 100644 --- a/programs/bets/src/instructions/mod.rs +++ b/programs/bets/src/instructions/mod.rs @@ -5,4 +5,7 @@ pub mod create_bet; pub use create_bet::*; pub mod join_bet; -pub use join_bet::*; \ No newline at end of file +pub use join_bet::*; + +pub mod close_bet; +pub use close_bet::*; \ No newline at end of file diff --git a/programs/bets/src/lib.rs b/programs/bets/src/lib.rs index b6652a9..a8b0a34 100644 --- a/programs/bets/src/lib.rs +++ b/programs/bets/src/lib.rs @@ -19,15 +19,15 @@ pub mod bets { initialize_bets_list::init(ctx) } - pub fn create_bet(ctx: Context, wager:u64, game_id:String)-> Result<()>{ - let clock = Clock::get().unwrap(); - let seed = clock.unix_timestamp as u64; - let nonce = seed % 100000; - + pub fn create_bet(ctx: Context, wager:u64, game_id:String, nonce:u64)-> Result<()>{ create_bet::create(ctx, wager,game_id,nonce) } pub fn join_bet(ctx: Context, game_id:String) -> Result<()>{ join_bet::join(ctx, game_id) } + + pub fn close_bet(ctx:Context, winner:Pubkey)->Result<()> { + close_bet::close(ctx, winner) + } }