From 27b100a0906e53cfbdcde4d32ef61190bb96f35a Mon Sep 17 00:00:00 2001 From: Bryan Date: Thu, 12 Dec 2024 11:40:19 -0600 Subject: [PATCH] Take a refactor pass on SwapScreen (#845) --- src/features/swaps/SwapScreen.tsx | 190 +++++++++++++++--------------- 1 file changed, 97 insertions(+), 93 deletions(-) diff --git a/src/features/swaps/SwapScreen.tsx b/src/features/swaps/SwapScreen.tsx index dcbd85b7..7b3d4721 100644 --- a/src/features/swaps/SwapScreen.tsx +++ b/src/features/swaps/SwapScreen.tsx @@ -368,13 +368,23 @@ const SwapScreen = () => { } if (selectorMode === SelectorMode.youReceive) { + setRecipient('') + setRecipientOpen(false) + if (mint.equals(inputMint)) { setInputMint(outputMint) } setOutputMint(mint) } }, - [selectorMode, refresh, inputMint, outputMint], + [ + selectorMode, + inputMint, + outputMint, + refresh, + setRecipient, + setRecipientOpen, + ], ) const tokenData = useMemo(() => { @@ -423,49 +433,54 @@ const SwapScreen = () => { const getOutputAmount = useCallback( async ({ balance }: { balance: BN }) => { setRouteNotFound(false) - if (outputMintAcc && inputMintAcc) { - const { address: input, decimals: inputDecimals } = inputMintAcc - const { address: output, decimals: outputDecimals } = outputMintAcc - - if (!isDevnet && !output.equals(DC_MINT)) { - const route = await getRoute({ - amount: balance.toNumber(), - inputMint: input.toBase58(), - outputMint: output.toBase58(), - slippageBps, - }) - if (!route) { - setRouteNotFound(true) - } - setPriceImpact(Number(route?.priceImpactPct || '0') * 100) + if (!outputMintAcc || !inputMintAcc) return - return setOutputAmount( - toNumber(new BN(Number(route?.outAmount || 0)), outputDecimals), - ) + const { address: input, decimals: inputDecimals } = inputMintAcc + const { address: output, decimals: outputDecimals } = outputMintAcc + const handleDevnetPrice = () => { + if (price && !input.equals(HNT_MINT)) { + return setOutputAmount(price) } - if (output.equals(DC_MINT)) { - if (input.equals(HNT_MINT)) { - return setOutputAmount( - toNumber( - networkTokensToDc(toBN(balance, inputDecimals)) || new BN(0), - inputDecimals, - ), - ) - } + return setOutputAmount(0) + } + + const handleJupiterRoute = async () => { + const route = await getRoute({ + amount: balance.toNumber(), + inputMint: input.toBase58(), + outputMint: output.toBase58(), + slippageBps, + }) + + if (!route) { setRouteNotFound(true) + setPriceImpact(0) return setOutputAmount(0) } - if (isDevnet) { - if (price && !input.equals(HNT_MINT)) { - return setOutputAmount(price) - } + setPriceImpact(Number(route?.priceImpactPct || '0') * 100) + return setOutputAmount( + toNumber(new BN(Number(route?.outAmount || 0)), outputDecimals), + ) + } + const handleDCConversion = () => { + if (!input.equals(HNT_MINT)) { + setRouteNotFound(true) return setOutputAmount(0) } - return setOutputAmount(0) + return setOutputAmount( + toNumber( + networkTokensToDc(toBN(balance, inputDecimals)) || new BN(0), + inputDecimals, + ), + ) } + + if (isDevnet) return handleDevnetPrice() + if (output.equals(DC_MINT)) return handleDCConversion() + return handleJupiterRoute() }, [ getRoute, @@ -479,34 +494,18 @@ const SwapScreen = () => { ) useEffect(() => { - setRecipient('') - setRecipientOpen(false) - setOutputAmount(0) - }, [ - inputMint, - inputAmount, - outputMint, - setRecipient, - setRecipientOpen, - setOutputAmount, - ]) + if ( + !inputMintAcc?.address?.equals(inputMint) || + !outputMintAcc?.address?.equals(outputMint) || + typeof inputAmount === 'undefined' || + inputAmount <= 0 + ) { + return + } - useEffect(() => { - // if changing outputMint ensure we get new routes - ;(async () => { - if ( - !isDevnet && - inputMintAcc?.address === inputMint && - outputMintAcc?.address === outputMint && - typeof inputAmount !== 'undefined' && - inputAmount > 0 && - !outputMintAcc?.address.equals(DC_MINT) - ) { - await getOutputAmount({ - balance: toBN(inputAmount || 0, inputMintAcc.decimals), - }) - } - })() + getOutputAmount({ + balance: toBN(inputAmount, inputMintAcc.decimals), + }) }, [ getOutputAmount, inputMint, @@ -514,53 +513,58 @@ const SwapScreen = () => { inputMintAcc, outputMint, outputMintAcc, - isDevnet, ]) const getInputAmount = useCallback( async ({ balance }: { balance: BN }) => { - if (outputMintAcc && inputMintAcc) { - const { address: input, decimals: inputDecimals } = inputMintAcc - const { address: output, decimals: outputDecimals } = outputMintAcc - - if (!isDevnet && !output.equals(DC_MINT)) { - const route = await getRoute({ - amount: balance.toNumber(), - inputMint: output.toBase58(), - outputMint: input.toBase58(), - slippageBps, - }) + setRouteNotFound(false) + if (!outputMintAcc || !inputMintAcc) return - if (!route) { - setRouteNotFound(true) - return setInputAmount(0) - } + const { address: input, decimals: inputDecimals } = inputMintAcc + const { address: output, decimals: outputDecimals } = outputMintAcc - return setInputAmount( - toNumber(new BN(Number(route?.outAmount || 0)), inputDecimals), - ) + const handleDevnetPrice = () => { + if (price && !input.equals(HNT_MINT)) { + return setInputAmount(price) } - if (output.equals(DC_MINT)) { - if (input.equals(HNT_MINT)) { - return setInputAmount( - toNumber( - dcToNetworkTokens(toBN(balance, outputDecimals)) || new BN(0), - inputDecimals, - ), - ) - } + return setInputAmount(0) + } + + const handleJupiterRoute = async () => { + const route = await getRoute({ + amount: balance.toNumber(), + inputMint: output.toBase58(), + outputMint: input.toBase58(), + slippageBps, + }) + + if (!route) { setRouteNotFound(true) + return setInputAmount(0) } - if (isDevnet) { - if (price && !input.equals(HNT_MINT)) { - return setInputAmount(price) - } - return setInputAmount(0) + return setInputAmount( + toNumber(new BN(Number(route?.outAmount || 0)), inputDecimals), + ) + } + + const handleDCConversion = () => { + if (input.equals(HNT_MINT)) { + return setInputAmount( + toNumber( + dcToNetworkTokens(toBN(balance, outputDecimals)) || new BN(0), + inputDecimals, + ), + ) } + setRouteNotFound(true) return setInputAmount(0) } + + if (isDevnet) return handleDevnetPrice() + if (output.equals(DC_MINT)) return handleDCConversion() + return handleJupiterRoute() }, [ dcToNetworkTokens,