Skip to content
Closed
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
25 changes: 19 additions & 6 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSorobanReact, WalletNetwork } from 'stellar-react';
import { AppContext, ProtocolsStatus } from 'contexts';
import { useFactory } from 'hooks';
import { useAggregator } from 'hooks/useAggregator';
import { useContext, useMemo } from 'react';
import { useCallback, useContext, useMemo } from 'react';
import { TokenType } from 'interfaces';
import { getBestPath, getHorizonBestPath } from 'helpers/horizon/getHorizonPath';
import {
Expand All @@ -22,6 +22,7 @@ import { getExpectedAmount } from './getExpectedAmount';
import { Networks } from '@stellar/stellar-sdk';
import { getSwapRoute, getSwapSplitRoute } from 'services/soroswapApi';
import { passphraseToBackendNetworkName } from 'services/pairs';
import { debounce } from 'lodash-es';
import { DEFAULT_SLIPPAGE_INPUT_VALUE } from 'components/Settings/MaxSlippageSettings';
import { useUserSlippageToleranceWithDefault } from 'state/user/hooks';

Expand Down Expand Up @@ -174,8 +175,10 @@ export const useSoroswapApi = () => {
let sorobanPath: BuildTradeReturn | BuildSplitTradeReturn | undefined;
if (isAggregator) {
const swapSplitRequest: SwapRouteSplitRequest = {
assetIn: tradeType === TradeType.EXACT_INPUT ? amountAsset.currency.contract : quoteAsset.contract,
assetOut: tradeType === TradeType.EXACT_INPUT ? quoteAsset.contract : amountAsset.currency.contract,
assetIn:
tradeType === TradeType.EXACT_INPUT ? amountAsset.currency.contract : quoteAsset.contract,
assetOut:
tradeType === TradeType.EXACT_INPUT ? quoteAsset.contract : amountAsset.currency.contract,
amount: amount,
tradeType: tradeType,
protocols: getProtocols,
Expand All @@ -195,8 +198,10 @@ export const useSoroswapApi = () => {
}
} else if (isSoroswapEnabled) {
const swapRequest: SwapRouteRequest = {
assetIn: tradeType === TradeType.EXACT_INPUT ? amountAsset.currency.contract : quoteAsset.contract,
assetOut: tradeType === TradeType.EXACT_INPUT ? quoteAsset.contract : amountAsset.currency.contract,
assetIn:
tradeType === TradeType.EXACT_INPUT ? amountAsset.currency.contract : quoteAsset.contract,
assetOut:
tradeType === TradeType.EXACT_INPUT ? quoteAsset.contract : amountAsset.currency.contract,
amount: amount,
tradeType: tradeType,
slippageTolerance: Math.floor(Number(allowedSlippage) * 100).toString(),
Expand All @@ -217,5 +222,13 @@ export const useSoroswapApi = () => {
return bestPath;
};

return { generateRoute, resetRouterSdkCache, maxHops };
const generateRouteDebounce = useCallback(debounce(generateRoute, 500), [
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider including 'generateRoute' in the dependency array of useCallback to ensure that changes to its implementation are captured.

Copilot uses AI. Check for mistakes.
factory,
getProtocols,
isAggregator,
network,
sorobanContext,
]);

return { generateRoute: generateRouteDebounce, resetRouterSdkCache, maxHops };
};
Loading