Skip to content

Commit

Permalink
call info for pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
abstract829 committed Jun 14, 2024
1 parent 8623195 commit c211056
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
25 changes: 3 additions & 22 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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],
Expand Down
25 changes: 14 additions & 11 deletions src/functions/getLpTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,38 +36,40 @@ 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,
token_1,
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,
};
Expand Down
27 changes: 11 additions & 16 deletions src/services/pairs.ts
Original file line number Diff line number Diff line change
@@ -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 } = {
Expand All @@ -18,14 +17,10 @@ const passphraseToBackendNetworkName: { [x: string]: string } = {
export const fetchAllSoroswapPairs = async (networkPassphrase: string) => {
const networkName = passphraseToBackendNetworkName[networkPassphrase];

const { data } = await axios.post<SubscribedPair[]>(
`${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<MercuryPair[]>(`https://info.soroswap.finance/api/pairs/plain`, {
params: {
network: networkName,
},
);
});
return data;
};

0 comments on commit c211056

Please sign in to comment.