Documentation
¶
Index ¶
- Constants
- func CalculateExpiration(principal sdk.Coin, vaultReserves sdk.Coin, rate string, ...) (int64, error)
- func CalculateInterestEarned(principal sdk.Coin, rate string, periodSeconds int64) (sdkmath.Int, error)
- func CalculatePeriods(vaultReserves sdk.Coin, principal sdk.Coin, rate string, periodSeconds int64, ...) (int64, int64, error)
Constants ¶
const ( SecondsPerHour = 3_600 SecondsPerDay = 86_400 SecondsPerMonth = 2_628_000 SecondsPerYear = 31_536_000 // TODO What should this value be, and does it need to be hardcoded? CalculatePeriodsLimit = 2 * SecondsPerMonth CalculatePeriodsNoLimit = 0 EulerPrecision = 17 )
Variables ¶
This section is empty.
Functions ¶
func CalculateExpiration ¶
func CalculateExpiration(principal sdk.Coin, vaultReserves sdk.Coin, rate string, periodSeconds, startTime, limit int64) (int64, error)
CalculateExpiration determines the epoch time at which a vault will no longer be able to pay the required interest on a principal amount.
It simulates the compounding process period by period, tracking the vault's reserve balance. The expiration time is the start time of the first period for which the earned interest exceeds the vault's remaining reserves.
Parameters:
- principal: The initial amount of the asset earning interest.
- vaultReserves: The funds available in the vault to pay out as interest.
- rate: The annual interest rate (e.g., "0.04" for 4%).
- periodSeconds: The compounding interval in seconds.
- startTime: The epoch second when the interest calculation begins.
Returns: - The expiration time as an epoch second. - If the vault will never be depleted (e.g., zero interest rate), it returns the startTime. - An error if the inputs are invalid or a calculation fails.
func CalculateInterestEarned ¶
func CalculateInterestEarned(principal sdk.Coin, rate string, periodSeconds int64) (sdkmath.Int, error)
CalculateInterestEarned computes the continuously compounded interest for a given principal over a period.
It uses the formula `Interest = P * (e^(rt)) - P`, where:
- P is the `principal`.
- r is the annual `rate`.
- t is the time in years, derived from (`periodSeconds` / 31_536_000).
This function returns the interest as a `cosmosmath.Int`, truncating any fractional part to ensure compatibility with coin amounts. It uses deterministic arithmetic via `cosmosmath.LegacyDec` and approximates e^x using a Maclaurin series (`utils.ExpDec`).
func CalculatePeriods ¶
func CalculatePeriods( vaultReserves sdk.Coin, principal sdk.Coin, rate string, periodSeconds int64, limit int64, ) (int64, int64, error)
CalculatePeriods simulates continuous compounding to determine how many compounding periods a vault can sustain paying interest before its reserves are depleted.
The interest for each period is calculated using the continuous compounding formula, and added or subtracted from the principal depending on the sign of the rate.
Parameters:
- vaultReserves: The available funds in the vault to pay interest.
- principal: The initial amount earning interest.
- rate: The annual interest rate (as a string, e.g. "0.05" for 5%, "-0.05" for -5%).
- periodSeconds: The length of each compounding period in seconds.
- limit: The maximum total duration (in seconds) to simulate. Once the total simulated time reaches or exceeds this limit, the loop stops, even if reserves are not fully depleted. A limit of 0 means no limit.
Returns:
- The number of full compounding periods the reserves can cover (up to the limit).
- A placeholder value (currently always zero) for potential future use.
- An error if interest calculation fails or input is invalid.
Types ¶
This section is empty.