Skip to content

Commit

Permalink
fix: check math in fee claim logic (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbpvsc authored May 3, 2024
1 parent abe574e commit 5828906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions clients/rust/marginfi-cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ Emissions:
Rate: {:?}
Mint: {:?}
Remaining: {:?}
Unclaimed
Fees: {:?}
Insurance: {:?}
Last Update: {:?}h ago ({})
"#,
bank.group,
Expand Down Expand Up @@ -198,6 +201,10 @@ Last Update: {:?}h ago ({})
I80F48::from(bank.emissions_rate),
bank.emissions_mint,
I80F48::from(bank.emissions_remaining),
I80F48::from(bank.collected_group_fees_outstanding)
/ EXP_10_I80F48[bank.mint_decimals as usize],
I80F48::from(bank.collected_insurance_fees_outstanding)
/ EXP_10_I80F48[bank.mint_decimals as usize],
SystemTime::now()
.duration_since(UNIX_EPOCH + Duration::from_secs(bank.last_update as u64))
.unwrap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,41 @@ pub fn lending_pool_collect_bank_fees(ctx: Context<LendingPoolCollectBankFees>)
let mut available_liquidity = I80F48::from_num(liquidity_vault.amount);

let (insurance_fee_transfer_amount, new_outstanding_insurance_fees) = {
let outstanding_fees = I80F48::from(bank.collected_insurance_fees_outstanding);
let transfer_amount = min(outstanding_fees, available_liquidity).int();

(transfer_amount.int(), outstanding_fees - transfer_amount)
let outstanding = I80F48::from(bank.collected_insurance_fees_outstanding);
let transfer_amount = min(outstanding, available_liquidity).int();

(
transfer_amount.int(),
outstanding
.checked_sub(transfer_amount)
.ok_or_else(math_error!())?,
)
};

bank.collected_insurance_fees_outstanding = new_outstanding_insurance_fees.into();

available_liquidity -= insurance_fee_transfer_amount;
available_liquidity = available_liquidity
.checked_sub(insurance_fee_transfer_amount)
.ok_or_else(math_error!())?;

let (group_fee_transfer_amount, new_outstanding_group_fees) = {
let outstanding_fees = I80F48::from(bank.collected_group_fees_outstanding);
let transfer_amount = min(outstanding_fees, available_liquidity).int();

(transfer_amount.int(), outstanding_fees - transfer_amount)
let outstanding = I80F48::from(bank.collected_group_fees_outstanding);
let transfer_amount = min(outstanding, available_liquidity).int();

(
transfer_amount.int(),
outstanding
.checked_sub(transfer_amount)
.ok_or_else(math_error!())?,
)
};

available_liquidity = available_liquidity
.checked_sub(group_fee_transfer_amount)
.ok_or_else(math_error!())?;

assert!(available_liquidity >= I80F48::ZERO);

bank.collected_group_fees_outstanding = new_outstanding_group_fees.into();

bank.withdraw_spl_transfer(
Expand Down

0 comments on commit 5828906

Please sign in to comment.