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

Insufficient liquidity in Aggregator Warning #563

Merged
merged 7 commits into from
Nov 17, 2024
Merged
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
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// 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.
22 changes: 13 additions & 9 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<SEO title="Soroswap" data-testid="SEO" />
{xlmToken && <SwapComponent prefilledState={prefilledState} />}
{xlmToken && (
<SwapComponent prefilledState={prefilledState} setPrefilledState={setPrefilledState} />
)}
</>
);
}
28 changes: 23 additions & 5 deletions src/components/Swap/SwapComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ const INITIAL_SWAP_STATE = {

export function SwapComponent({
prefilledState = {},
setPrefilledState,
disableTokenInputs = false,
handleDoSwap,
}: {
prefilledState?: Partial<SwapState>;
setPrefilledState?: (value: Partial<SwapState>) => void;
disableTokenInputs?: boolean;
handleDoSwap?: (setSwapState: (value: SetStateAction<SwapStateProps>) => void) => void;
}) {
const sorobanContext = useSorobanReact();
const { refetch } = useGetMyBalances()
const { refetch } = useGetMyBalances();
const { SnackbarContext } = useContext(AppContext);
const [showPriceImpactModal, setShowPriceImpactModal] = useState<boolean>(false);
const [txError, setTxError] = useState<boolean>(false);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -202,13 +207,25 @@ 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],
);

const handleOutputSelect = useCallback(
(outputCurrency: TokenType) => {
onCurrencySelection(Field.OUTPUT, outputCurrency);
setPrefilledState
? setPrefilledState({
[Field.INPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
[Field.OUTPUT]: { currencyId: outputCurrency.contract },
})
: null;
},
[onCurrencySelection],
);
Expand Down Expand Up @@ -309,9 +326,10 @@ export function SwapComponent({
...currentState,
showConfirm: false,
}));
}).finally(() => {
refetch()
nativeBalance.mutate()
})
.finally(() => {
refetch();
nativeBalance.mutate();
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/configs/protocols.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"key": "soroswap",
"value": true
"value": false
},
{
"key": "phoenix",
Expand Down
39 changes: 21 additions & 18 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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) => {
Expand All @@ -164,9 +169,7 @@ export const useRouterSDK = () => {
return result;
})) as BuildTradeRoute;
}

const bestPath = getBestPath(horizonPath, sorobanPath, tradeType);

return bestPath;
};

Expand Down
13 changes: 10 additions & 3 deletions src/hooks/useSwapMainButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
Loading