diff --git a/programs/bets/src/constants.rs b/programs/bets/src/constants.rs index fae4535..a1cc8a8 100644 --- a/programs/bets/src/constants.rs +++ b/programs/bets/src/constants.rs @@ -2,3 +2,6 @@ use anchor_lang::prelude::*; #[constant] pub const SEED: &str = "anchor"; + +#[constant] +pub const FEE_COLLECTOR: &str= "cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc"; \ No newline at end of file diff --git a/programs/bets/src/error.rs b/programs/bets/src/error.rs index 8322399..f9e101d 100644 --- a/programs/bets/src/error.rs +++ b/programs/bets/src/error.rs @@ -15,4 +15,6 @@ pub enum BettingError { BetAlreadyFilled, #[msg("The winner must be either the owner or the joiner of the bet.")] InvalidWinner, + #[msg("Please use the correct fee collector address")] + InvalidFeeCollector } \ 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 f9d9fe8..b3d3c20 100644 --- a/programs/bets/src/instructions/close_bet.rs +++ b/programs/bets/src/instructions/close_bet.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use anchor_lang::prelude::*; use crate::{error::BettingError, *}; @@ -8,6 +10,20 @@ pub fn close(ctx: Context, winner:Pubkey)->Result<()>{ BettingError::InvalidWinner ); + // Check fee wallet is correct + let fee_collector_pubkey = Pubkey::from_str(FEE_COLLECTOR).map_err(|_| BettingError::InvalidFeeCollector)?; + require_keys_eq!( + ctx.accounts.fee_wallet.key(), + fee_collector_pubkey, + BettingError::InvalidFeeCollector + ); + + // Calculate the 5% fee + let total_lamports = **bet_vault.to_account_info().lamports.borrow(); + let fee = total_lamports / 20; // 5% + **bet_vault.to_account_info().try_borrow_mut_lamports()? -= fee; + **ctx.accounts.fee_wallet.to_account_info().try_borrow_mut_lamports()? += fee; + 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()); @@ -27,6 +43,10 @@ pub struct CloseBet<'info>{ #[account(mut, close=winner)] pub bet_vault: Account<'info, BetVault>, + /// CHECK: This is validated against the FEE_COLLECTOR constant. + #[account(mut)] + pub fee_wallet: AccountInfo<'info>, + #[account(mut)] pub winner: SystemAccount<'info>,