diff --git a/token-lending/program/src/processor.rs b/token-lending/program/src/processor.rs index f22fe97e631..492f96577db 100644 --- a/token-lending/program/src/processor.rs +++ b/token-lending/program/src/processor.rs @@ -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, }, }; @@ -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())?; diff --git a/token-lending/program/src/state/reserve.rs b/token-lending/program/src/state/reserve.rs index 068df08bf9c..f2ea8647d8a 100644 --- a/token-lending/program/src/state/reserve.rs +++ b/token-lending/program/src/state/reserve.rs @@ -330,24 +330,13 @@ impl Reserve { } /// Calculate protocol fee redemption accounting for availible liquidity and accumulated fees - pub fn calculate_redeem_fees(&self) -> Result { - // 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 { + 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, - }) + )) } } @@ -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 { @@ -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(()) }