Skip to content

Commit

Permalink
implement penalty sweep (anchor 29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider committed Mar 10, 2024
1 parent 7ff8b26 commit 0d53857
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 13 additions & 5 deletions programs/openbook-v2/src/instructions/sweep_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@ use crate::token_utils::*;
pub fn sweep_fees(ctx: Context<SweepFees>) -> Result<()> {
let mut market = ctx.accounts.market.load_mut()?;

let amount = market.fees_available;
let fee_amount = market.fees_available;
market.fees_available = 0;
market.quote_deposit_total -= amount;
market.quote_deposit_total -= fee_amount;

let seeds = market_seeds!(market, ctx.accounts.market.key());
drop(market);

token_transfer_signed(
amount,
fee_amount,
&ctx.accounts.token_program,
&ctx.accounts.market_quote_vault,
&ctx.accounts.token_receiver_account,
&ctx.accounts.market_authority,
seeds,
)?;

let rent = Rent::get()?;
let rent_exempt_amount = rent.minimum_balance(ctx.accounts.market.to_account_info().data_len());
let penalty_amount = ctx.accounts.market.to_account_info().lamports().saturating_sub(rent_exempt_amount);
ctx.accounts.market.sub_lamports(penalty_amount)?;
ctx.accounts.collect_fee_admin.add_lamports(penalty_amount)?;

emit!(SweepFeesLog {
market: ctx.accounts.market.key(),
amount,
receiver: ctx.accounts.token_receiver_account.key(),
fee_amount,
fee_receiver: ctx.accounts.token_receiver_account.key(),
penalty_amount,
penalty_receiver: ctx.accounts.collect_fee_admin.key(),
});

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions programs/openbook-v2/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ pub struct SettleFundsLog {
#[event]
pub struct SweepFeesLog {
pub market: Pubkey,
pub amount: u64,
pub receiver: Pubkey,
pub fee_amount: u64,
pub fee_receiver: Pubkey,
pub penalty_amount: u64,
pub penalty_receiver: Pubkey,
}

#[event]
Expand Down

0 comments on commit 0d53857

Please sign in to comment.