Skip to content

Commit

Permalink
fix(swap): invalidate old uuid and route when a new route request is …
Browse files Browse the repository at this point in the history
…made

Fixes #16574
  • Loading branch information
dlipicar committed Oct 31, 2024
1 parent 5aa5390 commit ea94065
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
56 changes: 56 additions & 0 deletions storybook/qmlTests/tests/tst_SwapModal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
13 changes: 5 additions & 8 deletions ui/app/AppLayouts/Wallet/popups/swap/SwapModal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== ""
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions ui/app/AppLayouts/Wallet/popups/swap/SwapModalAdaptor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ QObject {
}

function reset() {
d.uuid = ""
root.swapFormData.resetFormData()
root.swapOutputData.reset()
root.validSwapProposalReceived = false
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ea94065

Please sign in to comment.