Skip to content

Commit 699ec1a

Browse files
Andrew718PLTSAndrew718PLTS
and
Andrew718PLTS
authored
Fix: Prevented clicks on 'Swap' button when it's not ready to send txs (#196)
### Description It prevents clicking the 'Swap' button on the 'Confirm Swap' state by adding a button disable condition that checks if the txs (approval & swap or swap only) are ready to be confirmed. When it's not ready, the button is disabled and has the grey colour as we do for the 'Continue' button on the 'Swap' state. ### Other changes Fixed this case: Sometimes, it shows the 'Sending swap transaction' text when there's no allowance to skip approval tx (therefore, there should be two TXs, approval and swap, and corresponding the 'Sending two transactions: Approval and Swap' text) ### Tested **Description:** There are two cases to test: 1. When there are two txs - an approval and a swap. 2. When there is only one tx - a swap one. **STR:** 1. Navigate the app and connect your wallet. 3. Fill out the form with any amount. 4. Click the 'Continue' button as soon as it's enabled. 5. Click the 'Swap' button as soon as it's enabled. **Expected result:** 1. The tx/s are sent as soon as you click the 'Swap' button. 2. It displays the 'Sending swap transaction', with only one tx to swap directly. 3. It displays the 'Sending two transactions: Approval and Swap', with two txs to approve and swap. 4. Swapped successfully; the balances are updated. ### 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) --------- Co-authored-by: Andrew718PLTS <[email protected]>
1 parent 11847ac commit 699ec1a

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/components/buttons/3DButton.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const Button3D = ({
3535
disabled={isDisabled}
3636
>
3737
<span
38-
className={`group font-inter outline-offset-4 cursor-pointer ${getSubstrateButtonColor({
38+
className={`group font-inter outline-offset-4 cursor-pointer ${getShadowButtonColor({
3939
isDisabled,
4040
isWalletConnected,
4141
isError,
@@ -63,7 +63,7 @@ export const Button3D = ({
6363
)
6464
}
6565

66-
function getSubstrateButtonColor({
66+
function getShadowButtonColor({
6767
isDisabled,
6868
isWalletConnected,
6969
isError,

src/features/swap/SwapConfirm.tsx

+22-21
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,22 @@ export function SwapConfirmCard({ formValues }: Props) {
147147
return
148148
}
149149

150-
if (!sendApproveTx || isApproveTxSuccess || isApproveTxLoading) {
151-
logger.debug('Approve already started or finished, ignoring submit')
152-
return
153-
}
154-
155-
try {
156-
logger.info('Sending approve tx')
157-
const approveResult = await sendApproveTx()
158-
const approveReceipt = await approveResult.wait(1)
159-
toastToYourSuccess('Approve complete, starting swap', approveReceipt.transactionHash, chainId)
160-
setApproveConfirmed(true)
161-
logger.info(`Tx receipt received for approve: ${approveReceipt.transactionHash}`)
162-
} catch (error) {
163-
logger.error('Failed to approve token', error)
164-
setIsModalOpen(false)
150+
if (!skipApprove && sendApproveTx) {
151+
try {
152+
logger.info('Sending approve tx')
153+
const approveResult = await sendApproveTx()
154+
const approveReceipt = await approveResult.wait(1)
155+
toastToYourSuccess(
156+
'Approve complete, starting swap',
157+
approveReceipt.transactionHash,
158+
chainId
159+
)
160+
setApproveConfirmed(true)
161+
logger.info(`Tx receipt received for approve: ${approveReceipt.transactionHash}`)
162+
} catch (error) {
163+
logger.error('Failed to approve token', error)
164+
setIsModalOpen(false)
165+
}
165166
}
166167
}
167168

@@ -191,6 +192,8 @@ export function SwapConfirmCard({ formValues }: Props) {
191192
refetch().catch((e) => logger.error('Failed to refetch quote:', e))
192193
}
193194

195+
const isSwapReady = !sendApproveTx || isApproveTxSuccess || isApproveTxLoading
196+
194197
return (
195198
<FloatingBox
196199
width="w-screen md:w-[432px] "
@@ -238,7 +241,7 @@ export function SwapConfirmCard({ formValues }: Props) {
238241
</div>
239242

240243
<div className="flex w-full px-6 pb-6 mt-6">
241-
<Button3D isFullWidth onClick={onSubmit}>
244+
<Button3D isFullWidth onClick={onSubmit} isDisabled={isSwapReady}>
242245
Swap
243246
</Button3D>
244247
</div>
@@ -248,7 +251,7 @@ export function SwapConfirmCard({ formValues }: Props) {
248251
close={() => setIsModalOpen(false)}
249252
width="max-w-[432px]"
250253
>
251-
<MentoLogoLoader needsApproval={needsApproval} />
254+
<MentoLogoLoader skipApprove={skipApprove} />
252255
</Modal>
253256
</FloatingBox>
254257
)
@@ -326,7 +329,7 @@ const ChevronRight = (props: SVGProps<SVGSVGElement>) => (
326329
</svg>
327330
)
328331

329-
const MentoLogoLoader = ({ needsApproval }: { needsApproval: boolean }) => {
332+
const MentoLogoLoader = ({ skipApprove }: { skipApprove: boolean }) => {
330333
const { connector } = useAccount()
331334

332335
return (
@@ -342,9 +345,7 @@ const MentoLogoLoader = ({ needsApproval }: { needsApproval: boolean }) => {
342345

343346
<div className="my-6">
344347
<div className="text-sm text-center text-[#636768] dark:text-[#AAB3B6]">
345-
{needsApproval
346-
? 'Sending two transactions: Approve and Swap'
347-
: 'Sending swap transaction'}
348+
{skipApprove ? 'Sending swap transaction' : 'Sending two transactions: Approve and Swap'}
348349
</div>
349350
<div className="mt-3 text-sm text-center text-[#636768] dark:text-[#AAB3B6]">
350351
{`Sign with ${connector?.name || 'wallet'} to proceed`}

0 commit comments

Comments
 (0)