Created
August 26, 2025 17:16
-
-
Save wphan/0a829fc21393d6a47c443ecadeeb4449 to your computer and use it in GitHub Desktop.
drift earn deposit/borrow interest APR->APY example
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
| import { | |
| convertToNumber, | |
| calculateBorrowRate, | |
| calculateDepositRate, | |
| PERCENTAGE_PRECISION, | |
| } from '@drift-labs/sdk'; | |
| const { driftClient } = await initDriftClients(process.env.RPC_URL!); | |
| const getApyFromApr = ( | |
| aprPercent: number, | |
| compoundingFrequency: 'hourly' | 'daily' | |
| ): number => { | |
| const n = compoundingFrequency === 'hourly' ? 24 * 365 : 365; | |
| const aprDecimal = aprPercent / 100; | |
| return ((1 + aprDecimal / n) ** n - 1); | |
| }; | |
| const mkt = driftClient.getSpotMarketAccount(0)!; | |
| const depositRateApr = convertToNumber(calculateDepositRate(mkt), PERCENTAGE_PRECISION) * 100.0; | |
| const borrowRateApr = convertToNumber(calculateBorrowRate(mkt), PERCENTAGE_PRECISION) * 100.0; | |
| const depositRateApy = getApyFromApr(depositRateApr, 'daily') * 100.0; | |
| const borrowRateApy = getApyFromApr(borrowRateApr, 'daily') * 100.0; | |
| console.log(`Deposit rate: ${depositRateApr.toFixed(6)}% APR, ${depositRateApy.toFixed(6)}% APY`); | |
| console.log(`Borrow rate: ${borrowRateApr.toFixed(6)}% APR, ${borrowRateApy.toFixed(6)}% APY`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment