Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nope-finance committed Jun 10, 2022
1 parent ad4b495 commit 742e436
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
15 changes: 5 additions & 10 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::{
math::{Decimal, Rate, TryAdd, TryDiv, TryMul, TrySub, WAD},
pyth,
state::{
CalculateBorrowResult, CalculateLiquidationResult, CalculateRedeemFeesResult,
CalculateRepayResult, InitLendingMarketParams, InitObligationParams, InitReserveParams,
LendingMarket, NewReserveCollateralParams, NewReserveLiquidityParams, Obligation, Reserve,
CalculateBorrowResult, CalculateLiquidationResult, CalculateRepayResult,
InitLendingMarketParams, InitObligationParams, InitReserveParams, LendingMarket,
NewReserveCollateralParams, NewReserveLiquidityParams, Obligation, Reserve,
ReserveCollateral, ReserveConfig, ReserveLiquidity,
},
};
Expand Down Expand Up @@ -2261,17 +2261,12 @@ fn process_redeem_fees(program_id: &Pubkey, accounts: &[AccountInfo]) -> Program
return Err(LendingError::InvalidMarketAuthority.into());
}

let CalculateRedeemFeesResult {
settle_amount,
withdraw_amount,
} = reserve.calculate_redeem_fees()?;
let withdraw_amount = reserve.calculate_redeem_fees()?;
if withdraw_amount == 0 {
return Err(LendingError::InsufficientProtocolFeesToRedeem.into());
}

reserve
.liquidity
.redeem_fees(settle_amount, withdraw_amount)?;
reserve.liquidity.redeem_fees(withdraw_amount)?;
reserve.last_update.mark_stale();
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;

Expand Down
33 changes: 7 additions & 26 deletions token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,13 @@ impl Reserve {
}

/// Calculate protocol fee redemption accounting for availible liquidity and accumulated fees
pub fn calculate_redeem_fees(&self) -> Result<CalculateRedeemFeesResult, ProgramError> {
// idt i need to do this but i wasn't sure if the collected fees were collecting as "virtual ctokens"
// let exchange_rate = self.collateral_exchange_rate()?;
// let accumulated_protocol_fees_as_liquidity = exchange_rate.decimal_collateral_to_liquidity(self.liquidity.accumulated_protocol_fees_wads)?;
// let withdraw_amount = min(self.liquidity.available_amount, accumulated_protocol_fees_as_liquidity.try_floor_u64()?);
// let settle_amount = exchange_rate.decimal_liquidity_to_collateral(Decimal::from(withdraw_amount))?;
let withdraw_amount = min(
pub fn calculate_redeem_fees(&self) -> Result<u64, ProgramError> {
Ok(min(
self.liquidity.available_amount,
self.liquidity
.accumulated_protocol_fees_wads
.try_floor_u64()?,
);
let settle_amount = Decimal::from(withdraw_amount);

Ok(CalculateRedeemFeesResult {
settle_amount,
withdraw_amount,
})
))
}
}

Expand Down Expand Up @@ -399,15 +388,6 @@ pub struct CalculateLiquidationResult {
pub withdraw_amount: u64,
}

/// Calculate redeem fees result
#[derive(Debug)]
pub struct CalculateRedeemFeesResult {
/// The Decimal amount of fees redeemed
pub settle_amount: Decimal,
/// Amount of liquidity to withdraw to redeem from the reserve
pub withdraw_amount: u64,
}

/// Reserve liquidity
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ReserveLiquidity {
Expand Down Expand Up @@ -509,13 +489,14 @@ impl ReserveLiquidity {
}

/// Subtract settle amount from accumulated_protocol_fees_wads and withdraw_amount from available liquidity
pub fn redeem_fees(&mut self, settle_amount: Decimal, withdraw_amount: u64) -> ProgramResult {
pub fn redeem_fees(&mut self, withdraw_amount: u64) -> ProgramResult {
self.available_amount = self
.available_amount
.checked_sub(withdraw_amount)
.ok_or(LendingError::MathOverflow)?;
self.accumulated_protocol_fees_wads =
self.accumulated_protocol_fees_wads.try_sub(settle_amount)?;
self.accumulated_protocol_fees_wads = self
.accumulated_protocol_fees_wads
.try_sub(Decimal::from(withdraw_amount))?;

Ok(())
}
Expand Down

0 comments on commit 742e436

Please sign in to comment.