osmosis-labs / isotonic

Smart Contracts for the Lendex Protocol
MIT License
1 stars 0 forks source link

Query: APY endpoint #117

Closed uint closed 2 years ago

uint commented 2 years ago

Description

We want to provide a query for APY.

struct QueryMsg {
    // ...
    /// Returns ApyResponse
    Apy {},
}

There's a separate APY value for lender and borrower:

struct ApyResponse {
    /// If market conditions remain exactly the same for the next year, how much % interest will a borrower have to pay?
    borrower: Decimal,
    /// If market conditions remain exactly the same for the next year, how much % interest will a lender earn?
    lender: Decimal,
}

They are different because of:

Math

charge_periods = SECONDS_IN_YEAR / cfg.interest_charge_period
rate = cfg.rates.calculate_interest_rate(utilisation)
borrower_apy = (1 + rate/charge_periods)^(charge_periods) - 1
lender_apy = borrower_apy * utilisation * (1 - cfg.reserve_factor)

We calculate the multipliers in calculate_interest in a similar way - we could probably just reuse that.

Bonus points

Bonus points for verifying this math makes sense.