Created
July 11, 2024 08:38
-
-
Save 0xdeepmehta/2b63f7af42327d947c3ff01163b1bbc5 to your computer and use it in GitHub Desktop.
APY calculation from APR on the Basis of solana block time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateAPYFromAPR(annualPercentageRate) { | |
// Slot calculations | |
const SLOTS_PER_SECOND = 2; | |
const SLOTS_PER_MINUTE = SLOTS_PER_SECOND * 60; | |
const SLOTS_PER_HOUR = SLOTS_PER_MINUTE * 60; | |
const SLOTS_PER_DAY = SLOTS_PER_HOUR * 24; | |
const SLOTS_PER_YEAR = SLOTS_PER_DAY * 365; | |
// Step 1: Calculate the rate per slot | |
const ratePerSlot = new Decimal(1).plus( | |
new Decimal(annualPercentageRate).dividedBy(SLOTS_PER_YEAR) | |
); | |
// Step 2: Compound the rate over a year | |
const compoundedAnnualRate = ratePerSlot.pow(SLOTS_PER_YEAR); | |
// Step 3: Convert to APY by subtracting 1 (to get the percentage increase) | |
const apy = compoundedAnnualRate.minus(1); | |
return apy.toNumber(); | |
} | |
// Example usage: | |
const apr = 0.05; // 5% APR | |
const apy = calculateAPYFromAPR(apr); | |
console.log(`APR: ${(apr * 100).toFixed(2)}%, APY: ${(apy * 100).toFixed(2)}%`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment