Skip to content

Commit

Permalink
use info endpoint for getPairsFn in router sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
abstract829 committed Jun 11, 2024
1 parent 487a0e6 commit 71f574e
Show file tree
Hide file tree
Showing 3 changed files with 617 additions and 242 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"react-virtualized-auto-sizer": "^1.0.20",
"react-window": "^1.8.9",
"redux": "^4.2.1",
"soroswap-router-sdk": "^1.2.4",
"soroswap-router-sdk": "1.2.6",
"swr": "^2.2.0",
"typescript": "5.3.3",
"use-resize-observer": "^9.1.0"
Expand Down
44 changes: 30 additions & 14 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { useFactory } from 'hooks';
import { useContext, useMemo } from 'react';
import { CurrencyAmount, Networks, Protocols, Router, Token, TradeType } from 'soroswap-router-sdk';
import { AppContext } from 'contexts';

const backendUrl = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_URL;
const backendApiKey = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY;
const shouldUseBackend = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED === 'true';
import axios from 'axios';
import { TokenType } from 'interfaces';

export interface GenerateRouteProps {
amountTokenAddress: string;
Expand All @@ -15,6 +13,11 @@ export interface GenerateRouteProps {
tradeType: TradeType;
}

const queryNetworkDict: { [x: string]: 'MAINNET' | 'TESTNET' } = {
[Networks.PUBLIC]: 'MAINNET',
[Networks.TESTNET]: 'TESTNET',
};

export const useRouterSDK = () => {
const sorobanContext = useSorobanReact();
const { factory } = useFactory(sorobanContext);
Expand All @@ -25,19 +28,33 @@ export const useRouterSDK = () => {
const network = sorobanContext.activeChain?.networkPassphrase as Networks;

const router = useMemo(() => {
if (!backendUrl || !backendApiKey) {
throw new Error(
'NEXT_PUBLIC_SOROSWAP_BACKEND_URL and NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY must be set in the environment variables.',
);
}

return new Router({
backendUrl,
backendApiKey,
getPairsFn: async () => {
let queryNetwork = queryNetworkDict[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,
};
});
},
pairsCacheInSeconds: 60,
protocols: [Protocols.SOROSWAP],
network,
shouldUseBackend,
maxHops,
});
}, [network, maxHops]);
Expand All @@ -62,7 +79,6 @@ export const useRouterSDK = () => {
tradeType,
}: GenerateRouteProps) => {
if (!factory) throw new Error('Factory address not found');

const currencyAmount = fromAddressAndAmountToCurrencyAmount(amountTokenAddress, amount);
const quoteCurrency = fromAddressToToken(quoteTokenAddress);

Expand Down
Loading

0 comments on commit 71f574e

Please sign in to comment.