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 8ea4877 commit 4b3c245
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 60 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
24 changes: 10 additions & 14 deletions token-lending/program/tests/refresh_obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(44_000);
test.set_bpf_compute_max_units(45_000);

const SOL_DEPOSIT_AMOUNT: u64 = 100;
const USDC_BORROW_AMOUNT: u64 = 1_000;
Expand Down Expand Up @@ -155,11 +155,8 @@ async fn test_success() {
.unwrap();
let protocol_take_rate = Rate::from_percent(usdc_reserve.config.protocol_take_rate);
let delta_accumulated_protocol_fees = net_new_debt.try_mul(protocol_take_rate).unwrap();
let delta_borrowed_amount_wads = net_new_debt
.try_sub(delta_accumulated_protocol_fees)
.unwrap();
let new_borrow_amount_wads = Decimal::from(USDC_BORROW_AMOUNT_FRACTIONAL)
.try_add(delta_borrowed_amount_wads)
.try_add(net_new_debt)
.unwrap();

let liquidity_price = liquidity.market_value.try_div(compound_borrow).unwrap();
Expand All @@ -170,18 +167,17 @@ async fn test_success() {
);
assert_eq!(liquidity.cumulative_borrow_rate_wads, compound_rate.into());
assert_eq!(
usdc_reserve
.liquidity
.borrowed_amount_wads
.try_add(delta_accumulated_protocol_fees)
.unwrap(),
usdc_reserve.liquidity.borrowed_amount_wads,
liquidity.borrowed_amount_wads
);
assert_eq!(liquidity.borrowed_amount_wads, new_borrow_amount_wads);
assert_eq!(
usdc_reserve.liquidity.accumulated_protocol_fees_wads,
delta_accumulated_protocol_fees
);
assert_eq!(
liquidity.borrowed_amount_wads,
new_borrow_amount_wads
.try_add(delta_accumulated_protocol_fees)
.unwrap()
sol_reserve.liquidity.accumulated_protocol_fees_wads,
Decimal::from(0u64)
);
assert_eq!(sol_reserve.liquidity.market_price, collateral_price,);
assert_eq!(usdc_reserve.liquidity.market_price, liquidity_price,);
Expand Down
19 changes: 9 additions & 10 deletions token-lending/program/tests/refresh_reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ async fn test_success() {
.unwrap();
let protocol_take_rate = Rate::from_percent(usdc_reserve.config.protocol_take_rate);
let delta_accumulated_protocol_fees = net_new_debt.try_mul(protocol_take_rate).unwrap();
let delta_borrowed_amount_wads = net_new_debt
.try_sub(delta_accumulated_protocol_fees)
.unwrap();
let new_borrow_amount_wads = Decimal::from(BORROW_AMOUNT)
.try_add(delta_borrowed_amount_wads)
.unwrap();

assert_eq!(
sol_reserve.liquidity.cumulative_borrow_rate_wads,
Expand All @@ -137,10 +131,7 @@ async fn test_success() {
sol_reserve.liquidity.cumulative_borrow_rate_wads,
usdc_reserve.liquidity.cumulative_borrow_rate_wads
);
assert_eq!(
sol_reserve.liquidity.borrowed_amount_wads,
new_borrow_amount_wads
);
assert_eq!(sol_reserve.liquidity.borrowed_amount_wads, compound_borrow);
assert_eq!(
sol_reserve.liquidity.borrowed_amount_wads,
usdc_reserve.liquidity.borrowed_amount_wads
Expand All @@ -153,4 +144,12 @@ async fn test_success() {
usdc_reserve.liquidity.market_price,
usdc_test_reserve.market_price
);
assert_eq!(
delta_accumulated_protocol_fees,
usdc_reserve.liquidity.accumulated_protocol_fees_wads
);
assert_eq!(
delta_accumulated_protocol_fees,
sol_reserve.liquidity.accumulated_protocol_fees_wads
);
}

0 comments on commit 4b3c245

Please sign in to comment.