Skip to content

Commit

Permalink
properly keeping track of availible amount when redeeming fees
Browse files Browse the repository at this point in the history
  • Loading branch information
nope-finance committed Jun 2, 2022
1 parent d532f16 commit 2901de9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use solana_program::{
};
use spl_token::solana_program::instruction::AccountMeta;
use spl_token::state::{Account, Mint};
use std::{convert::TryInto, result::Result};
use std::{convert::TryInto, result::Result, cmp::min};
use switchboard_program::{
get_aggregator, get_aggregator_result, AggregatorState, RoundResult, SwitchboardAccountType,
};
Expand Down Expand Up @@ -2256,16 +2256,21 @@ fn process_redeem_fees(program_id: &Pubkey, accounts: &[AccountInfo]) -> Program
return Err(LendingError::InvalidMarketAuthority.into());
}

let fee_receiver_claimable_amount = reserve
let mut fee_receiver_claimable_amount = reserve
.liquidity
.accumulated_protocol_fees_wads
.try_floor_u64()?;

fee_receiver_claimable_amount = min(fee_receiver_claimable_amount, reserve.liquidity.available_amount);

reserve.liquidity.accumulated_protocol_fees_wads = reserve
.liquidity
.accumulated_protocol_fees_wads
.try_sub(Decimal::from(fee_receiver_claimable_amount))?;

// for accounting purposes we need to subtract from availible amount and add to borrowed amount
reserve.liquidity.borrow(Decimal::from(fee_receiver_claimable_amount))?;

Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;

spl_token_transfer(TokenTransferParams {
Expand Down
10 changes: 10 additions & 0 deletions token-lending/program/tests/redeem_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,18 @@ async fn test_success() {
.unwrap();
let new_borrow_amount_wads = Decimal::from(BORROW_AMOUNT)
.try_add(delta_borrowed_amount_wads)
.unwrap()
.try_add(Decimal::from(usdc_balance_after - usdc_balance_before))
.unwrap();

assert_eq!(
usdc_reserve_before.liquidity.total_supply(),
usdc_reserve.liquidity.total_supply(),
);
assert_eq!(
sol_reserve_before.liquidity.total_supply(),
sol_reserve.liquidity.total_supply(),
);
assert_eq!(
sol_reserve.liquidity.cumulative_borrow_rate_wads,
compound_rate.into()
Expand Down

0 comments on commit 2901de9

Please sign in to comment.