diff --git a/next-env.d.ts b/next-env.d.ts index 4f11a03d..a4a7b3f5 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information. diff --git a/pages/index.tsx b/pages/index.tsx index afb19080..4e33e51e 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -21,21 +21,25 @@ export default function Home() { }); useEffect(() => { - const newXlmToken = - xlmTokenList.find((tList) => tList.network === activeChain?.id)?.assets[0].contract ?? null; - setXlmToken(newXlmToken); + if (prefilledState.INPUT?.currencyId == null) { + const newXlmToken = + xlmTokenList.find((tList) => tList.network === activeChain?.id)?.assets[0].contract ?? null; + setXlmToken(newXlmToken); - const newPrefilledState = { - [Field.INPUT]: { currencyId: newXlmToken }, - [Field.OUTPUT]: { currencyId: null }, - }; - setPrefilledState(newPrefilledState); + const newPrefilledState = { + [Field.INPUT]: { currencyId: newXlmToken }, + [Field.OUTPUT]: { currencyId: null }, + }; + setPrefilledState(newPrefilledState); + } }, [activeChain, xlmToken]); return ( <> - {xlmToken && } + {xlmToken && ( + + )} ); } diff --git a/src/components/Swap/SwapComponent.tsx b/src/components/Swap/SwapComponent.tsx index 135b0a88..14be04d9 100644 --- a/src/components/Swap/SwapComponent.tsx +++ b/src/components/Swap/SwapComponent.tsx @@ -108,15 +108,17 @@ const INITIAL_SWAP_STATE = { export function SwapComponent({ prefilledState = {}, + setPrefilledState, disableTokenInputs = false, handleDoSwap, }: { prefilledState?: Partial; + setPrefilledState?: (value: Partial) => void; disableTokenInputs?: boolean; handleDoSwap?: (setSwapState: (value: SetStateAction) => void) => void; }) { const sorobanContext = useSorobanReact(); - const { refetch } = useGetMyBalances() + const { refetch } = useGetMyBalances(); const { SnackbarContext } = useContext(AppContext); const [showPriceImpactModal, setShowPriceImpactModal] = useState(false); const [txError, setTxError] = useState(false); @@ -153,7 +155,10 @@ export function SwapComponent({ } = useDerivedSwapInfo(state); useEffect(() => { - if (typeof currencyBalances[Field.OUTPUT] != 'string' && currencyBalances[Field.OUTPUT].balance === undefined) { + if ( + typeof currencyBalances[Field.OUTPUT] != 'string' && + currencyBalances[Field.OUTPUT].balance === undefined + ) { setNeedTrustline(true); } else { setNeedTrustline(false); @@ -202,6 +207,12 @@ export function SwapComponent({ const handleInputSelect = useCallback( (inputCurrency: TokenType) => { onCurrencySelection(Field.INPUT, inputCurrency); + setPrefilledState + ? setPrefilledState({ + [Field.INPUT]: { currencyId: inputCurrency.contract }, + [Field.OUTPUT]: { currencyId: prefilledState.OUTPUT?.currencyId }, + }) + : null; }, [onCurrencySelection], ); @@ -209,6 +220,12 @@ export function SwapComponent({ const handleOutputSelect = useCallback( (outputCurrency: TokenType) => { onCurrencySelection(Field.OUTPUT, outputCurrency); + setPrefilledState + ? setPrefilledState({ + [Field.INPUT]: { currencyId: prefilledState.OUTPUT?.currencyId }, + [Field.OUTPUT]: { currencyId: outputCurrency.contract }, + }) + : null; }, [onCurrencySelection], ); @@ -309,9 +326,10 @@ export function SwapComponent({ ...currentState, showConfirm: false, })); - }).finally(() => { - refetch() - nativeBalance.mutate() + }) + .finally(() => { + refetch(); + nativeBalance.mutate(); }); }; diff --git a/src/configs/protocols.config.json b/src/configs/protocols.config.json index b1c48992..140198c2 100644 --- a/src/configs/protocols.config.json +++ b/src/configs/protocols.config.json @@ -6,7 +6,7 @@ }, { "key": "soroswap", - "value": true + "value": false }, { "key": "phoenix", diff --git a/src/functions/generateRoute.ts b/src/functions/generateRoute.ts index a94e0d7f..938c0beb 100644 --- a/src/functions/generateRoute.ts +++ b/src/functions/generateRoute.ts @@ -64,28 +64,29 @@ export const useRouterSDK = () => { const getPairsFns = useMemo(() => { const routerProtocols = [] - if(shouldUseBackend) return undefined + if (shouldUseBackend) return undefined // here you should add your new supported aggregator protocols - for(let protocol of protocolsStatus){ - if(protocol.key === Protocol.SOROSWAP && protocol.value === true){ - routerProtocols.push({protocol: Protocol.SOROSWAP, fn: async () => fetchAllSoroswapPairs(network)}); + for (let protocol of protocolsStatus) { + if (protocol.key === Protocol.SOROSWAP && protocol.value === true) { + routerProtocols.push({ protocol: Protocol.SOROSWAP, fn: async () => fetchAllSoroswapPairs(network) }); } - if(protocol.key === Protocol.PHOENIX && protocol.value === true){ - routerProtocols.push({protocol: Protocol.PHOENIX, fn: async () => fetchAllPhoenixPairs(network)}); + if (protocol.key === Protocol.PHOENIX && protocol.value === true) { + routerProtocols.push({ protocol: Protocol.PHOENIX, fn: async () => fetchAllPhoenixPairs(network) }); } } + console.log('routerProtocols:', routerProtocols); return routerProtocols; }, [network, protocolsStatus]); const getProtocols = useMemo(() => { const newProtocols = []; - for(let protocol of protocolsStatus){ - if(protocol.key != PlatformType.STELLAR_CLASSIC && protocol.value === true){ + for (let protocol of protocolsStatus) { + if (protocol.key != PlatformType.STELLAR_CLASSIC && protocol.value === true) { newProtocols.push(protocol.key); } } return newProtocols as Protocol[]; - },[protocolsStatus]); + }, [protocolsStatus]); const router = useMemo(() => { return new Router({ @@ -124,9 +125,9 @@ export const useRouterSDK = () => { ); const quoteCurrency = fromAddressToToken(quoteAsset.contract); - const isHorizonEnabled = currentProtocolsStatus.find((p) => p.key === PlatformType.STELLAR_CLASSIC)?.value; + const isHorizonEnabled = currentProtocolsStatus.find((p) => p.key === PlatformType.STELLAR_CLASSIC)?.value; - const isSoroswapEnabled = currentProtocolsStatus.find((p) => p.key === Protocol.SOROSWAP)?.value; + const isSoroswapEnabled = currentProtocolsStatus.find((p) => p.key === Protocol.SOROSWAP)?.value; const horizonProps = { assetFrom: amountAsset.currency, @@ -136,10 +137,10 @@ export const useRouterSDK = () => { }; let horizonPath: BuildTradeRoute | undefined; - if(isHorizonEnabled){ + if (isHorizonEnabled) { horizonPath = (await getHorizonBestPath(horizonProps, sorobanContext)) as BuildTradeRoute; } - + let sorobanPath: BuildTradeRoute | undefined; if (isAggregator) { sorobanPath = (await router @@ -149,10 +150,14 @@ export const useRouterSDK = () => { const result = { ...response, platform: PlatformType.AGGREGATOR, - }; + quoteCurrency: CurrencyAmount.fromRawAmount(quoteCurrency, '0') + } as BuildTradeRoute; return result; - })) as BuildTradeRoute; - } else if(isSoroswapEnabled){ + }).catch((e) => { + console.error('error while generating soroban path:', e); + return undefined; + })) as BuildTradeRoute | undefined; + } else if (isSoroswapEnabled) { sorobanPath = (await router .route(currencyAmount, quoteCurrency, tradeType, factory, sorobanContext as any) .then((response) => { @@ -164,9 +169,7 @@ export const useRouterSDK = () => { return result; })) as BuildTradeRoute; } - const bestPath = getBestPath(horizonPath, sorobanPath, tradeType); - return bestPath; }; diff --git a/src/hooks/useSwapMainButton.ts b/src/hooks/useSwapMainButton.ts index 1378672a..a269ed25 100644 --- a/src/hooks/useSwapMainButton.ts +++ b/src/hooks/useSwapMainButton.ts @@ -7,6 +7,7 @@ import { Field } from 'state/swap/actions'; import { relevantTokensType } from './useBalances'; import useGetMyBalances from './useGetMyBalances'; import useGetNativeTokenBalance from './useGetNativeTokenBalance'; +import { useAggregator } from './useAggregator'; interface Props { currencies: any; @@ -34,6 +35,7 @@ const useSwapMainButton = ({ const { isConnectWalletModalOpen, setConnectWalletModalOpen } = ConnectWalletModal; const { data } = useGetNativeTokenBalance(); const { availableNativeBalance } = useGetMyBalances(); + const { isEnabled: aggregatorEnabled } = useAggregator(); const { address } = sorobanContext; const userBalances = useGetMyBalances(); @@ -66,9 +68,14 @@ const useSwapMainButton = ({ Number(inputA) > Number(balanceA) ? currencyA?.code : undefined; const invalidAmount = Number(inputA) < 0 || Number(inputB) < 0; - - const insufficientLiquidity = !noAmountTyped && !trade; - + let insufficientLiquidity = !noAmountTyped && !trade; + if(aggregatorEnabled){ + const distribution = trade?.distribution; + if (distribution?.every((d) => d.path.length === 0)) { + insufficientLiquidity = true; + } + } + return { currencyA, currencyB,