diff --git a/src/functions/generateRoute.ts b/src/functions/generateRoute.ts index 573a794b..bc8eecf5 100644 --- a/src/functions/generateRoute.ts +++ b/src/functions/generateRoute.ts @@ -3,8 +3,7 @@ import { useFactory } from 'hooks'; import { useContext, useMemo } from 'react'; import { CurrencyAmount, Networks, Protocols, Router, Token, TradeType } from 'soroswap-router-sdk'; import { AppContext } from 'contexts'; -import axios from 'axios'; -import { TokenType } from 'interfaces'; +import { fetchAllSoroswapPairs } from 'services/pairs'; export interface GenerateRouteProps { amountTokenAddress: string; @@ -30,27 +29,9 @@ export const useRouterSDK = () => { const router = useMemo(() => { return new Router({ getPairsFn: async () => { - let queryNetwork = queryNetworkDict[network]; + const data = await fetchAllSoroswapPairs(network); - const { data } = await axios.get< - { - tokenA: TokenType; - tokenB: TokenType; - reserveA: string; - reserveB: string; - }[] - >('https://info.soroswap.finance/api/pairs', { - params: { network: queryNetwork }, - }); - - return data.map((pair) => { - return { - tokenA: pair.tokenA.contract, - tokenB: pair.tokenB.contract, - reserveA: pair.reserveA, - reserveB: pair.reserveB, - }; - }); + return data; }, pairsCacheInSeconds: 60, protocols: [Protocols.SOROSWAP], diff --git a/src/functions/getLpTokens.tsx b/src/functions/getLpTokens.tsx index ae686fc7..3e2ebb51 100644 --- a/src/functions/getLpTokens.tsx +++ b/src/functions/getLpTokens.tsx @@ -8,6 +8,7 @@ import { SorobanContextType } from '@soroban-react/core'; import { tokenBalance } from 'hooks'; import { TokenMapType, TokenType } from 'interfaces'; import BigNumber from 'bignumber.js'; +import { getTotalShares } from './LiquidityPools'; export type LpTokensObj = { token_0: TokenType | undefined; @@ -35,28 +36,30 @@ const getLpResultsFromBackendPairs = async ( for (const element of pairsBackend) { const pairLpTokens = await tokenBalance( - element.contractId, + element.address, sorobanContext.address, sorobanContext, ); if (pairLpTokens != 0) { - const token_0 = await findToken(element.token0, tokensAsMap, sorobanContext); - const token_1 = await findToken(element.token1, tokensAsMap, sorobanContext); + const token_0 = await findToken(element.tokenA, tokensAsMap, sorobanContext); + const token_1 = await findToken(element.tokenB, tokensAsMap, sorobanContext); + + const totalShares = await getTotalShares(element.address, sorobanContext); const lpPercentage = BigNumber(pairLpTokens as BigNumber) - .dividedBy(Number(element.totalShares)) + .dividedBy(Number(totalShares)) .multipliedBy(100) .decimalPlaces(7); if (!token_0 || !token_1) return; const myReserve0 = BigNumber(pairLpTokens as BigNumber) - ?.multipliedBy(BigNumber(element.reserve0)) - .dividedBy(Number(element.totalShares)); + ?.multipliedBy(BigNumber(element.reserveA)) + .dividedBy(Number(totalShares)); const myReserve1 = BigNumber(pairLpTokens as BigNumber) - ?.multipliedBy(BigNumber(element.reserve1)) - .dividedBy(Number(element.totalShares)); + ?.multipliedBy(BigNumber(element.reserveB)) + .dividedBy(Number(totalShares)); const toReturn = { token_0, @@ -64,9 +67,9 @@ const getLpResultsFromBackendPairs = async ( balance: pairLpTokens, lpPercentage: lpPercentage.toString(), status: 'Active', - reserve0: BigNumber(element.reserve0), - reserve1: BigNumber(element.reserve1), - totalShares: element.totalShares, + reserve0: BigNumber(element.reserveA), + reserve1: BigNumber(element.reserveB), + totalShares: totalShares, myReserve0, myReserve1, }; diff --git a/src/services/pairs.ts b/src/services/pairs.ts index b2413535..c24f58f2 100644 --- a/src/services/pairs.ts +++ b/src/services/pairs.ts @@ -1,13 +1,12 @@ import axios from 'axios'; import { Networks } from '@stellar/stellar-sdk'; -interface SubscribedPair { - contractId: string; - token0: string; - token1: string; - reserve0: string; - reserve1: string; - totalShares: string; - protocol: string; + +interface MercuryPair { + tokenA: string; + tokenB: string; + address: string; + reserveA: string; + reserveB: string; } const passphraseToBackendNetworkName: { [x: string]: string } = { @@ -18,14 +17,10 @@ const passphraseToBackendNetworkName: { [x: string]: string } = { export const fetchAllSoroswapPairs = async (networkPassphrase: string) => { const networkName = passphraseToBackendNetworkName[networkPassphrase]; - const { data } = await axios.post( - `${process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_URL}/pairs/all?network=${networkName}&protocols=soroswap`, - undefined, - { - headers: { - apiKey: process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY, - }, + const { data } = await axios.get(`https://info.soroswap.finance/api/pairs/plain`, { + params: { + network: networkName, }, - ); + }); return data; };