Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update soroswap-router-sdk and add get pairs from zephyr #458

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.production.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED=false
NEXT_PUBLIC_SOROSWAP_BACKEND_URL=https://backend.soroswap.finance
NEXT_PUBLIC_TAG_ID=
NEXT_PUBLIC_TEST_TOKENS_ADMIN_SECRET_KEY=
NEXT_PUBLIC_TRUSTLINE_WALLET_PUBLIC_KEY=
NEXT_PUBLIC_TAG_ID=

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
Loading