Skip to content

Commit

Permalink
ensure sufficient tokens to swap (#536)
Browse files Browse the repository at this point in the history
* ensure sufficient tokens to swap

* simplify logic

* handle errors

* throw in try catch
  • Loading branch information
bryzettler authored Nov 2, 2023
1 parent 83eb328 commit 9d5dd63
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
24 changes: 2 additions & 22 deletions src/features/swaps/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
18 changes: 11 additions & 7 deletions src/storage/JupiterProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useCurrentWallet } from '@hooks/useCurrentWallet'
import {
Configuration,
Expand All @@ -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<string, string[]>
interface IJupiterContextState {
Expand Down Expand Up @@ -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)
}
Expand All @@ -93,10 +96,10 @@ export const JupiterProvider: React.FC = ({ children }) => {

const getSwapTx = useCallback(
async (opts?: Pick<SwapPostRequest, 'swapRequest'>) => {
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,
Expand All @@ -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],
Expand Down

0 comments on commit 9d5dd63

Please sign in to comment.