From ea94065c57051597b5cf43f2e0f04a3774f248fb Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Mon, 28 Oct 2024 18:38:14 -0300 Subject: [PATCH] fix(swap): invalidate old uuid and route when a new route request is made Fixes #16574 --- storybook/qmlTests/tests/tst_SwapModal.qml | 56 +++++++++++++++++++ .../Wallet/popups/swap/SwapModal.qml | 13 ++--- .../Wallet/popups/swap/SwapModalAdaptor.qml | 9 +++ 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/storybook/qmlTests/tests/tst_SwapModal.qml b/storybook/qmlTests/tests/tst_SwapModal.qml index 497de5411bb..d7b45f18661 100644 --- a/storybook/qmlTests/tests/tst_SwapModal.qml +++ b/storybook/qmlTests/tests/tst_SwapModal.qml @@ -1866,5 +1866,61 @@ Item { // wait(root.swapFormData.autoRefreshTime*2) // compare(fetchSuggestedRoutesCalled.count, 2) } + + function test_uuid_change() { + root.swapFormData.fromTokenAmount = "0.0001" + root.swapFormData.selectedAccountAddress = "0x7F47C2e18a4BBf5487E6fb082eC2D9Ab0E6d7240" + root.swapFormData.selectedNetworkChainId = 11155111 + root.swapFormData.fromTokensKey = "ETH" + root.swapFormData.toTokenKey = "STT" + + // Launch popup + launchAndVerfyModal() + + const payPanel = findChild(controlUnderTest, "payPanel") + verify(!!payPanel) + + const receivePanel = findChild(controlUnderTest, "receivePanel") + verify(!!receivePanel) + + waitForItemPolished(controlUnderTest.contentItem) + + // check if fetchSuggestedRoutes called + fetchSuggestedRoutesCalled.wait() + + // emit routes ready + let txHasRouteNoApproval = root.dummySwapTransactionRoutes.txHasRouteNoApproval + txHasRouteNoApproval.uuid = root.swapAdaptor.uuid + root.swapStore.suggestedRoutesReady(txHasRouteNoApproval, "", "") + + let lastUuid = root.swapAdaptor.uuid + + // edit some params to retry swap + root.swapFormData.fromTokenAmount = "0.00011" + waitForRendering(receivePanel) + formValuesChanged.wait() + // verify loading state was set and no errors currently + verifyLoadingAndNoErrorsState(payPanel, receivePanel) + + // uuid changed + verify(root.swapAdaptor.uuid !== lastUuid) + + // emit event with route that needs no approval for previous uuid + txHasRouteNoApproval.uuid = lastUuid + root.swapStore.suggestedRoutesReady(txHasRouteNoApproval, "", "") + + // route with old uuid should have been ignored + verifyLoadingAndNoErrorsState(payPanel, receivePanel) + + // emit routes ready + txHasRouteNoApproval.uuid = root.swapAdaptor.uuid + root.swapStore.suggestedRoutesReady(txHasRouteNoApproval, "", "") + + // verify loading state removed and data is displayed as expected on the Modal + verify(root.swapAdaptor.validSwapProposalReceived) + verify(!root.swapAdaptor.swapProposalLoading) + + closeAndVerfyModal() + } } } diff --git a/ui/app/AppLayouts/Wallet/popups/swap/SwapModal.qml b/ui/app/AppLayouts/Wallet/popups/swap/SwapModal.qml index a183a6300e7..12ac1ea1fa6 100644 --- a/ui/app/AppLayouts/Wallet/popups/swap/SwapModal.qml +++ b/ui/app/AppLayouts/Wallet/popups/swap/SwapModal.qml @@ -41,14 +41,13 @@ StatusDialog { }) function fetchSuggestedRoutes() { + root.swapAdaptor.invalidateSuggestedRoute() if (root.swapInputParamsForm.isFormFilledCorrectly()) { root.swapAdaptor.swapProposalLoading = true + debounceFetchSuggestedRoutes() + } else { + root.swapAdaptor.swapProposalLoading = false } - root.swapAdaptor.validSwapProposalReceived = false - root.swapAdaptor.approvalPending = false - root.swapAdaptor.approvalSuccessful = false - root.swapAdaptor.swapOutputData.resetPathInfoAndError() - debounceFetchSuggestedRoutes() } readonly property bool isError: root.swapAdaptor.errorMessage !== "" @@ -464,9 +463,7 @@ StatusDialog { feesLoading: root.swapAdaptor.swapProposalLoading fromTokenSymbol: root.swapAdaptor.fromToken.symbol - fromTokenAmount: SQUtils.AmountsArithmetic.div( - SQUtils.AmountsArithmetic.fromString(root.swapAdaptor.swapOutputData.approvalAmountRequired), - SQUtils.AmountsArithmetic.fromNumber(1, root.swapAdaptor.fromToken.decimals ?? 18)).toFixed() + fromTokenAmount: root.swapInputParamsForm.fromTokenAmount fromTokenContractAddress: SQUtils.ModelUtils.getByKey(root.swapAdaptor.fromToken.addressPerChain, "chainId", root.swapInputParamsForm.selectedNetworkChainId, "address") diff --git a/ui/app/AppLayouts/Wallet/popups/swap/SwapModalAdaptor.qml b/ui/app/AppLayouts/Wallet/popups/swap/SwapModalAdaptor.qml index 2d72b88b993..c1f5321cffc 100644 --- a/ui/app/AppLayouts/Wallet/popups/swap/SwapModalAdaptor.qml +++ b/ui/app/AppLayouts/Wallet/popups/swap/SwapModalAdaptor.qml @@ -274,6 +274,7 @@ QObject { } function reset() { + d.uuid = "" root.swapFormData.resetFormData() root.swapOutputData.reset() root.validSwapProposalReceived = false @@ -302,6 +303,14 @@ QObject { return disabledChainIds.join(":") } + function invalidateSuggestedRoute() { + d.uuid = "" + root.validSwapProposalReceived = false + root.approvalPending = false + root.approvalSuccessful = false + root.swapOutputData.resetPathInfoAndError() + } + function fetchSuggestedRoutes(cryptoValueInRaw) { root.swapFormData.toTokenAmount = "" if (root.swapFormData.isFormFilledCorrectly() && !!cryptoValueInRaw) {