Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit acdc35a

Browse files
Fix: Prevented infinity swap tx sending on user rejection (#204)
### Description It prevents infinity swap tx sending on user rejection by adding the `swapRejectedRef` setting when it's been rejected by the user (checking via error message). **Additionally, to read:** The `useState` doesn't work in this case because the React state updates asynchronously, so useEffect does not immediately pick up the change in isSwapRejected during the first rejection (that's why I had a case when the first rejection is ignored). ### Other changes None ### Tested Reject when there's an approval and a swap TXs. 1. Select tokens to have an approval TX first (the eXOF would be a good choice). 2. Click the 'Continue' button. 3. Wait until the 'Approve ${token}' button is enabled to click. 4. Click the 'Approve ${token}' button. 5. Approve the approval TX. 6. Reject the swap TX. Reject when there's no approval TX. 1. Select tokens to avoid an approval TX (a precondition is to swap some tokens before). 2. Click the 'Continue' button. 3. Wait until the 'Swap' button is enabled to click. 4. Click the 'Swap' button. 5. Reject the swap TX. ### Related issues - Fixes #issue number here ### Checklist before requesting a review - [ ] I have performed a self-review of my own code - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] The PR title follows the [conventions](https://www.notion.so/Git-Branching-and-Commit-Message-Conventions-18f66f7d06444cfcbac5725ffbc7c04a?pvs=4#9355048863c549ef92fe210a8a1298aa) - [ ] I have run the [regression tests](https://www.notion.so/Mento-Web-App-Regression-Tests-37bd43a7da8d4e38b65993320a33d557)
1 parent ff6f1a1 commit acdc35a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/features/swap/SwapConfirm.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Lottie from 'lottie-react'
2-
import { SVGProps, useEffect, useState } from 'react'
2+
import { SVGProps, useEffect, useRef, useState } from 'react'
33
import mentoLoaderBlue from 'src/animations/Mentoloader_blue.json'
44
import mentoLoaderGreen from 'src/animations/Mentoloader_green.json'
55
import { toastToYourSuccess } from 'src/components/TxSuccessToast'
@@ -150,7 +150,7 @@ export function SwapConfirmCard({ formValues }: Props) {
150150
const approveResult = await sendApproveTx()
151151
const approveReceipt = await approveResult.wait(1)
152152
toastToYourSuccess(
153-
'Approve complete, starting swap',
153+
'Approve complete! Sending swap tx',
154154
approveReceipt.transactionHash,
155155
chainId
156156
)
@@ -163,19 +163,32 @@ export function SwapConfirmCard({ formValues }: Props) {
163163
}
164164
}
165165

166+
const swapRejectedRef = useRef(false)
167+
166168
// TODO find a way to have this trigger from the onSubmit
167169
useEffect(() => {
168-
if (isSwapTxLoading || isSwapTxSuccess || !isApproveTxSuccess || !sendSwapTx) return
170+
if (
171+
isSwapTxLoading ||
172+
isSwapTxSuccess ||
173+
!isApproveTxSuccess ||
174+
!sendSwapTx ||
175+
swapRejectedRef.current
176+
)
177+
return
169178
logger.info('Sending swap tx')
170179
sendSwapTx()
171180
.then((swapResult) => swapResult.wait(1))
172181
.then((swapReceipt) => {
173182
logger.info(`Tx receipt received for swap: ${swapReceipt.transactionHash}`)
174183
toastToYourSuccess('Swap Complete!', swapReceipt.transactionHash, chainId)
175184
dispatch(setFormValues(null))
185+
swapRejectedRef.current = false
176186
})
177-
.catch((e) => {
178-
logger.error('Swap failure:', e)
187+
.catch((error) => {
188+
logger.error('Swap failure:', error)
189+
if (error.message === 'User rejected request') {
190+
swapRejectedRef.current = true
191+
}
179192
})
180193
.finally(() => setIsModalOpen(false))
181194
}, [isApproveTxSuccess, isSwapTxLoading, isSwapTxSuccess, sendSwapTx, chainId, dispatch])

0 commit comments

Comments
 (0)