Skip to content

Commit

Permalink
Get lrtsSOL apr from solayer api.
Browse files Browse the repository at this point in the history
Implement apy/apr convertions
  • Loading branch information
sablevsky committed Dec 13, 2024
1 parent c14bac2 commit cea9713
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/api/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export * from './user'

export * as solana from './solana'
export * from './solana'

export * as solayer from './solayer'
export * from './solayer'
16 changes: 16 additions & 0 deletions src/api/common/solayer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios from 'axios'
import { BN } from 'fbonds-core'

import { ZERO_BN, convertApyToApr } from '@banx/utils'

const SOLAYER_API_URL = 'https://app.solayer.org/api'

export const fetchSolayerApr = async () => {
const { data } = await axios.get<{ apy: number }>(`${SOLAYER_API_URL}/info`)

const apr = convertApyToApr(data?.apy || 0, 12)

if (!apr) return ZERO_BN

return new BN(apr * 100)
}
15 changes: 8 additions & 7 deletions src/pages/tokenLending/LeverageLanding/LeverageLanding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Multiply, SOL, StarSecondary, USDC } from '@banx/icons'
import { PATHS } from '@banx/router'
import { buildUrlWithModeAndToken } from '@banx/store'
import { AssetMode, useTokenType } from '@banx/store/common'
import { isBanxSolTokenType, isUsdcTokenType } from '@banx/utils'
import { convertAprToApy, isBanxSolTokenType, isUsdcTokenType } from '@banx/utils'

import { MULTIPLY_PAIRS, MultiplyPair } from '../LeveragePage'
import { useCollateralYield } from '../LeveragePage/hooks'
Expand Down Expand Up @@ -88,7 +88,7 @@ export const LeverageLanding = () => {
type TokenItem = {
logoUrl?: string
ticker: string
maxApr?: number
maxApy?: number
maxMultiply?: number
disabled?: boolean
extraRewardsDescription?: string
Expand All @@ -106,13 +106,14 @@ const TokenItemRealLink: FC<TokenItemRealLinkProps> = ({ pair, onClick }) => {
const { collateralLogoUrl, collateralTicker } = pair

const maxApr = collateralYield.toNumber() / 100
const maxApy = convertAprToApy(maxApr, 12)

return (
<TokenItemLink
logoUrl={collateralLogoUrl}
ticker={collateralTicker}
extraRewardsDescription="This market earns additional rewards"
maxApr={maxApr}
maxApy={maxApy}
maxMultiply={100}
onClick={onClick}
/>
Expand All @@ -122,7 +123,7 @@ const TokenItemRealLink: FC<TokenItemRealLinkProps> = ({ pair, onClick }) => {
const TokenItemLink: FC<TokenItem & { onClick: () => void }> = ({
ticker,
logoUrl,
maxApr,
maxApy,
maxMultiply,
extraRewardsDescription,
disabled,
Expand All @@ -137,17 +138,17 @@ const TokenItemLink: FC<TokenItem & { onClick: () => void }> = ({
{logoUrl && <img src={logoUrl} alt={ticker} />}
<p className={styles.tokenItemTicker}>{ticker}</p>

{maxApr && (
{maxApy && (
<div className={styles.tokenItemApyWrapper}>
<p className={styles.tokenItemApyTitle}>
{extraRewardsDescription && (
<Tooltip title={extraRewardsDescription}>
<StarSecondary />
</Tooltip>
)}
Max apr
Max apy
</p>
<p className={styles.tokenItemApyValue}>{maxApr}%</p>
<p className={styles.tokenItemApyValue}>{maxApy.toFixed(2)}%</p>
</div>
)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/tokenLending/LeveragePage/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BN, web3 } from 'fbonds-core'
import { BASE_POINTS } from 'fbonds-core/lib/fbond-protocol/constants'
import { LendingTokenType } from 'fbonds-core/lib/fbond-protocol/types'

import { fetchSolayerApr } from '@banx/api/common'
import { fetchJlpAprPercent } from '@banx/api/tokens'
import {
CreateLeverageTxnData,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const MULTIPLY_PAIRS: MultiplyPair[] = [
collateralLogoUrl:
'https://i.degencdn.com/ipfs/bafkreigoloautosntlteqccwusdjpxvtm3nm3kqvkavwxwickrk5irfety',
collateralMint: lrtsSOL.LRTS_MINT,
getCollateralYield: () => Promise.resolve(new BN(0.082 * BASE_POINTS)),
getCollateralYield: fetchSolayerApr,
marketTokenType: LendingTokenType.BanxSol,
marketPublicKey: new web3.PublicKey('7EuPa26AjGdnQ7JcqM3kFhwFR4U2NQTU9guHcmaDF2G'),
loanValueLimit: new BN(120 * getTokenDecimals(LendingTokenType.BanxSol)),
Expand Down
25 changes: 22 additions & 3 deletions src/utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,28 @@ export const stringToHex = (str: string, decimals?: number): string => {
return stringToBN(str, decimals).toString(16).toUpperCase()
}

export const convertAprToApy = (apr: number, compoundingPeriods = 1) => {
const apy = (1 + apr / compoundingPeriods) ** compoundingPeriods - 1
return apy * 100
/**
*
* @param apr The APR as a percentage (e.g., 5 for 5% APR).
* @param compoundingPeriods The number of compounding periods per year (e.g., 12 for monthly compounding).
* @returns The APY as a percentage (e.g., 5.12 for 5.12% APY).
*/
export const convertAprToApy = (apr: number, compoundingPeriods = 12) => {
const aprDecimal = apr / 100
const apyDecimal = Math.pow(1 + aprDecimal / compoundingPeriods, compoundingPeriods) - 1
return apyDecimal * 100
}

/**
*
* @param apy The APY as a percentage (e.g., 5 for 5% APR).
* @param compoundingPeriods The number of compounding periods per year (e.g., 12 for monthly compounding).
* @returns The APR as a percentage (e.g., 5.12 for 5.12% APR).
*/
export const convertApyToApr = (apy: number, compoundingPeriods = 1) => {
const apyDecimal = apy / 100
const aprDecimal = compoundingPeriods * (Math.pow(1 + apyDecimal, 1 / compoundingPeriods) - 1)
return aprDecimal * 100
}

export const isExponentialNotation = (n: number) => {
Expand Down

0 comments on commit cea9713

Please sign in to comment.