From 9d5dd63e406b266dcec4d08ca2847ed071ae97ae Mon Sep 17 00:00:00 2001 From: Bryan Date: Thu, 2 Nov 2023 09:48:37 -0500 Subject: [PATCH] ensure sufficient tokens to swap (#536) * ensure sufficient tokens to swap * simplify logic * handle errors * throw in try catch --- src/features/swaps/SwapScreen.tsx | 24 ++---------------------- src/storage/JupiterProvider.tsx | 18 +++++++++++------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/features/swaps/SwapScreen.tsx b/src/features/swaps/SwapScreen.tsx index 9a61b536a..f2de90f54 100644 --- a/src/features/swaps/SwapScreen.tsx +++ b/src/features/swaps/SwapScreen.tsx @@ -114,17 +114,11 @@ const SwapScreen = () => { const [recipient, setRecipient] = useState('') const [isRecipientOpen, setRecipientOpen] = useState(false) const { visibleTokens } = useVisibleTokens() + const { loading, error: jupiterError, routeMap, getRoute } = useJupiter() const { price, loading: loadingPrice } = useTreasuryPrice( inputMint, inputAmount, ) - const { - loading, - error: jupiterError, - routeMap, - routes, - getRoute, - } = useJupiter() const inputMintDecimals = useMint(inputMint)?.info?.decimals const outputMintDecimals = useMint(outputMint)?.info?.decimals @@ -180,23 +174,9 @@ const SwapScreen = () => { const insufficientTokensToSwap = useMemo(() => { if (inputAmount > 0 && typeof inputMintDecimals !== 'undefined') { - if (!isDevnet && !outputMint.equals(DC_MINT)) { - return ( - routes?.outAmount && - new BN(routes?.outAmount || 0).lt(new BN(inputAmount)) - ) - } - return inputMintBalance?.lt(toBN(inputAmount || 0, inputMintDecimals)) } - }, [ - inputAmount, - inputMintDecimals, - inputMintBalance, - outputMint, - routes, - isDevnet, - ]) + }, [inputAmount, inputMintDecimals, inputMintBalance]) const showError = useMemo(() => { if (hasRecipientError) return t('generic.notValidSolanaAddress') diff --git a/src/storage/JupiterProvider.tsx b/src/storage/JupiterProvider.tsx index 56662fe0c..ddae99d84 100644 --- a/src/storage/JupiterProvider.tsx +++ b/src/storage/JupiterProvider.tsx @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { useCurrentWallet } from '@hooks/useCurrentWallet' import { Configuration, @@ -17,6 +18,7 @@ import React, { } from 'react' import { useTranslation } from 'react-i18next' import Config from 'react-native-config' +import * as Logger from '../utils/logger' type RouteMap = Map interface IJupiterContextState { @@ -80,8 +82,9 @@ export const JupiterProvider: React.FC = ({ children }) => { platformFeeBps: Number(Config.JUPITER_FEE_BPS) || 0, }) setRoutes(foundRoutes) - } catch (err) { - setError(err) + } catch (err: any) { + Logger.error(err) + setError(err.toString()) } finally { setLoading(false) } @@ -93,10 +96,10 @@ export const JupiterProvider: React.FC = ({ children }) => { const getSwapTx = useCallback( async (opts?: Pick) => { - if (!routes) throw new Error(t('errors.swap.routes')) - if (!wallet) throw new Error(t('errors.account')) - try { + if (!routes) throw new Error(t('errors.swap.routes')) + if (!wallet) throw new Error(t('errors.account')) + const { swapTransaction } = await api.swapPost({ swapRequest: { quoteResponse: routes, @@ -108,8 +111,9 @@ export const JupiterProvider: React.FC = ({ children }) => { const swapTransactionBuf = Buffer.from(swapTransaction, 'base64') return VersionedTransaction.deserialize(swapTransactionBuf) - } catch (err) { - setError(err) + } catch (err: any) { + Logger.error(err) + setError(err.toString()) } }, [t, api, routes, wallet],