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 import AppLayouts.Wallet 1.0 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 required property SwapOutputData swapOutputData // the below 2 properties holds the state of finding a swap proposal property bool validSwapProposalReceived: false property bool swapProposalLoading: false property bool showCommunityTokens // To expose the selected from and to Token from the SwapModal readonly property var fromToken: fromTokenEntry.item readonly property var toToken: toTokenEntry.item readonly property var nonWatchAccounts: SortFilterProxyModel { sourceModel: root.swapStore.accounts filters: ValueFilter { roleName: "walletType" value: Constants.watchWalletType inverted: true } sorters: RoleSorter { roleName: "position"; sortOrder: Qt.AscendingOrder } proxyRoles: [ FastExpressionRole { name: "accountBalance" expression: d.processAccountBalance(model.address) expectedRoles: ["address"] }, FastExpressionRole { name: "fromToken" expression: root.fromToken }, FastExpressionRole { name: "colorizedChainPrefixes" function getChainShortNames(chainIds) { const chainShortNames = root.getNetworkShortNames(chainIds) return WalletUtils.colorizedChainPrefix(chainShortNames) } expression: getChainShortNames(model.preferredSharingChainIds) expectedRoles: ["preferredSharingChainIds"] } ] } readonly property SortFilterProxyModel filteredFlatNetworksModel: SortFilterProxyModel { sourceModel: root.swapStore.flatNetworks filters: ValueFilter { roleName: "isTest"; value: root.swapStore.areTestNetworksEnabled } } // Model prepared to provide filtered and sorted assets as per the advanced Settings in token management readonly property var processedAssetsModel: SortFilterProxyModel { property real displayAssetsBelowBalanceThresholdAmount: root.walletAssetsStore.walletTokensStore.getDisplayAssetsBelowBalanceThresholdDisplayAmount() sourceModel: d.assetsWithFilteredBalances proxyRoles: [ FastExpressionRole { name: "currentBalance" expression: { // FIXME recalc when selectedNetworkChainId changes root.swapFormData.selectedNetworkChainId return d.getTotalBalance(model.balances, model.decimals) } expectedRoles: ["balances", "decimals"] }, FastExpressionRole { name: "currentCurrencyBalance" expression: { if (!!model.marketDetails) { return model.currentBalance * model.marketDetails.currencyPrice.amount } return 0 } expectedRoles: ["marketDetails", "currentBalance"] } ] filters: [ FastExpressionFilter { expression: { root.walletAssetsStore.assetsController.revision if (!root.walletAssetsStore.assetsController.filterAcceptsSymbol(model.symbol)) // explicitely hidden return false if (!!model.communityId) return root.showCommunityTokens if (root.walletAssetsStore.walletTokensStore.displayAssetsBelowBalance) return model.currentCurrencyBalance > processedAssetsModel.displayAssetsBelowBalanceThresholdAmount return true } expectedRoles: ["symbol", "communityId", "currentCurrencyBalance"] } ] // FIXME sort by assetsController instead, to have the sorting/order as in the main wallet view } 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 } QtObject { id: d property string uuid // Internal model filtering balances by the account selected in the AccountsModalHeader readonly property SubmodelProxyModel assetsWithFilteredBalances: SubmodelProxyModel { sourceModel: root.walletAssetsStore.groupedAccountAssetsModel submodelRoleName: "balances" delegateModel: SortFilterProxyModel { sourceModel: submodel filters: [ ValueFilter { roleName: "chainId" value: root.swapFormData.selectedNetworkChainId enabled: root.swapFormData.selectedNetworkChainId !== -1 }, ValueFilter { roleName: "account" value: root.swapFormData.selectedAccountAddress } ] } } readonly property SubmodelProxyModel filteredBalancesModel: SubmodelProxyModel { sourceModel: root.walletAssetsStore.baseGroupedAccountAssetModel submodelRoleName: "balances" delegateModel: SortFilterProxyModel { sourceModel: joinModel filters: ValueFilter { roleName: "chainId" value: root.swapFormData.selectedNetworkChainId } readonly property LeftJoinModel joinModel: LeftJoinModel { leftModel: submodel rightModel: root.swapStore.flatNetworks joinRole: "chainId" } } } function processAccountBalance(address) { if (!root.swapFormData.fromTokensKey) { return null } let network = ModelUtils.getByKey(root.filteredFlatNetworksModel, "chainId", root.swapFormData.selectedNetworkChainId) if (!network) { return null } let balancesModel = ModelUtils.getByKey(filteredBalancesModel, "tokensKey", root.swapFormData.fromTokensKey, "balances") let accountBalance = ModelUtils.getByKey(balancesModel, "account", address) if(accountBalance) { let balance = AmountsArithmetic.toNumber(accountBalance.balance, root.fromToken.decimals) let formattedBalance = root.formatCurrencyAmount(balance, root.fromToken.symbol) accountBalance.formattedBalance = formattedBalance return accountBalance } return { balance: "0", iconUrl: network.iconUrl, chainColor: network.chainColor, formattedBalance: root.formatCurrencyAmount(.0 , root.fromToken.symbol) } } /* Internal function to calculate total balance */ function getTotalBalance(balances, decimals, chainIds = [root.swapFormData.selectedNetworkChainId]) { let totalBalance = 0 for(let i=0; i