Skip to content

Commit

Permalink
Add fetchConversionRate from jup
Browse files Browse the repository at this point in the history
  • Loading branch information
sablevsky committed Dec 9, 2024
1 parent 1004b88 commit ab3080e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export * from './staking'
export * as helius from './helius'
export * from './helius'

export * as jup from './jup'
export * from './jup'

export * as user from './user'
export * from './user'

Expand Down
41 changes: 41 additions & 0 deletions src/api/common/jup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createJupiterApiClient } from '@jup-ag/api'
import { LendingTokenType } from 'fbonds-core/lib/fbond-protocol/types'

import { USDC_ADDRESS, WSOL_ADDRESS } from '@banx/constants'
import { getTokenDecimals } from '@banx/utils'

type FetchConversionRate = {
lendingTokenType: LendingTokenType
tokenMint: string
}
export const fetchConversionRate = async ({
lendingTokenType,
tokenMint,
}: FetchConversionRate): Promise<number> => {
try {
const tokenDecimals = getTokenDecimals(lendingTokenType)
const lendingTokenMint =
lendingTokenType === LendingTokenType.Usdc ? USDC_ADDRESS : WSOL_ADDRESS

const jupiterQuoteApi = createJupiterApiClient()
const quote = await jupiterQuoteApi.quoteGet({
inputMint: lendingTokenMint,
outputMint: tokenMint,
amount: getTokenDecimals(lendingTokenType),
//TODO Fix slippage problem
slippageBps: 300,
computeAutoSlippage: true,
swapMode: 'ExactIn',
onlyDirectRoutes: true,
asLegacyTransaction: false,
maxAccounts: 64,
minimizeSlippage: false,
// dexes: ['Whirlpool'],
})

return parseInt(quote.outAmount) / tokenDecimals
} catch (error) {
console.error(error)
throw new Error('Failed to fetch token price')
}
}

0 comments on commit ab3080e

Please sign in to comment.