From 5e0e4b48e2d0e24f4f5e5ecd3e1ec2b083235e6b Mon Sep 17 00:00:00 2001 From: Sewmina Dilshan Date: Fri, 4 Apr 2025 15:01:12 +0530 Subject: [PATCH] join check --- Anchor.toml | 2 +- programs/bets/src/error.rs | 6 ++---- programs/bets/src/instructions/close_bet.rs | 4 ++-- programs/bets/src/instructions/create_bet.rs | 2 +- programs/bets/src/instructions/join_bet.rs | 6 +++++- programs/bets/src/lib.rs | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Anchor.toml b/Anchor.toml index 5d1257b..5b6b0a6 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -5,7 +5,7 @@ resolution = true skip-lint = false [programs.localnet] -bets = "HxsDuhD7wcPxcMsrYdteMYxkffuwff8HoxhZ7NuFtM37" +bets = "JAf3ZkQ469okXAzA6BKJeKBb9ZkCtZanULaUsapskoyn" [registry] url = "https://api.apr.dev" diff --git a/programs/bets/src/error.rs b/programs/bets/src/error.rs index 2202edb..8322399 100644 --- a/programs/bets/src/error.rs +++ b/programs/bets/src/error.rs @@ -11,10 +11,8 @@ pub enum ErrorCode { pub enum BettingError { #[msg("Bet is not filled yet!")] BetNotFilled, -} - -#[error_code] -pub enum BetError { + #[msg("Bet is already filled")] + BetAlreadyFilled, #[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 index 4338799..f9d9fe8 100644 --- a/programs/bets/src/instructions/close_bet.rs +++ b/programs/bets/src/instructions/close_bet.rs @@ -1,11 +1,11 @@ use anchor_lang::prelude::*; -use crate::{error::BetError, *}; +use crate::{error::BettingError, *}; 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 + BettingError::InvalidWinner ); let bets_list = &mut ctx.accounts.bets_list; diff --git a/programs/bets/src/instructions/create_bet.rs b/programs/bets/src/instructions/create_bet.rs index 342d7d9..86e4462 100644 --- a/programs/bets/src/instructions/create_bet.rs +++ b/programs/bets/src/instructions/create_bet.rs @@ -29,7 +29,7 @@ pub fn create(ctx: Context, wager: u64, user_id:String, game_id:Strin } #[derive(Accounts)] -#[instruction(wager: u64, game_id: String, _nonce:u64)] +#[instruction(wager: u64,user_id:String, game_id: String, _nonce:u64)] pub struct CreateBet<'info> { #[account(mut)] pub payer: Signer<'info>, diff --git a/programs/bets/src/instructions/join_bet.rs b/programs/bets/src/instructions/join_bet.rs index 48a8349..ecae4a0 100644 --- a/programs/bets/src/instructions/join_bet.rs +++ b/programs/bets/src/instructions/join_bet.rs @@ -1,8 +1,12 @@ use anchor_lang::prelude::*; -use crate::*; +use crate::{error::BettingError, *}; pub fn join(ctx: Context,user_id:String, _game_id:String) ->Result<()>{ let bet_vault = &mut ctx.accounts.bet_vault; + require!( + bet_vault.joiner == Pubkey::default(), + BettingError::BetAlreadyFilled + ); let payer= &ctx.accounts.payer; let ix = anchor_lang::solana_program::system_instruction::transfer( diff --git a/programs/bets/src/lib.rs b/programs/bets/src/lib.rs index 598e5a0..b0f1737 100644 --- a/programs/bets/src/lib.rs +++ b/programs/bets/src/lib.rs @@ -9,7 +9,7 @@ pub use constants::*; pub use instructions::*; pub use state::*; -declare_id!("HxsDuhD7wcPxcMsrYdteMYxkffuwff8HoxhZ7NuFtM37"); +declare_id!("JAf3ZkQ469okXAzA6BKJeKBb9ZkCtZanULaUsapskoyn"); #[program] pub mod bets {