fix: reintroduce swap proposal autorefresh

This commit is contained in:
Dario Gabriel Lipicar 2024-10-25 00:52:57 -03:00
parent 52f59bc60b
commit ea7deb12fa
No known key found for this signature in database
GPG Key ID: 9625E9494309D203
3 changed files with 71 additions and 12 deletions

View File

@ -20,7 +20,7 @@ QtObject {
// default token key // default token key
property string defaultToTokenKey: "" property string defaultToTokenKey: ""
// 15 seconds // 15 seconds
property int autoRefreshTime: 15000 property int expirationTime: 15000
onSelectedAccountAddressChanged: root.formValuesChanged() onSelectedAccountAddressChanged: root.formValuesChanged()
onSelectedNetworkChainIdChanged: root.formValuesChanged() onSelectedNetworkChainIdChanged: root.formValuesChanged()

View File

@ -40,13 +40,7 @@ StatusDialog {
}) })
function fetchSuggestedRoutes() { function fetchSuggestedRoutes() {
if (root.swapInputParamsForm.isFormFilledCorrectly()) { root.swapAdaptor.prepareFetchSuggestedRoutes()
root.swapAdaptor.swapProposalLoading = true
}
root.swapAdaptor.validSwapProposalReceived = false
root.swapAdaptor.approvalPending = false
root.swapAdaptor.approvalSuccessful = false
root.swapAdaptor.swapOutputData.resetPathInfoAndError()
debounceFetchSuggestedRoutes() debounceFetchSuggestedRoutes()
} }
@ -61,6 +55,26 @@ StatusDialog {
function addMetricsEvent(subEventName) { function addMetricsEvent(subEventName) {
Global.addCentralizedMetricIfEnabled("swap", {subEvent: subEventName}) Global.addCentralizedMetricIfEnabled("swap", {subEvent: subEventName})
} }
property bool isReviewingApproveTx: false
property bool isReviewingSwapTx: false
readonly property bool hasStartedTxProcess: root.swapAdaptor.approvalPending || root.swapAdaptor.approvalSuccessful
readonly property bool canRefreshSwapProposal: !isReviewingApproveTx && !isReviewingSwapTx && !hasStartedTxProcess
function checkSwapProposalAutoRefresh() {
// Refresh the swap proposal if it has expired and:
// - the user isn't reviewing/signing a transaction
// - the user hasn't placed an approval or swap tx
if (root.swapAdaptor.swapProposalExpired && d.canRefreshSwapProposal) {
d.fetchSuggestedRoutes()
}
}
onCanRefreshSwapProposalChanged: {
if (canRefreshSwapProposal) {
d.checkSwapProposalAutoRefresh()
}
}
} }
Connections { Connections {
@ -80,6 +94,16 @@ StatusDialog {
} }
} }
Connections {
target: root.swapAdaptor
function onSwapProposalExpiredChanged() {
if (root.swapAdaptor.swapProposalExpired) {
d.checkSwapProposalAutoRefresh()
}
}
}
// needed as the first time the value not loaded correctly without this Binding // needed as the first time the value not loaded correctly without this Binding
Binding { Binding {
target: root.swapAdaptor target: root.swapAdaptor
@ -440,9 +464,7 @@ StatusDialog {
feesLoading: root.swapAdaptor.swapProposalLoading feesLoading: root.swapAdaptor.swapProposalLoading
fromTokenSymbol: root.swapAdaptor.fromToken.symbol fromTokenSymbol: root.swapAdaptor.fromToken.symbol
fromTokenAmount: SQUtils.AmountsArithmetic.div( fromTokenAmount: root.swapInputParamsForm.fromTokenAmount
SQUtils.AmountsArithmetic.fromString(root.swapAdaptor.swapOutputData.approvalAmountRequired),
SQUtils.AmountsArithmetic.fromNumber(1, root.swapAdaptor.fromToken.decimals ?? 18)).toFixed()
fromTokenContractAddress: SQUtils.ModelUtils.getByKey(root.swapAdaptor.fromToken.addressPerChain, fromTokenContractAddress: SQUtils.ModelUtils.getByKey(root.swapAdaptor.fromToken.addressPerChain,
"chainId", root.swapInputParamsForm.selectedNetworkChainId, "chainId", root.swapInputParamsForm.selectedNetworkChainId,
"address") "address")
@ -477,6 +499,10 @@ StatusDialog {
d.addMetricsEvent("send approve tx") d.addMetricsEvent("send approve tx")
root.swapAdaptor.sendApproveTx() root.swapAdaptor.sendApproveTx()
} }
onVisibleChanged: {
d.isReviewingApproveTx = visible
}
} }
} }
@ -533,6 +559,10 @@ StatusDialog {
root.swapAdaptor.sendSwapTx() root.swapAdaptor.sendSwapTx()
root.close() root.close()
} }
onVisibleChanged: {
d.isReviewingSwapTx = visible
}
} }
} }
} }

