From 3188250b1d3967376b904b23a27fb3008cbbb701 Mon Sep 17 00:00:00 2001 From: Cuteivist Date: Thu, 7 Mar 2024 14:47:17 +0100 Subject: [PATCH] feat(wallet): Add asset sorting by 1 day fiat change (#13826) --- .../wallet_section/assets/balances_model.nim | 4 ++++ .../wallet_account/dto/account_token_item.nim | 4 +++- .../service/wallet_account/service_token.nim | 10 +++++++-- .../Wallet/controls/SortOrderComboBox.qml | 2 +- ui/imports/shared/views/AssetsView.qml | 21 ++++++++++++------- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app/modules/main/wallet_section/assets/balances_model.nim b/src/app/modules/main/wallet_section/assets/balances_model.nim index d42a891904..6b61146ff6 100644 --- a/src/app/modules/main/wallet_section/assets/balances_model.nim +++ b/src/app/modules/main/wallet_section/assets/balances_model.nim @@ -6,6 +6,7 @@ type ModelRole {.pure.} = enum ChainId = UserRole + 1 Balance + Balance1DayAgo Account QtObject: @@ -39,6 +40,7 @@ QtObject: { ModelRole.ChainId.int:"chainId", ModelRole.Balance.int:"balance", + ModelRole.Balance1DayAgo.int:"balance1DayAgo", ModelRole.Account.int:"account", }.toTable @@ -55,5 +57,7 @@ QtObject: result = newQVariant(item.chainId) of ModelRole.Balance: result = newQVariant(item.balance.toString(10)) + of ModelRole.Balance1DayAgo: + result = newQVariant(item.balance1DayAgo.toString(10)) of ModelRole.Account: result = newQVariant(item.account) diff --git a/src/app_service/service/wallet_account/dto/account_token_item.nim b/src/app_service/service/wallet_account/dto/account_token_item.nim index db14fadb07..dd39d60261 100644 --- a/src/app_service/service/wallet_account/dto/account_token_item.nim +++ b/src/app_service/service/wallet_account/dto/account_token_item.nim @@ -4,12 +4,14 @@ type BalanceItem* = ref object of RootObj account*: string chainId*: int balance*: Uint256 + balance1DayAgo*: Uint256 proc `$`*(self: BalanceItem): string = result = fmt"""BalanceItem[ account: {self.account}, chainId: {self.chainId}, - balance: {self.balance}]""" + balance: {self.balance}, + balance1DayAgo: {self.balance1DayAgo}]""" type GroupedTokenItem* = ref object of RootObj diff --git a/src/app_service/service/wallet_account/service_token.nim b/src/app_service/service/wallet_account/service_token.nim index a9612f7c7e..11617b29cb 100644 --- a/src/app_service/service/wallet_account/service_token.nim +++ b/src/app_service/service/wallet_account/service_token.nim @@ -48,17 +48,23 @@ proc onAllTokensBuilt*(self: Service, response: string) {.slot.} = if not rawBalanceStr.contains("nil"): rawBalance = rawBalanceStr.parse(Uint256) + var balance1DayAgo: Uint256 = u256(0) + let balance1DayAgoStr = balanceObj{"balance1DayAgo"}.getStr + if not balance1DayAgoStr.contains("nil"): + balance1DayAgo = stint.parse(balance1DayAgoStr, UInt256) + let token_by_symbol_key = if communityId.isEmptyOrWhitespace: symbol else: address if groupedAccountsTokensBalances.hasKey(token_by_symbol_key): groupedAccountsTokensBalances[token_by_symbol_key].balancesPerAccount.add(BalanceItem(account: accountAddress, chainId: chainId, - balance: rawBalance)) + balance: rawBalance, + balance1DayAgo: balance1DayAgo)) else: groupedAccountsTokensBalances[token_by_symbol_key] = GroupedTokenItem( tokensKey: token_by_symbol_key, symbol: symbol, - balancesPerAccount: @[BalanceItem(account: accountAddress, chainId: chainId, balance: rawBalance)] + balancesPerAccount: @[BalanceItem(account: accountAddress, chainId: chainId, balance: rawBalance, balance1DayAgo: balance1DayAgo)] ) # set assetsLoading to false once the tokens are loaded diff --git a/ui/app/AppLayouts/Wallet/controls/SortOrderComboBox.qml b/ui/app/AppLayouts/Wallet/controls/SortOrderComboBox.qml index 383f9d1f02..c452de6ebf 100644 --- a/ui/app/AppLayouts/Wallet/controls/SortOrderComboBox.qml +++ b/ui/app/AppLayouts/Wallet/controls/SortOrderComboBox.qml @@ -51,7 +51,7 @@ ComboBox { TokenOrderCurrencyBalance, // FIAT value of asset balance (enabledNetworkCurrencyBalance) TokenOrderBalance, // Number of tokens (enabledNetworkBalance) TokenOrderCurrencyPrice, // Value per token in FIAT (currencyPrice) - TokenOrder1WChange, // Level of change in asset balance value (in FIAT) comp. to 7 days earlier + TokenOrder1DChange, // Level of change in asset balance value (in FIAT) comp. to 1 day earlier TokenOrderAlpha, // Alphabetic by asset name (name) TokenOrderDateAdded, // Date added descending (newest first) TokenOrderGroupName, // Collection or Community name diff --git a/ui/imports/shared/views/AssetsView.qml b/ui/imports/shared/views/AssetsView.qml index 29f265f263..c60f9369c5 100644 --- a/ui/imports/shared/views/AssetsView.qml +++ b/ui/imports/shared/views/AssetsView.qml @@ -62,7 +62,7 @@ ColumnLayout { return true // TODO handle UI threshold (#12611) } - function getTotalBalance(balances, decimals) { + function getTotalBalance(balances, decimals, key) { let totalBalance = 0 let nwFilters = root.networkFilters.split(":") let addrFilters = root.addressFilters.split(":") @@ -70,7 +70,7 @@ ColumnLayout { let balancePerAddressPerChain = ModelUtils.get(balances, i) if (nwFilters.includes(balancePerAddressPerChain.chainId+"") && addrFilters.includes(balancePerAddressPerChain.account)) { - totalBalance+=SQUtils.AmountsArithmetic.toNumber(balancePerAddressPerChain.balance, decimals) + totalBalance+=SQUtils.AmountsArithmetic.toNumber(balancePerAddressPerChain[key], decimals) } } return totalBalance @@ -81,7 +81,7 @@ ColumnLayout { proxyRoles: [ FastExpressionRole { name: "currentBalance" - expression: d.getTotalBalance(model.balances, model.decimals, root.addressFilters, root.networkFilters) + expression: d.getTotalBalance(model.balances, model.decimals, "balance") expectedRoles: ["balances", "decimals"] }, FastExpressionRole { @@ -105,9 +105,16 @@ ColumnLayout { expectedRoles: ["marketDetails"] }, FastExpressionRole { - name: "changePct24hour" - expression: model.marketDetails.changePct24hour - expectedRoles: ["marketDetails"] + name: "change1DayFiat" + expression: { + if (!model.isCommunityAsset && !!model.marketDetails) { + const balance1DayAgo = d.getTotalBalance(model.balances, model.decimals, "balance1DayAgo") + const change = (model.currentBalance * model.marketDetails.currencyPrice.amount) - (balance1DayAgo * (model.marketDetails.currencyPrice.amount - model.marketDetails.change24hour)) + return change + } + return 0 + } + expectedRoles: ["marketDetails", "balances", "decimals", "currentBalance", "isCommunityAsset"] }, FastExpressionRole { name: "isCommunityAsset" @@ -192,7 +199,7 @@ ColumnLayout { { value: SortOrderComboBox.TokenOrderCurrencyBalance, text: qsTr("Asset balance value"), icon: "token-sale", sortRoleName: "currentCurrencyBalance" }, // custom SFPM ExpressionRole on "enabledNetworkCurrencyBalance" amount { value: SortOrderComboBox.TokenOrderBalance, text: qsTr("Asset balance"), icon: "channel", sortRoleName: "currentBalance" }, // custom SFPM ExpressionRole on "enabledNetworkBalance" amount { value: SortOrderComboBox.TokenOrderCurrencyPrice, text: qsTr("Asset value"), icon: "token", sortRoleName: "tokenPrice" }, // custom SFPM ExpressionRole on "currencyPrice" amount - { value: SortOrderComboBox.TokenOrder1WChange, text: qsTr("1d change: balance value"), icon: "history", sortRoleName: "changePct24hour" }, // FIXME changePct1week role missing in backend!!! + { value: SortOrderComboBox.TokenOrder1DChange, text: qsTr("1d change: balance value"), icon: "history", sortRoleName: "change1DayFiat" }, // custom SFPM ExpressionRole { value: SortOrderComboBox.TokenOrderAlpha, text: qsTr("Asset name"), icon: "bold", sortRoleName: "name" }, { value: SortOrderComboBox.TokenOrderCustom, text: qsTr("Custom order"), icon: "exchange", sortRoleName: "" }, { value: SortOrderComboBox.TokenOrderNone, text: "---", icon: "", sortRoleName: "" }, // separator