Skip to content

Commit

Permalink
add in fees when compounding and make utilization_rate stable across …
Browse files Browse the repository at this point in the history
…claims
  • Loading branch information
nope-finance committed Jun 6, 2022
1 parent 681cd6a commit f81a9ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 6 additions & 1 deletion token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ impl ReserveLiquidity {
if total_supply == Decimal::zero() {
return Ok(Rate::zero());
}
self.borrowed_amount_wads.try_div(total_supply)?.try_into()
self.borrowed_amount_wads
.try_add(self.accumulated_protocol_fees_wads)?
.try_div(total_supply)?
.try_into()
}

/// Compound current borrow rate over elapsed slots
Expand All @@ -487,7 +490,9 @@ impl ReserveLiquidity {

let net_new_debt = self
.borrowed_amount_wads
.try_add(self.accumulated_protocol_fees_wads)?
.try_mul(compounded_interest_rate)?
.try_sub(self.accumulated_protocol_fees_wads)?
.try_sub(self.borrowed_amount_wads)?;

let delta_accumulated_protocol_fees = net_new_debt.try_mul(take_rate)?;
Expand Down
12 changes: 6 additions & 6 deletions token-lending/program/tests/redeem_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ async fn test_success() {
);

// utilization increases because redeeming adds to borrows and takes from availible
assert!(
usdc_reserve_before.liquidity.utilization_rate().unwrap()
< usdc_reserve.liquidity.utilization_rate().unwrap(),
assert_eq!(
usdc_reserve_before.liquidity.utilization_rate().unwrap(),
usdc_reserve.liquidity.utilization_rate().unwrap()
);
assert!(
sol_reserve_before.liquidity.utilization_rate().unwrap()
< sol_reserve.liquidity.utilization_rate().unwrap(),
assert_eq!(
sol_reserve_before.liquidity.utilization_rate().unwrap(),
sol_reserve.liquidity.utilization_rate().unwrap()
);
assert_eq!(
sol_reserve.liquidity.cumulative_borrow_rate_wads,
Expand Down
2 changes: 1 addition & 1 deletion 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

0 comments on commit f81a9ed

Please sign in to comment.