2024-05-15 21:22:13 +00:00
|
|
|
import QtQml 2.15
|
|
|
|
import SortFilterProxyModel 0.2
|
|
|
|
|
|
|
|
import StatusQ 0.1
|
|
|
|
import StatusQ.Core.Utils 0.1
|
|
|
|
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
import shared.stores 1.0
|
2024-06-07 12:27:56 +00:00
|
|
|
import AppLayouts.Wallet 1.0
|
2024-05-15 21:22:13 +00:00
|
|
|
import AppLayouts.Wallet.stores 1.0 as WalletStore
|
|
|
|
|
|
|
|
QObject {
|
|
|
|
id: root
|
|
|
|
|
|
|
|
required property CurrenciesStore currencyStore
|
|
|
|
required property WalletStore.WalletAssetsStore walletAssetsStore
|
|
|
|
required property WalletStore.SwapStore swapStore
|
|
|
|
required property SwapInputParamsForm swapFormData
|
2024-06-06 14:05:31 +00:00
|
|
|
required property SwapOutputData swapOutputData
|
2024-05-15 21:22:13 +00:00
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
// the below 2 properties holds the state of finding a swap proposal
|
|
|
|
property bool validSwapProposalReceived: false
|
2024-06-04 11:58:37 +00:00
|
|
|
property bool swapProposalLoading: false
|
|
|
|
|
2024-06-24 13:52:10 +00:00
|
|
|
// the below 2 properties holds the state of finding a swap proposal
|
|
|
|
property bool approvalPending: false
|
|
|
|
property bool approvalSuccessful: false
|
|
|
|
|
2024-07-19 03:36:36 +00:00
|
|
|
// the below property holds internal checks done by the SwapModal
|
|
|
|
property bool amountEnteredGreaterThanBalance: false
|
|
|
|
|
2024-06-04 11:58:37 +00:00
|
|
|
// To expose the selected from and to Token from the SwapModal
|
2024-06-07 12:27:56 +00:00
|
|
|
readonly property var fromToken: fromTokenEntry.item
|
|
|
|
readonly property var toToken: toTokenEntry.item
|
2024-07-10 18:35:24 +00:00
|
|
|
readonly property var selectedAccount: selectedAccountEntry.item
|
2024-06-04 11:58:37 +00:00
|
|
|
|
2024-06-24 13:52:10 +00:00
|
|
|
readonly property string uuid: d.uuid
|
|
|
|
|
2024-07-03 20:46:00 +00:00
|
|
|
// TO REVIEW: It has been created a `WalletAccountsAdaptor.qml` file.
|
|
|
|
// Probably this data transformation should live there since they have common base.
|
2024-05-15 21:22:13 +00:00
|
|
|
readonly property var nonWatchAccounts: SortFilterProxyModel {
|
|
|
|
sourceModel: root.swapStore.accounts
|
2024-07-15 11:17:42 +00:00
|
|
|
delayed: true // Delayed to allow `processAccountBalance` dependencies to be resolved
|
2024-05-15 21:22:13 +00:00
|
|
|
filters: ValueFilter {
|
2024-07-11 14:47:51 +00:00
|
|
|
roleName: "canSend"
|
|
|
|
value: true
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
2024-06-25 11:10:46 +00:00
|
|
|
sorters: [
|
|
|
|
RoleSorter { roleName: "currencyBalanceDouble"; sortOrder: Qt.DescendingOrder },
|
|
|
|
RoleSorter { roleName: "position"; sortOrder: Qt.AscendingOrder }
|
|
|
|
]
|
2024-05-15 21:22:13 +00:00
|
|
|
proxyRoles: [
|
|
|
|
FastExpressionRole {
|
|
|
|
name: "accountBalance"
|
2024-07-15 11:17:42 +00:00
|
|
|
expression: {
|
|
|
|
// dependencies
|
|
|
|
root.swapFormData.fromTokensKey
|
|
|
|
root.fromToken
|
|
|
|
root.fromToken.symbol
|
|
|
|
root.fromToken.decimals
|
|
|
|
root.swapFormData.selectedNetworkChainId
|
|
|
|
root.swapFormData.fromTokensKey
|
|
|
|
|
|
|
|
return d.processAccountBalance(model.address)
|
|
|
|
}
|
2024-05-15 21:22:13 +00:00
|
|
|
expectedRoles: ["address"]
|
|
|
|
},
|
2024-06-25 11:10:46 +00:00
|
|
|
FastExpressionRole {
|
|
|
|
name: "currencyBalanceDouble"
|
|
|
|
expression: model.currencyBalance.amount
|
|
|
|
expectedRoles: ["currencyBalance"]
|
|
|
|
},
|
2024-05-15 21:22:13 +00:00
|
|
|
FastExpressionRole {
|
|
|
|
name: "fromToken"
|
2024-06-04 11:58:37 +00:00
|
|
|
expression: root.fromToken
|
2024-06-07 12:27:56 +00:00
|
|
|
},
|
|
|
|
FastExpressionRole {
|
|
|
|
name: "colorizedChainPrefixes"
|
|
|
|
function getChainShortNames(chainIds) {
|
2024-07-03 20:46:00 +00:00
|
|
|
const chainShortNames = WalletUtils.getNetworkShortNames(chainIds, root.filteredFlatNetworksModel)
|
2024-06-07 12:27:56 +00:00
|
|
|
return WalletUtils.colorizedChainPrefix(chainShortNames)
|
|
|
|
}
|
|
|
|
expression: getChainShortNames(model.preferredSharingChainIds)
|
|
|
|
expectedRoles: ["preferredSharingChainIds"]
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:42:26 +00:00
|
|
|
readonly property SortFilterProxyModel filteredFlatNetworksModel: SortFilterProxyModel {
|
|
|
|
sourceModel: root.swapStore.flatNetworks
|
|
|
|
filters: ValueFilter { roleName: "isTest"; value: root.swapStore.areTestNetworksEnabled }
|
|
|
|
}
|
|
|
|
|
2024-07-19 03:36:36 +00:00
|
|
|
readonly property string errorMessage: d.errorMessage
|
|
|
|
readonly property bool isEthBalanceInsufficient: d.isEthBalanceInsufficient
|
|
|
|
readonly property bool isTokenBalanceInsufficient: d.isTokenBalanceInsufficient
|
|
|
|
|
2024-07-03 22:08:03 +00:00
|
|
|
signal suggestedRoutesReady()
|
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
QtObject {
|
|
|
|
id: d
|
2024-05-15 21:22:13 +00:00
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
property string uuid
|
2024-06-24 13:52:10 +00:00
|
|
|
// storing txHash to verify against tx completed event
|
|
|
|
property string txHash
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-06-25 11:10:46 +00:00
|
|
|
readonly property SubmodelProxyModel filteredBalancesModel: SubmodelProxyModel {
|
2024-06-06 14:05:31 +00:00
|
|
|
sourceModel: root.walletAssetsStore.baseGroupedAccountAssetModel
|
|
|
|
submodelRoleName: "balances"
|
|
|
|
delegateModel: SortFilterProxyModel {
|
|
|
|
sourceModel: joinModel
|
|
|
|
filters: ValueFilter {
|
2024-05-28 17:39:41 +00:00
|
|
|
roleName: "chainId"
|
|
|
|
value: root.swapFormData.selectedNetworkChainId
|
2024-06-06 14:05:31 +00:00
|
|
|
}
|
|
|
|
readonly property LeftJoinModel joinModel: LeftJoinModel {
|
|
|
|
leftModel: submodel
|
|
|
|
rightModel: root.swapStore.flatNetworks
|
|
|
|
|
|
|
|
joinRole: "chainId"
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
function processAccountBalance(address) {
|
2024-06-25 21:33:25 +00:00
|
|
|
if (!root.swapFormData.fromTokensKey || !root.fromToken) {
|
2024-06-07 12:27:56 +00:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
let network = ModelUtils.getByKey(root.filteredFlatNetworksModel, "chainId", root.swapFormData.selectedNetworkChainId)
|
2024-06-07 12:27:56 +00:00
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
let balancesModel = ModelUtils.getByKey(filteredBalancesModel, "tokensKey", root.swapFormData.fromTokensKey, "balances")
|
|
|
|
let accountBalance = ModelUtils.getByKey(balancesModel, "account", address)
|
2024-06-25 11:10:46 +00:00
|
|
|
if(accountBalance && accountBalance.balance !== "0") {
|
|
|
|
accountBalance.formattedBalance = root.formatCurrencyAmountFromBigInt(accountBalance.balance, root.fromToken.symbol, root.fromToken.decimals)
|
2024-06-06 14:05:31 +00:00
|
|
|
return accountBalance
|
2024-05-28 13:19:46 +00:00
|
|
|
}
|
2024-06-07 12:27:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
balance: "0",
|
|
|
|
iconUrl: network.iconUrl,
|
|
|
|
chainColor: network.chainColor,
|
2024-06-25 11:10:46 +00:00
|
|
|
formattedBalance: "0 %1".arg(root.fromToken.symbol)
|
2024-06-07 12:27:56 +00:00
|
|
|
}
|
2024-06-06 14:05:31 +00:00
|
|
|
}
|
2024-07-19 03:36:36 +00:00
|
|
|
|
|
|
|
// Properties to handle error states
|
|
|
|
readonly property bool isRouteEthBalanceInsufficient: root.validSwapProposalReceived && root.swapOutputData.errCode === Constants.swap.errorCodes.errNotEnoughNativeBalance
|
|
|
|
|
|
|
|
readonly property bool isRouteTokenBalanceInsufficient: root.validSwapProposalReceived && root.swapOutputData.errCode === Constants.swap.errorCodes.errNotEnoughTokenBalance
|
|
|
|
|
|
|
|
readonly property bool isTokenBalanceInsufficient: {
|
2024-07-24 20:10:36 +00:00
|
|
|
if (!!root.fromToken && !!root.fromToken.symbol) {
|
|
|
|
return (root.amountEnteredGreaterThanBalance || isRouteTokenBalanceInsufficient) &&
|
|
|
|
root.fromToken.symbol !== Constants.ethToken
|
|
|
|
}
|
|
|
|
return false
|
2024-07-19 03:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readonly property bool isEthBalanceInsufficient: {
|
2024-07-24 20:10:36 +00:00
|
|
|
if (!!root.fromToken && !!root.fromToken.symbol) {
|
|
|
|
return (root.amountEnteredGreaterThanBalance && root.fromToken.symbol === Constants.ethToken) ||
|
|
|
|
isRouteEthBalanceInsufficient
|
|
|
|
}
|
|
|
|
return false
|
2024-07-19 03:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readonly property bool isBalanceInsufficientForSwap: {
|
2024-07-24 20:10:36 +00:00
|
|
|
if (!!root.fromToken && !!root.fromToken.symbol) {
|
|
|
|
return (root.amountEnteredGreaterThanBalance && root.fromToken.symbol === Constants.ethToken) ||
|
|
|
|
(isTokenBalanceInsufficient && root.fromToken.symbol !== Constants.ethToken)
|
|
|
|
}
|
|
|
|
return false
|
2024-07-19 03:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readonly property bool isBalanceInsufficientForFees: !isBalanceInsufficientForSwap && isEthBalanceInsufficient
|
|
|
|
|
|
|
|
property string errorMessage: {
|
|
|
|
if (isBalanceInsufficientForSwap) {
|
|
|
|
return qsTr("Insufficient funds for swap")
|
|
|
|
} else if (isBalanceInsufficientForFees) {
|
|
|
|
return qsTr("Insufficient funds to pay gas fees")
|
|
|
|
} else if (root.swapOutputData.hasError) {
|
|
|
|
switch (root.swapOutputData.errCode) {
|
|
|
|
case Constants.swap.errorCodes.errPriceTimeout:
|
|
|
|
return qsTr("Fetching the price took longer than expected. Please, try again later.")
|
|
|
|
case Constants.swap.errorCodes.errNotEnoughLiquidity:
|
|
|
|
return qsTr("Not enough liquidity. Lower token amount or try again later.")
|
|
|
|
}
|
|
|
|
return qsTr("Something went wrong. Change amount, token or try again later.")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
2024-05-28 13:19:46 +00:00
|
|
|
|
2024-06-24 13:52:10 +00:00
|
|
|
ModelEntry {
|
|
|
|
id: fromTokenEntry
|
|
|
|
sourceModel: root.walletAssetsStore.walletTokensStore.plainTokensBySymbolModel
|
|
|
|
key: "key"
|
|
|
|
value: root.swapFormData.fromTokensKey
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelEntry {
|
|
|
|
id: toTokenEntry
|
|
|
|
sourceModel: root.walletAssetsStore.walletTokensStore.plainTokensBySymbolModel
|
|
|
|
key: "key"
|
|
|
|
value: root.swapFormData.toTokenKey
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelEntry {
|
|
|
|
id: selectedAccountEntry
|
|
|
|
sourceModel: root.nonWatchAccounts
|
|
|
|
key: "address"
|
|
|
|
value: root.swapFormData.selectedAccountAddress
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
Connections {
|
|
|
|
target: root.swapStore
|
2024-07-19 03:36:36 +00:00
|
|
|
function onSuggestedRoutesReady(txRoutes, errCode, errDescription) {
|
2024-06-30 22:08:08 +00:00
|
|
|
if (txRoutes.uuid !== d.uuid) {
|
|
|
|
// Suggested routes for a different fetch, ignore
|
|
|
|
return
|
|
|
|
}
|
2024-06-06 14:05:31 +00:00
|
|
|
root.swapOutputData.reset()
|
|
|
|
root.validSwapProposalReceived = false
|
|
|
|
root.swapProposalLoading = false
|
|
|
|
root.swapOutputData.rawPaths = txRoutes.rawPaths
|
2024-07-19 03:36:36 +00:00
|
|
|
root.swapOutputData.errCode = errCode
|
|
|
|
root.swapOutputData.errDescription = errDescription
|
2024-06-06 14:05:31 +00:00
|
|
|
// if valid route was found
|
2024-07-19 03:36:36 +00:00
|
|
|
if(txRoutes.suggestedRoutes.count > 0) {
|
2024-06-06 14:05:31 +00:00
|
|
|
root.validSwapProposalReceived = true
|
2024-06-18 22:51:49 +00:00
|
|
|
root.swapOutputData.toTokenAmount = AmountsArithmetic.div(AmountsArithmetic.fromString(txRoutes.amountToReceive), AmountsArithmetic.fromNumber(1, root.toToken.decimals)).toString()
|
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
let gasTimeEstimate = txRoutes.gasTimeEstimate
|
|
|
|
let totalTokenFeesInFiat = 0
|
2024-06-18 22:51:49 +00:00
|
|
|
if (!!root.fromToken && !!root.fromToken.marketDetails && !!root.fromToken.marketDetails.currencyPrice)
|
2024-06-06 14:05:31 +00:00
|
|
|
totalTokenFeesInFiat = gasTimeEstimate.totalTokenFees * root.fromToken.marketDetails.currencyPrice.amount
|
|
|
|
root.swapOutputData.totalFees = root.currencyStore.getFiatValue(gasTimeEstimate.totalFeesInEth, Constants.ethToken) + totalTokenFeesInFiat
|
2024-07-03 22:08:03 +00:00
|
|
|
let bestPath = ModelUtils.get(txRoutes.suggestedRoutes, 0, "route")
|
2024-06-24 13:52:10 +00:00
|
|
|
root.swapOutputData.approvalNeeded = !!bestPath ? bestPath.approvalRequired: false
|
|
|
|
root.swapOutputData.approvalGasFees = !!bestPath ? bestPath.approvalGasFees.toString() : ""
|
|
|
|
root.swapOutputData.approvalAmountRequired = !!bestPath ? bestPath.approvalAmountRequired: ""
|
|
|
|
root.swapOutputData.approvalContractAddress = !!bestPath ? bestPath.approvalContractAddress: ""
|
2024-07-19 03:36:36 +00:00
|
|
|
root.swapOutputData.estimatedTime = !!bestPath ? bestPath.estimatedTime: Constants.TransactionEstimatedTime.Unknown
|
2024-06-24 13:52:10 +00:00
|
|
|
root.swapOutputData.txProviderName = !!bestPath ? bestPath.bridgeName: ""
|
2024-07-19 03:36:36 +00:00
|
|
|
} else {
|
2024-06-06 14:05:31 +00:00
|
|
|
root.swapOutputData.hasError = true
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
2024-07-19 03:36:36 +00:00
|
|
|
root.swapOutputData.hasError = root.swapOutputData.hasError || root.swapOutputData.errCode !== ""
|
2024-07-03 22:08:03 +00:00
|
|
|
root.suggestedRoutesReady()
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
2024-06-24 13:52:10 +00:00
|
|
|
|
|
|
|
function onTransactionSent(chainId, txHash, uuid, error) {
|
|
|
|
if(root.swapOutputData.approvalNeeded) {
|
|
|
|
if (uuid !== d.uuid || !!error) {
|
|
|
|
root.approvalPending = false
|
|
|
|
root.approvalSuccessful = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
root.approvalPending = true
|
|
|
|
d.txHash = txHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTransactionSendingComplete(txHash, success) {
|
|
|
|
if(d.txHash === txHash && root.swapOutputData.approvalNeeded && root.approvalPending) {
|
|
|
|
root.approvalPending = false
|
|
|
|
root.approvalSuccessful = success
|
|
|
|
d.txHash = ""
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
function reset() {
|
|
|
|
root.swapFormData.resetFormData()
|
|
|
|
root.swapOutputData.reset()
|
|
|
|
root.validSwapProposalReceived = false
|
|
|
|
root.swapProposalLoading = false
|
2024-06-24 13:52:10 +00:00
|
|
|
root.approvalPending = false
|
|
|
|
root.approvalSuccessful = false
|
|
|
|
d.txHash = ""
|
2024-06-06 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatCurrencyAmount(balance, symbol, options = null, locale = null) {
|
|
|
|
return root.currencyStore.formatCurrencyAmount(balance, symbol, options, locale)
|
|
|
|
}
|
2024-06-10 12:51:33 +00:00
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
function formatCurrencyAmountFromBigInt(balance, symbol, decimals, options = null) {
|
|
|
|
return root.currencyStore.formatCurrencyAmountFromBigInt(balance, symbol, decimals, options)
|
|
|
|
}
|
2024-06-10 12:51:33 +00:00
|
|
|
|
2024-06-06 14:05:31 +00:00
|
|
|
function getDisabledChainIds(enabledChainId) {
|
|
|
|
let disabledChainIds = []
|
|
|
|
let chainIds = ModelUtils.modelToFlatArray(root.filteredFlatNetworksModel, "chainId")
|
|
|
|
for (let i = 0; i < chainIds.length; i++) {
|
|
|
|
if (chainIds[i] !== enabledChainId) {
|
|
|
|
disabledChainIds.push(chainIds[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disabledChainIds.join(":")
|
|
|
|
}
|
|
|
|
|
2024-06-25 23:03:19 +00:00
|
|
|
function fetchSuggestedRoutes(cryptoValueInRaw) {
|
2024-07-03 22:08:03 +00:00
|
|
|
root.swapFormData.toTokenAmount = ""
|
2024-06-25 23:03:19 +00:00
|
|
|
if (root.swapFormData.isFormFilledCorrectly() && !!cryptoValueInRaw) {
|
2024-06-06 14:05:31 +00:00
|
|
|
// Identify new swap with a different uuid
|
|
|
|
d.uuid = Utils.uuid()
|
|
|
|
|
2024-06-30 22:08:08 +00:00
|
|
|
root.swapProposalLoading = true
|
|
|
|
|
2024-07-10 18:35:24 +00:00
|
|
|
let accountAddress = root.swapFormData.selectedAccountAddress
|
2024-06-06 14:05:31 +00:00
|
|
|
let disabledChainIds = getDisabledChainIds(root.swapFormData.selectedNetworkChainId)
|
|
|
|
|
2024-06-30 22:08:08 +00:00
|
|
|
root.swapStore.fetchSuggestedRoutes(d.uuid, accountAddress, accountAddress,
|
2024-06-25 23:03:19 +00:00
|
|
|
cryptoValueInRaw, "0", root.swapFormData.fromTokensKey, root.swapFormData.toTokenKey,
|
|
|
|
disabledChainIds, disabledChainIds, Constants.SendType.Swap, "")
|
2024-06-06 14:05:31 +00:00
|
|
|
} else {
|
|
|
|
root.swapProposalLoading = false
|
2024-07-03 22:08:03 +00:00
|
|
|
root.swapOutputData.reset()
|
2024-06-06 14:05:31 +00:00
|
|
|
}
|
2024-06-10 12:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendApproveTx() {
|
2024-06-24 13:52:10 +00:00
|
|
|
root.approvalPending = true
|
2024-07-10 18:35:24 +00:00
|
|
|
const accountAddress = root.swapFormData.selectedAccountAddress
|
2024-06-10 12:51:33 +00:00
|
|
|
|
|
|
|
root.swapStore.authenticateAndTransfer(d.uuid, accountAddress, accountAddress,
|
2024-06-25 23:03:19 +00:00
|
|
|
root.swapFormData.fromTokensKey, root.swapFormData.toTokenKey,
|
2024-06-17 00:18:55 +00:00
|
|
|
Constants.SendType.Approve, "", false, root.swapOutputData.rawPaths, "")
|
2024-06-10 12:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendSwapTx() {
|
2024-07-10 18:35:24 +00:00
|
|
|
const accountAddress = root.swapFormData.selectedAccountAddress
|
2024-06-10 12:51:33 +00:00
|
|
|
|
|
|
|
root.swapStore.authenticateAndTransfer(d.uuid, accountAddress, accountAddress,
|
2024-06-25 23:03:19 +00:00
|
|
|
root.swapFormData.fromTokensKey, root.swapFormData.toTokenKey,
|
2024-06-17 00:18:55 +00:00
|
|
|
Constants.SendType.Swap, "", false, root.swapOutputData.rawPaths, root.swapFormData.selectedSlippage)
|
2024-06-10 12:51:33 +00:00
|
|
|
}
|
2024-05-15 21:22:13 +00:00
|
|
|
}
|