fee collection

This commit is contained in:
Sewmina Dilshan 2025-04-05 04:46:38 +05:30
parent 5e0e4b48e2
commit 69b4a083e7
3 changed files with 25 additions and 0 deletions

View File

@ -2,3 +2,6 @@ use anchor_lang::prelude::*;
#[constant]
pub const SEED: &str = "anchor";
#[constant]
pub const FEE_COLLECTOR: &str= "cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc";

View File

@ -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
}

View File

@ -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<CloseBet>, 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>,