From ab3080e7d59d8db58c3e2ef8d8f6da10714198ad Mon Sep 17 00:00:00 2001 From: sablevsky Date: Mon, 9 Dec 2024 12:55:51 +0100 Subject: [PATCH] Add fetchConversionRate from jup --- src/api/common/index.ts | 3 +++ src/api/common/jup/index.ts | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/api/common/jup/index.ts diff --git a/src/api/common/index.ts b/src/api/common/index.ts index f0c493267..9646843b0 100644 --- a/src/api/common/index.ts +++ b/src/api/common/index.ts @@ -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' diff --git a/src/api/common/jup/index.ts b/src/api/common/jup/index.ts new file mode 100644 index 000000000..cd0b8a8f5 --- /dev/null +++ b/src/api/common/jup/index.ts @@ -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 => { + 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') + } +}