Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Created July 11, 2024 08:38
Show Gist options
  • Save 0xdeepmehta/2b63f7af42327d947c3ff01163b1bbc5 to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/2b63f7af42327d947c3ff01163b1bbc5 to your computer and use it in GitHub Desktop.
APY calculation from APR on the Basis of solana block time
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