Computing Supply & Borrows
Monitoring Instructions:
  • refreshreserve - handles all the tracking work + updates ratio
  • refreshObligation - computes obligation balances based on reserve ratios
For Supply:
  • cToken Ratio acts as a % ownership of the LP
  • Imagine a pool with 99USDC, you deposit 1 USDC, you will own 1% of the pool
  • When the RefreshReserve occurs, the pool now has 110 USDC. Your 1% is now worth 1.1 USDC.
  • Total Reserves / cTokens Minted
For Borrows:
  • APY is used to update CumulativeBorrowRate as an index
  • When borrowing, calculate the initial CumulativeBorrowRate (lets say 1)
  • RefreshReserve → Updates the CumulativeBorrowRate (lets say its 1.02 now)
  • Obligation' borrows would be increased by *1.02/1 (2%).
1
/// Compound current borrow rate over elapsed slots
2
fn compound_interest(
3
&mut self,
4
current_borrow_rate: Rate,
5
slots_elapsed: u64,
6
) -> ProgramResult {
7
let slot_interest_rate = current_borrow_rate.try_div(SLOTS_PER_YEAR)?;
8
let compounded_interest_rate = Rate::one()
9
.try_add(slot_interest_rate)?
10
.try_pow(slots_elapsed)?;
11
self.cumulative_borrow_rate_wads = self
12
.cumulative_borrow_rate_wads
13
.try_mul(compounded_interest_rate)?;
14
self.borrowed_amount_wads = self
15
.borrowed_amount_wads
16
.try_mul(compounded_interest_rate)?;
17
Ok(())
18
}
19
}
Copied!
Why: Efficiency, this way RefreshReserve just has to update the cToken Rate or the CumulativeBorrowRate instead of updating every Obligation’s Supply or Borrow numbers. Tracking is only does at the ratio/index level, and never at an individual level unless changes to his positions are being made.
Copy link