Skip to content

Commit

Permalink
Merge pull request #576 from soroswap/fix/workingDirectPath
Browse files Browse the repository at this point in the history
direct path working
  • Loading branch information
chopan123 authored Nov 17, 2024
2 parents 2d76562 + 43f7de0 commit 5e554f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
19 changes: 9 additions & 10 deletions src/components/Swap/SwapComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export function SwapComponent({
currencies,
inputError: swapInputError,
} = useDerivedSwapInfo(state);

useEffect(() => {
if (
typeof currencyBalances[Field.OUTPUT] != 'string' &&
Expand Down Expand Up @@ -189,8 +188,8 @@ export function SwapComponent({

const userHasSpecifiedInputOutput = Boolean(
currencies[Field.INPUT] &&
currencies[Field.OUTPUT] &&
Number(parsedAmounts[independentField]?.value) > 0,
currencies[Field.OUTPUT] &&
Number(parsedAmounts[independentField]?.value) > 0,
);

const fiatValueInput = { data: 0, isLoading: true }; //useUSDPrice(parsedAmounts[Field.INPUT]) //TODO: create USDPrice function when available method to get this, for now it will be shown as loading
Expand All @@ -209,9 +208,9 @@ export function SwapComponent({
onCurrencySelection(Field.INPUT, inputCurrency);
setPrefilledState
? setPrefilledState({
[Field.INPUT]: { currencyId: inputCurrency.contract },
[Field.OUTPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
})
[Field.INPUT]: { currencyId: inputCurrency.contract },
[Field.OUTPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
})
: null;
},
[onCurrencySelection],
Expand All @@ -222,9 +221,9 @@ export function SwapComponent({
onCurrencySelection(Field.OUTPUT, outputCurrency);
setPrefilledState
? setPrefilledState({
[Field.INPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
[Field.OUTPUT]: { currencyId: outputCurrency.contract },
})
[Field.INPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
[Field.OUTPUT]: { currencyId: outputCurrency.contract },
})
: null;
},
[onCurrencySelection],
Expand Down Expand Up @@ -506,7 +505,7 @@ export function SwapComponent({
}
showMaxButton={false}
hideBalance={false}
onMax={() => {}}
onMax={() => { }}
fiatValue={showFiatValueOutput ? fiatValueOutput : undefined}
//priceImpact={stablecoinPriceImpact}
currency={currencies[Field.OUTPUT] ?? null}
Expand Down
53 changes: 52 additions & 1 deletion src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import { getBestPath, getHorizonBestPath } from 'helpers/horizon/getHorizonPath'
import { PlatformType } from 'state/routing/types';
import { CurrencyAmount as AmountAsset } from 'interfaces';
import { DexDistribution } from 'helpers/aggregator';
import { fetchFactory } from 'services/factory';
import { contractInvoke } from '@soroban-react/contracts';
import { reservesBNWithTokens } from 'hooks/useReserves';
import { getPairAddress } from './getPairAddress';
import BigNumber from 'bignumber.js';
import { getExpectedAmount } from './getExpectedAmount';

export interface BuildTradeRoute {
amountCurrency: AmountAsset | CurrencyAmount<Currency>;
Expand Down Expand Up @@ -51,6 +57,7 @@ const queryNetworkDict: { [x: string]: 'MAINNET' | 'TESTNET' } = {
};

const shouldUseBackend = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED === 'true';
const shouldUseDirectPath = process.env.NEXT_PUBLIC_DIRECT_PATH_ENABLED === 'true';

export const useRouterSDK = () => {
const sorobanContext = useSorobanReact();
Expand All @@ -74,7 +81,6 @@ export const useRouterSDK = () => {
routerProtocols.push({ protocol: Protocol.PHOENIX, fn: async () => fetchAllPhoenixPairs(network) });
}
}
console.log('routerProtocols:', routerProtocols);
return routerProtocols;
}, [network, protocolsStatus]);

Expand Down Expand Up @@ -118,6 +124,51 @@ export const useRouterSDK = () => {
tradeType,
currentProtocolsStatus,
}: GenerateRouteProps) => {
if (shouldUseDirectPath) {
try {
// get pair address from factory
const pairAddress = await getPairAddress(amountAsset.currency.contract, quoteAsset.contract, sorobanContext);

// Get reserves from pair
const reserves = await reservesBNWithTokens(pairAddress, sorobanContext);
if (!reserves?.reserve0 || !reserves?.reserve1) {
throw new Error('Reserves not found');
}

// Get amountOut or amountIn from reserves and tradeType
let outputAmount = await getExpectedAmount(
amountAsset.currency,
quoteAsset,
new BigNumber(amount),
sorobanContext,
tradeType
);

// Convert from lumens to stroops (multiply by 10^7)
outputAmount = outputAmount.integerValue();

const quoteCurrencyAmount = CurrencyAmount.fromRawAmount(fromAddressToToken(quoteAsset.contract), outputAmount.toString());

return {
amountCurrency: amountAsset,
quoteCurrency: CurrencyAmount.fromRawAmount(fromAddressToToken(quoteAsset.contract), outputAmount.toString()),
tradeType,
trade: {
amountIn: tradeType === TradeType.EXACT_INPUT ? amount : outputAmount.toString(),
amountInMax: tradeType === TradeType.EXACT_OUTPUT ? outputAmount.toString() : undefined,
amountOut: tradeType === TradeType.EXACT_INPUT ? outputAmount.toString() : amount,
amountOutMin: tradeType === TradeType.EXACT_INPUT ? outputAmount.toString() : undefined,
path: [amountAsset.currency.contract, quoteAsset.contract],
},
priceImpact: new Percent('0'),
platform: PlatformType.ROUTER,
} as BuildTradeRoute;
} catch (error) {
console.error('Error generating direct path:', error);
throw error;
}
}

if (!factory) throw new Error('Factory address not found');
const currencyAmount = fromAddressAndAmountToCurrencyAmount(
amountAsset.currency.contract,
Expand Down
19 changes: 5 additions & 14 deletions src/hooks/useBestTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useBestTrade(
resetRouterSdkCache: () => void;
} {
const { generateRoute, resetRouterSdkCache, maxHops } = useRouterSDK();
const {protocolsStatus} = useContext(AppContext).Settings;
const { protocolsStatus } = useContext(AppContext).Settings;
/**
* Custom hook that fetches the best trade based on the specified amount and trade type.
*
Expand Down Expand Up @@ -82,7 +82,6 @@ export function useBestTrade(

useEffect(() => {
if (!data || !currencyIn || !currencyOut) return;

if (tradeType === TradeType.EXACT_INPUT) {
const result = data.trade as {
amountIn: string;
Expand Down Expand Up @@ -126,6 +125,7 @@ export function useBestTrade(

// Create the trade object
const trade: InterfaceTrade = useMemo(() => {

const baseTrade = {
inputAmount,
outputAmount,
Expand All @@ -146,7 +146,7 @@ export function useBestTrade(
}

return baseTrade;
}, [expectedAmount, inputAmount, outputAmount, tradeType, data, protocolsStatus]);
}, [expectedAmount, inputAmount, outputAmount, tradeType, data]);

/*
If the pairAddress or the trades chenges, we upgrade the tradeResult
Expand All @@ -158,7 +158,7 @@ export function useBestTrade(

const myTradeResult = { state: state, trade: trade };
return myTradeResult;
}, [data, trade, protocolsStatus]); //should get the pair address and quotes
}, [data, trade]); //should get the pair address and quotes

const skipFetch: boolean = false;

Expand Down Expand Up @@ -191,16 +191,7 @@ export function useBestTrade(
resetRouterSdkCache,
};
}
}, [
skipFetch,
amountSpecified,
otherCurrency,
tradeResult,
trade,
isLoading,
resetRouterSdkCache,
protocolsStatus
]);
}, [skipFetch, amountSpecified, otherCurrency, tradeResult, trade, isLoading, resetRouterSdkCache]);

return bestTrade;
}

0 comments on commit 5e554f2

Please sign in to comment.