View File

@ -19,9 +19,10 @@ QObject {
required property SwapInputParamsForm swapFormData required property SwapInputParamsForm swapFormData
required property SwapOutputData swapOutputData required property SwapOutputData swapOutputData
// the below 2 properties holds the state of finding a swap proposal // the below 3 properties holds the state of finding a swap proposal
property bool validSwapProposalReceived: false property bool validSwapProposalReceived: false
property bool swapProposalLoading: false property bool swapProposalLoading: false
property bool swapProposalExpired: false // We consider a swap proposal "expired" some time after it was received
// the below 2 properties holds the state of finding a swap proposal // the below 2 properties holds the state of finding a swap proposal
property bool approvalPending: false property bool approvalPending: false
@ -193,6 +194,15 @@ QObject {
} }
return "" return ""
} }
property Timer swapProposalExpiredTimer: Timer {
interval: root.swapFormData.expirationTime
running: false
repeat: false
onTriggered: {
root.swapProposalExpired = true
}
}
} }
ModelEntry { ModelEntry {
@ -246,6 +256,9 @@ QObject {
root.swapOutputData.approvalContractAddress = !!bestPath ? bestPath.approvalContractAddress: "" root.swapOutputData.approvalContractAddress = !!bestPath ? bestPath.approvalContractAddress: ""
root.swapOutputData.estimatedTime = !!bestPath ? bestPath.estimatedTime: Constants.TransactionEstimatedTime.Unknown root.swapOutputData.estimatedTime = !!bestPath ? bestPath.estimatedTime: Constants.TransactionEstimatedTime.Unknown
root.swapOutputData.txProviderName = !!bestPath ? bestPath.bridgeName: "" root.swapOutputData.txProviderName = !!bestPath ? bestPath.bridgeName: ""
if (!root.swapProposalExpired) {
d.swapProposalExpiredTimer.start()
}
} else { } else {
root.swapOutputData.hasError = true root.swapOutputData.hasError = true
} }
@ -274,6 +287,7 @@ QObject {
} }
function reset() { function reset() {
d.uuid = ""
root.swapFormData.resetFormData() root.swapFormData.resetFormData()
root.swapOutputData.reset() root.swapOutputData.reset()
root.validSwapProposalReceived = false root.validSwapProposalReceived = false
@ -281,6 +295,8 @@ QObject {
root.approvalPending = false root.approvalPending = false
root.approvalSuccessful = false root.approvalSuccessful = false
d.txHash = "" d.txHash = ""
root.swapProposalExpired = false
d.swapProposalExpiredTimer.stop()
} }
function formatCurrencyAmount(balance, symbol, options = null, locale = null) { function formatCurrencyAmount(balance, symbol, options = null, locale = null) {
@ -302,6 +318,19 @@ QObject {
return disabledChainIds.join(":") return disabledChainIds.join(":")
} }
function prepareFetchSuggestedRoutes() {
d.uuid = ""
if (root.swapFormData.isFormFilledCorrectly()) {
root.swapProposalLoading = true
}
root.swapProposalExpired = false
d.swapProposalExpiredTimer.stop()
root.validSwapProposalReceived = false
root.approvalPending = false
root.approvalSuccessful = false
root.swapOutputData.resetPathInfoAndError()
}
function fetchSuggestedRoutes(cryptoValueInRaw) { function fetchSuggestedRoutes(cryptoValueInRaw) {
root.swapFormData.toTokenAmount = "" root.swapFormData.toTokenAmount = ""
if (root.swapFormData.isFormFilledCorrectly() && !!cryptoValueInRaw) { if (root.swapFormData.isFormFilledCorrectly() && !!cryptoValueInRaw) {