Skip to content

Commit

Permalink
fix: prevent race condition for swap state (#6624)
Browse files Browse the repository at this point in the history
  • Loading branch information
just-toby committed May 22, 2023
1 parent caa2524 commit 95814e3
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,20 @@ export function Swap({
if (stablecoinPriceImpact && !confirmPriceImpactWithoutFee(stablecoinPriceImpact)) {
return
}
setSwapState({ attemptingTxn: true, tradeToConfirm, showConfirm, swapErrorMessage: undefined, txHash: undefined })
setSwapState((currentState) => ({
...currentState,
attemptingTxn: true,
swapErrorMessage: undefined,
txHash: undefined,
}))
swapCallback()
.then((hash) => {
setSwapState({ attemptingTxn: false, tradeToConfirm, showConfirm, swapErrorMessage: undefined, txHash: hash })
setSwapState((currentState) => ({
...currentState,
attemptingTxn: false,
swapErrorMessage: undefined,
txHash: hash,
}))
sendEvent({
category: 'Swap',
action: 'transaction hash',
Expand All @@ -440,19 +450,16 @@ export function Swap({
})
})
.catch((error) => {
setSwapState({
setSwapState((currentState) => ({
...currentState,
attemptingTxn: false,
tradeToConfirm,
showConfirm,
swapErrorMessage: error.message,
txHash: undefined,
})
}))
})
}, [
swapCallback,
stablecoinPriceImpact,
tradeToConfirm,
showConfirm,
recipient,
recipientAddress,
account,
Expand All @@ -471,16 +478,16 @@ export function Swap({
}, [stablecoinPriceImpact, trade])

const handleConfirmDismiss = useCallback(() => {
setSwapState({ showConfirm: false, tradeToConfirm, attemptingTxn, swapErrorMessage, txHash })
setSwapState((currentState) => ({ ...currentState, showConfirm: false }))
// if there was a tx hash, we want to clear the input
if (txHash) {
onUserInput(Field.INPUT, '')
}
}, [attemptingTxn, onUserInput, swapErrorMessage, tradeToConfirm, txHash])
}, [onUserInput, txHash])

const handleAcceptChanges = useCallback(() => {
setSwapState({ tradeToConfirm: trade, swapErrorMessage, txHash, attemptingTxn, showConfirm })
}, [attemptingTxn, showConfirm, swapErrorMessage, trade, txHash])
setSwapState((currentState) => ({ ...currentState, tradeToConfirm: trade }))
}, [trade])

const handleInputSelect = useCallback(
(inputCurrency: Currency) => {
Expand Down

1 comment on commit 95814e3

@vercel
Copy link

@vercel vercel bot commented on 95814e3 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

interface – ./

interface-git-main-uniswap.vercel.app
interface-uniswap.vercel.app

Please sign in to comment.