Skip to main content

rate_per_second_to_apy

Schema: lib_math

Description

Converts a rate-per-second value into annual percentage yield (APY). This function is particularly useful in financial calculations where interest rates are represented in terms of per-second rates and need to be converted into an annualized form. For example, Compound-based lending pools store interest rates as rates-per-second.

In addition to the rate_per_second_to_apy function, a variant named rate_per_second_to_apy_percentage also exists. The only difference between these two functions is that rate_per_second_to_apy uses a 0-1 scale, where-as the rate_per_second_to_apy_percentage uses a 0-100 scale.

Example representing an APY of 25.3%:

  • rate_per_second_to_apy will return 0.253.
  • rate_per_second_to_apy_percentage will return 25.3.

Usage

Function signature

lib_math.rate_per_second_to_apy(rate_per_second numeric) returns numeric

Parameters

NameTypeDescription
rate_per_secondnumericThe rate per second to be converted into APY

Results

NameTypeDescription
apynumericThe annual percentage yield (APY) calculated from the given rate-per-second value.

Examples

Simple hard-coded APY of 0.0482
select lib_math.rate_per_second_to_apy(1492886069);
Get supply/borrow rates for the hUSDT pool (0xce86ebc669bbf07a64a0a55bb105cc2b5b5d1961)
-- This example just uses hard-coded references to the hUSDT
-- contract (0xce86ebc669bbf07a64a0a55bb105cc2b5b5d1961)
with contract_metadata as (
select account, alias, (context->>'decimals')::int as decimals
from address_metadata
where account = '0xce86ebc669bbf07a64a0a55bb105cc2b5b5d1961'
),
supply_rate as (
select cm.contract, cm.val as supply_rate_per_timestamp
from lib.latest_custom_metric_by_contract(
array['0xce86ebc669bbf07a64a0a55bb105cc2b5b5d1961'],
'supplyRate'
) cm
),
borrow_rate as (
select cm.contract, cm.val as borrow_rate_per_timestamp
from lib.latest_custom_metric_by_contract(
array['0xce86ebc669bbf07a64a0a55bb105cc2b5b5d1961'],
'borrowRate'
) cm
)
select
cm.account,
cm.alias,
lib_math.rate_per_second_to_apy(sr.supply_rate_per_timestamp) as supply_apy,
lib_math.rate_per_second_to_apy_percentage(sr.supply_rate_per_timestamp) as supply_apy_percentage,
lib_math.rate_per_second_to_apy(br.borrow_rate_per_timestamp) as borrow_apy,
lib_math.rate_per_second_to_apy_percentage(br.borrow_rate_per_timestamp) as borrow_apy_percentage
from contract_metadata cm
join supply_rate sr on cm.account = sr.contract
join borrow_rate br on cm.account = br.contract;