# Computing Supply & Borrows

**Monitoring Instructions:**

- RefreshReserve - handles all the tracking work and updates ratio
- RefreshObligation - computes obligation balances based on reserve ratios

**For Supply:**

- cToken Ratio acts as a % ownership of the LP
- For a pool with 99 USDC, if 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 (let's say 1)
- RefreshReserve → Updates the CumulativeBorrowRate (let's say its 1.02)
- Obligation borrows would be increased by *1.02/1 or 2%

/// Compound current borrow rate over elapsed slots

fn compound_interest(

&mut self,

current_borrow_rate: Rate,

slots_elapsed: u64,

) -> ProgramResult {

let slot_interest_rate = current_borrow_rate.try_div(SLOTS_PER_YEAR)?;

let compounded_interest_rate = Rate::one()

.try_add(slot_interest_rate)?

.try_pow(slots_elapsed)?;

self.cumulative_borrow_rate_wads = self

.cumulative_borrow_rate_wads

.try_mul(compounded_interest_rate)?;

self.borrowed_amount_wads = self

.borrowed_amount_wads

.try_mul(compounded_interest_rate)?;

Ok(())

}

}

**Why**: For efficiency, the RefreshReserve just has to update the cToken Rate or the CumulativeBorrowRate instead of updating every obligation’s supply or borrow numbers. Tracking is only done at the ratio/index level, and never at an individual level unless changes to the user's positions are being made.

Last modified 1yr ago