Commit 95814e32 authored by eddie's avatar eddie Committed by GitHub

fix: prevent race condition for swap state (#6624)

parent caa2524e
...@@ -417,10 +417,20 @@ export function Swap({ ...@@ -417,10 +417,20 @@ export function Swap({
if (stablecoinPriceImpact && !confirmPriceImpactWithoutFee(stablecoinPriceImpact)) { if (stablecoinPriceImpact && !confirmPriceImpactWithoutFee(stablecoinPriceImpact)) {
return return
} }
setSwapState({ attemptingTxn: true, tradeToConfirm, showConfirm, swapErrorMessage: undefined, txHash: undefined }) setSwapState((currentState) => ({
...currentState,
attemptingTxn: true,
swapErrorMessage: undefined,
txHash: undefined,
}))
swapCallback() swapCallback()
.then((hash) => { .then((hash) => {
setSwapState({ attemptingTxn: false, tradeToConfirm, showConfirm, swapErrorMessage: undefined, txHash: hash }) setSwapState((currentState) => ({
...currentState,
attemptingTxn: false,
swapErrorMessage: undefined,
txHash: hash,
}))
sendEvent({ sendEvent({
category: 'Swap', category: 'Swap',
action: 'transaction hash', action: 'transaction hash',
...@@ -440,19 +450,16 @@ export function Swap({ ...@@ -440,19 +450,16 @@ export function Swap({
}) })
}) })
.catch((error) => { .catch((error) => {
setSwapState({ setSwapState((currentState) => ({
...currentState,
attemptingTxn: false, attemptingTxn: false,
tradeToConfirm,
showConfirm,
swapErrorMessage: error.message, swapErrorMessage: error.message,
txHash: undefined, txHash: undefined,
}) }))
}) })
}, [ }, [
swapCallback, swapCallback,
stablecoinPriceImpact, stablecoinPriceImpact,
tradeToConfirm,
showConfirm,
recipient, recipient,
recipientAddress, recipientAddress,
account, account,
...@@ -471,16 +478,16 @@ export function Swap({ ...@@ -471,16 +478,16 @@ export function Swap({
}, [stablecoinPriceImpact, trade]) }, [stablecoinPriceImpact, trade])
const handleConfirmDismiss = useCallback(() => { 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 there was a tx hash, we want to clear the input
if (txHash) { if (txHash) {
onUserInput(Field.INPUT, '') onUserInput(Field.INPUT, '')
} }
}, [attemptingTxn, onUserInput, swapErrorMessage, tradeToConfirm, txHash]) }, [onUserInput, txHash])
const handleAcceptChanges = useCallback(() => { const handleAcceptChanges = useCallback(() => {
setSwapState({ tradeToConfirm: trade, swapErrorMessage, txHash, attemptingTxn, showConfirm }) setSwapState((currentState) => ({ ...currentState, tradeToConfirm: trade }))
}, [attemptingTxn, showConfirm, swapErrorMessage, trade, txHash]) }, [trade])
const handleInputSelect = useCallback( const handleInputSelect = useCallback(
(inputCurrency: Currency) => { (inputCurrency: Currency) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment