From 20102ebe3b3d0d8fe36f225cd5e33bcc95441ad8 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Thu, 27 Apr 2023 12:32:44 +0200 Subject: [PATCH] chore(@desktop/wallet): wallet improvements --- .../current_account/module.nim | 6 +- .../browser_section/current_account/view.nim | 14 +-- .../profile_section/wallet/accounts/item.nim | 68 +++------- .../profile_section/wallet/accounts/model.nim | 18 +-- .../wallet/accounts/module.nim | 7 +- .../profile_section/wallet/accounts/utils.nim | 34 ----- .../main/wallet_section/accounts/item.nim | 76 ++++-------- .../main/wallet_section/accounts/model.nim | 18 +-- .../main/wallet_section/accounts/module.nim | 5 +- .../main/wallet_section/accounts/utils.nim | 25 ---- .../main/wallet_section/assets/module.nim | 7 +- .../main/wallet_section/assets/utils.nim | 10 -- .../main/wallet_section/controller.nim | 2 +- .../main/wallet_section/overview/module.nim | 2 +- .../main/wallet_section/send/account_item.nim | 65 ++++------ .../wallet_section/send/accounts_model.nim | 14 +-- .../main/wallet_section/send/controller.nim | 2 +- .../main/wallet_section/send/module.nim | 5 +- .../main/wallet_section/send/utils.nim | 30 ----- .../modules/main/wallet_section/send/view.nim | 4 +- .../wallet_section/transactions/utils.nim | 2 +- src/app/modules/shared/wallet_utils.nim | 116 ++++++++++++++++++ .../modules/shared_models/balance_utils.nim | 11 -- .../shared_models/currency_amount_utils.nim | 10 -- src/app/modules/shared_models/token_utils.nim | 42 ------- .../shared_models/wallet_account_item.nim | 88 +++++++++++++ 26 files changed, 321 insertions(+), 360 deletions(-) delete mode 100644 src/app/modules/main/profile_section/wallet/accounts/utils.nim delete mode 100644 src/app/modules/main/wallet_section/accounts/utils.nim delete mode 100644 src/app/modules/main/wallet_section/assets/utils.nim delete mode 100644 src/app/modules/main/wallet_section/send/utils.nim create mode 100644 src/app/modules/shared/wallet_utils.nim delete mode 100644 src/app/modules/shared_models/balance_utils.nim delete mode 100644 src/app/modules/shared_models/currency_amount_utils.nim delete mode 100644 src/app/modules/shared_models/token_utils.nim create mode 100644 src/app/modules/shared_models/wallet_account_item.nim diff --git a/src/app/modules/main/browser_section/current_account/module.nim b/src/app/modules/main/browser_section/current_account/module.nim index 1e575cda3e..9b1642a049 100644 --- a/src/app/modules/main/browser_section/current_account/module.nim +++ b/src/app/modules/main/browser_section/current_account/module.nim @@ -6,10 +6,8 @@ import ../../../../../app_service/service/wallet_account/service as wallet_accou import ../../../../../app_service/service/network/service as network_service import ../../../../../app_service/service/token/service as token_service import ../../../../../app_service/service/currency/service as currency_service +import ../../../shared/wallet_utils import ../../../shared_models/token_model as token_model -import ../../../shared_models/token_utils - -import ../../wallet_section/accounts/utils import ./io_interface, ./view, ./controller import ../io_interface as delegate_interface @@ -68,7 +66,7 @@ proc switchAccount*(self: Module, accountIndex: int) = let currencyFormat = self.controller.getCurrencyFormat(currency) - let accountItem = walletAccountToItem( + let accountItem = walletAccountToWalletAccountsItem( walletAccount, enabledChainIds, currency, diff --git a/src/app/modules/main/browser_section/current_account/view.nim b/src/app/modules/main/browser_section/current_account/view.nim index 1b9c0e1def..85f332298f 100644 --- a/src/app/modules/main/browser_section/current_account/view.nim +++ b/src/app/modules/main/browser_section/current_account/view.nim @@ -121,19 +121,19 @@ QtObject: return self.assets.hasGas(chainId, nativeGasSymbol, requiredGas) proc setData*(self: View, item: account_item.Item) = - self.name = item.getName() + self.name = item.name() self.nameChanged() - self.address = item.getAddress() + self.address = item.address() self.addressChanged() - self.path = item.getPath() + self.path = item.path() self.pathChanged() - self.color = item.getColor() + self.color = item.color() self.colorChanged() - self.walletType = item.getWalletType() + self.walletType = item.walletType() self.walletTypeChanged() - self.currencyBalance = item.getCurrencyBalance() + self.currencyBalance = item.currencyBalance() self.currencyBalanceChanged() - self.emoji = item.getEmoji() + self.emoji = item.emoji() self.emojiChanged() proc isAddressCurrentAccount*(self: View, address: string): bool = diff --git a/src/app/modules/main/profile_section/wallet/accounts/item.nim b/src/app/modules/main/profile_section/wallet/accounts/item.nim index a841df2575..596e3724d7 100644 --- a/src/app/modules/main/profile_section/wallet/accounts/item.nim +++ b/src/app/modules/main/profile_section/wallet/accounts/item.nim @@ -1,16 +1,12 @@ import strformat +import ../../../../shared_models/wallet_account_item import ./related_accounts_model as related_accounts_model +export wallet_account_item + type - Item* = object - name*: string - address: string - color*: string - emoji*: string - walletType: string - path: string + Item* = ref object of WalletAccountItem relatedAccounts: related_accounts_model.Model - keyUid: string proc initItem*( name: string = "", @@ -22,47 +18,21 @@ proc initItem*( relatedAccounts: related_accounts_model.Model = nil, keyUid: string = "", ): Item = - result.name = name - result.address = address - result.path = path - result.color = color - result.walletType = walletType - result.emoji = emoji + result = Item() + result.WalletAccountItem.setup(name, + address, + color, + emoji, + walletType, + path, + keyUid) result.relatedAccounts = relatedAccounts - result.keyUid = keyUid - + proc `$`*(self: Item): string = - result = fmt"""WalletAccountItem( - name: {self.name}, - address: {self.address}, - path: {self.path}, - color: {self.color}, - walletType: {self.walletType}, - emoji: {self.emoji}, - relatedAccounts: {self.relatedAccounts} - keyUid: {self.keyUid}, - ]""" + result = "ProfileSection-Accounts-Item(" + result = result & $self.WalletAccountItem + result = result & "\nrelatedAccounts: " & $self.relatedAccounts + result = result & ")" -proc getName*(self: Item): string = - return self.name - -proc getAddress*(self: Item): string = - return self.address - -proc getPath*(self: Item): string = - return self.path - -proc getEmoji*(self: Item): string = - return self.emoji - -proc getColor*(self: Item): string = - return self.color - -proc getWalletType*(self: Item): string = - return self.walletType - -proc getRelatedAccounts*(self: Item): related_accounts_model.Model = - return self.relatedAccounts - -proc getKeyUid*(self: Item): string = - return self.keyUid \ No newline at end of file +proc relatedAccounts*(self: Item): related_accounts_model.Model = + return self.relatedAccounts \ No newline at end of file diff --git a/src/app/modules/main/profile_section/wallet/accounts/model.nim b/src/app/modules/main/profile_section/wallet/accounts/model.nim index b53b34c5af..215145db43 100644 --- a/src/app/modules/main/profile_section/wallet/accounts/model.nim +++ b/src/app/modules/main/profile_section/wallet/accounts/model.nim @@ -66,7 +66,7 @@ QtObject: proc onUpdatedAccount*(self: Model, account: Item) = var i = 0 for item in self.items.mitems: - if account.getAddress() == item.getAddress(): + if account.address == item.address: item.name = account.name item.color = account.color item.emoji = account.emoji @@ -89,18 +89,18 @@ QtObject: case enumRole: of ModelRole.Name: - result = newQVariant(item.getName()) + result = newQVariant(item.name()) of ModelRole.Address: - result = newQVariant(item.getAddress()) + result = newQVariant(item.address()) of ModelRole.Path: - result = newQVariant(item.getPath()) + result = newQVariant(item.path()) of ModelRole.Color: - result = newQVariant(item.getColor()) + result = newQVariant(item.color()) of ModelRole.WalletType: - result = newQVariant(item.getWalletType()) + result = newQVariant(item.walletType()) of ModelRole.Emoji: - result = newQVariant(item.getEmoji()) + result = newQVariant(item.emoji()) of ModelRole.RelatedAccounts: - result = newQVariant(item.getRelatedAccounts()) + result = newQVariant(item.relatedAccounts()) of ModelRole.KeyUid: - result = newQVariant(item.getKeyUid()) \ No newline at end of file + result = newQVariant(item.keyUid()) \ No newline at end of file diff --git a/src/app/modules/main/profile_section/wallet/accounts/module.nim b/src/app/modules/main/profile_section/wallet/accounts/module.nim index 354493ece6..5a6fc74383 100644 --- a/src/app/modules/main/profile_section/wallet/accounts/module.nim +++ b/src/app/modules/main/profile_section/wallet/accounts/module.nim @@ -1,7 +1,8 @@ import NimQml, sequtils, sugar, chronicles -import ./io_interface, ./view, ./item, ./controller, ./utils +import ./io_interface, ./view, ./item, ./controller import ../io_interface as delegate_interface +import ../../../../shared/wallet_utils import ../../../../../global/global_singleton import ../../../../../core/eventemitter import ../../../../../../app_service/service/keycard/service as keycard_service @@ -63,7 +64,7 @@ method refreshWalletAccounts*(self: Module) = let walletAccounts = self.controller.getWalletAccounts() let items = walletAccounts.map(w => (block: - walletAccountToItem(w) + walletAccountToWalletSettingsAccountsItem(w) )) self.view.setItems(items) @@ -77,7 +78,7 @@ method load*(self: Module) = self.events.on(SIGNAL_WALLET_ACCOUNT_UPDATED) do(e:Args): let args = WalletAccountUpdated(e) - self.view.onUpdatedAccount(walletAccountToItem(args.account)) + self.view.onUpdatedAccount(walletAccountToWalletSettingsAccountsItem(args.account)) self.events.on(SIGNAL_NEW_KEYCARD_SET) do(e: Args): let args = KeycardActivityArgs(e) diff --git a/src/app/modules/main/profile_section/wallet/accounts/utils.nim b/src/app/modules/main/profile_section/wallet/accounts/utils.nim deleted file mode 100644 index c2f5ae3514..0000000000 --- a/src/app/modules/main/profile_section/wallet/accounts/utils.nim +++ /dev/null @@ -1,34 +0,0 @@ -import tables, sequtils, sugar -import ../../../../../../app_service/service/wallet_account/service as wallet_account_service -import ./item -import ./related_account_item as related_account_item -import ./related_accounts_model as related_accounts_model - -proc walletAccountToRelatedAccountItem*(w: WalletAccountDto) : related_account_item.Item = - return related_account_item.initItem( - w.name, - w.color, - w.emoji, - ) - -proc walletAccountToItem*( - w: WalletAccountDto, -) : item.Item = - let relatedAccounts = related_accounts_model.newModel() - if w.isNil: - return item.initItem() - - relatedAccounts.setItems( - w.relatedAccounts.map(x => walletAccountToRelatedAccountItem(x)) - ) - - return item.initItem( - w.name, - w.address, - w.path, - w.color, - w.walletType, - w.emoji, - relatedAccounts, - w.keyUid, - ) diff --git a/src/app/modules/main/wallet_section/accounts/item.nim b/src/app/modules/main/wallet_section/accounts/item.nim index c2f895c95c..4bc0a5906b 100644 --- a/src/app/modules/main/wallet_section/accounts/item.nim +++ b/src/app/modules/main/wallet_section/accounts/item.nim @@ -1,17 +1,13 @@ import strformat +import ../../../shared_models/wallet_account_item import ../../../shared_models/currency_amount +export wallet_account_item + type - Item* = object - name: string - address: string - path: string - color: string - walletType: string - currencyBalance: CurrencyAmount - emoji: string - keyUid: string + Item* = ref object of WalletAccountItem assetsLoading: bool + currencyBalance: CurrencyAmount proc initItem*( name: string = "", @@ -24,52 +20,26 @@ proc initItem*( keyUid: string = "", assetsLoading: bool = true, ): Item = - result.name = name - result.address = address - result.path = path - result.color = color - result.walletType = walletType - result.currencyBalance = currencyBalance - result.emoji = emoji - result.keyUid = keyUid + result = Item() + result.WalletAccountItem.setup(name, + address, + color, + emoji, + walletType, + path, + keyUid) result.assetsLoading = assetsLoading - + result.currencyBalance = currencyBalance + proc `$`*(self: Item): string = - result = fmt"""WalletAccountItem( - name: {self.name}, - address: {self.address}, - path: {self.path}, - color: {self.color}, - walletType: {self.walletType}, - currencyBalance: {self.currencyBalance}, - emoji: {self.emoji}, - keyUid: {self.keyUid}, - assetsLoading: {self.assetsLoading}, - ]""" - -proc getName*(self: Item): string = - return self.name - -proc getAddress*(self: Item): string = - return self.address - -proc getPath*(self: Item): string = - return self.path - -proc getEmoji*(self: Item): string = - return self.emoji - -proc getColor*(self: Item): string = - return self.color - -proc getWalletType*(self: Item): string = - return self.walletType - -proc getCurrencyBalance*(self: Item): CurrencyAmount = + result = "WalletSection-Accounts-Item(" + result = result & $self.WalletAccountItem + result = result & "\nassetsLoading: " & $self.assetsLoading + result = result & "\ncurrencyBalance: " & $self.currencyBalance + result = result & ")" + +proc currencyBalance*(self: Item): CurrencyAmount = return self.currencyBalance -proc getKeyUid*(self: Item): string = - return self.keyUid - -proc getAssetsLoading*(self: Item): bool = +proc assetsLoading*(self: Item): bool = return self.assetsLoading diff --git a/src/app/modules/main/wallet_section/accounts/model.nim b/src/app/modules/main/wallet_section/accounts/model.nim index 1a347929cd..79d97877d7 100644 --- a/src/app/modules/main/wallet_section/accounts/model.nim +++ b/src/app/modules/main/wallet_section/accounts/model.nim @@ -80,20 +80,20 @@ QtObject: case enumRole: of ModelRole.Name: - result = newQVariant(item.getName()) + result = newQVariant(item.name()) of ModelRole.Address: - result = newQVariant(item.getAddress()) + result = newQVariant(item.address()) of ModelRole.Path: - result = newQVariant(item.getPath()) + result = newQVariant(item.path()) of ModelRole.Color: - result = newQVariant(item.getColor()) + result = newQVariant(item.color()) of ModelRole.WalletType: - result = newQVariant(item.getWalletType()) + result = newQVariant(item.walletType()) of ModelRole.CurrencyBalance: - result = newQVariant(item.getCurrencyBalance()) + result = newQVariant(item.currencyBalance()) of ModelRole.Emoji: - result = newQVariant(item.getEmoji()) + result = newQVariant(item.emoji()) of ModelRole.KeyUid: - result = newQVariant(item.getKeyUid()) + result = newQVariant(item.keyUid()) of ModelRole.AssetsLoading: - result = newQVariant(item.getAssetsLoading()) + result = newQVariant(item.assetsLoading()) diff --git a/src/app/modules/main/wallet_section/accounts/module.nim b/src/app/modules/main/wallet_section/accounts/module.nim index c0895b25ec..41c1353973 100644 --- a/src/app/modules/main/wallet_section/accounts/module.nim +++ b/src/app/modules/main/wallet_section/accounts/module.nim @@ -1,6 +1,6 @@ import tables, NimQml, sequtils, sugar, chronicles -import ./io_interface, ./view, ./item, ./controller, ./utils +import ./io_interface, ./view, ./item, ./controller import ../io_interface as delegate_interface import ../../../../global/global_singleton import ../../../../core/eventemitter @@ -9,6 +9,7 @@ import ../../../../../app_service/service/keycard/service as keycard_service import ../../../../../app_service/service/wallet_account/service as wallet_account_service import ../../../../../app_service/service/network/service as network_service import ../../../../../app_service/service/currency/service as currency_service +import ../../../shared/wallet_utils import ../../../shared_modules/keycard_popup/io_interface as keycard_shared_module export io_interface @@ -64,7 +65,7 @@ method refreshWalletAccounts*(self: Module) = let currencyFormat = self.controller.getCurrencyFormat(currency) let items = walletAccounts.map(w => (block: - walletAccountToItem( + walletAccountToWalletAccountsItem( w, enabledChainIds, currency, diff --git a/src/app/modules/main/wallet_section/accounts/utils.nim b/src/app/modules/main/wallet_section/accounts/utils.nim deleted file mode 100644 index b260a556e1..0000000000 --- a/src/app/modules/main/wallet_section/accounts/utils.nim +++ /dev/null @@ -1,25 +0,0 @@ -import ./item - -import ../../../../../app_service/service/wallet_account/dto -import ../../../../../app_service/service/currency/dto as currency_dto -import ../../../shared_models/currency_amount -import ../../../shared_models/currency_amount_utils - - -proc walletAccountToItem*( - w: WalletAccountDto, - enabledChainIds: seq[int], - currency: string, - currencyFormat: CurrencyFormatDto, - ) : item.Item = - return item.initItem( - w.name, - w.address, - w.path, - w.color, - w.walletType, - currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), - w.emoji, - w.keyUid, - w.assetsLoading, - ) diff --git a/src/app/modules/main/wallet_section/assets/module.nim b/src/app/modules/main/wallet_section/assets/module.nim index b32c1e3d51..bc95217286 100644 --- a/src/app/modules/main/wallet_section/assets/module.nim +++ b/src/app/modules/main/wallet_section/assets/module.nim @@ -9,13 +9,11 @@ import ../../../../../app_service/service/network/service as network_service import ../../../../../app_service/service/network_connection/service as network_connection import ../../../../../app_service/service/collectible/service as collectible_service import ../../../../../app_service/service/node/service as node_service -import ../../../shared_models/currency_amount_utils +import ../../../shared/wallet_utils import ../../../shared_models/currency_amount import ../../../shared_models/token_model as token_model import ../../../shared_models/token_item as token_item -import ../../../shared_models/token_utils import ./item as account_item -import ./utils import ./io_interface, ./view, ./controller import ../io_interface as delegate_interface @@ -129,7 +127,7 @@ method switchAccount*(self: Module, accountIndex: int) = self.currentAccountIndex = 0 walletAccount = self.controller.getWalletAccount(self.currentAccountIndex) - let accountItem = walletAccountToItem(walletAccount) + let accountItem = walletAccountToWalletAssetsItem(walletAccount) self.view.setData(accountItem) if walletAccount.tokens.len == 0 and walletAccount.assetsLoading: @@ -137,7 +135,6 @@ method switchAccount*(self: Module, accountIndex: int) = else: self.setAssetsAndBalance(walletAccount.tokens) - proc onTokensRebuilt(self: Module, accountsTokens: OrderedTable[string, seq[WalletTokenDto]], hasBalanceCache: bool, hasMarketValuesCache: bool) = let walletAccount = self.controller.getWalletAccount(self.currentAccountIndex) if not accountsTokens.contains(walletAccount.address): diff --git a/src/app/modules/main/wallet_section/assets/utils.nim b/src/app/modules/main/wallet_section/assets/utils.nim deleted file mode 100644 index 94f6591f49..0000000000 --- a/src/app/modules/main/wallet_section/assets/utils.nim +++ /dev/null @@ -1,10 +0,0 @@ -import ./item - -import ../../../../../app_service/service/wallet_account/dto - -proc walletAccountToItem*( - w: WalletAccountDto, -) : item.Item = - return item.initItem( - w.assetsLoading, - ) diff --git a/src/app/modules/main/wallet_section/controller.nim b/src/app/modules/main/wallet_section/controller.nim index 310df385cd..c72938bc0f 100644 --- a/src/app/modules/main/wallet_section/controller.nim +++ b/src/app/modules/main/wallet_section/controller.nim @@ -3,8 +3,8 @@ import ../../../../app_service/service/settings/service as settings_service import ../../../../app_service/service/wallet_account/service as wallet_account_service import ../../../../app_service/service/currency/service as currency_service +import ../../shared/wallet_utils import ../../shared_models/currency_amount -import ../../shared_models/currency_amount_utils type Controller* = ref object of RootObj diff --git a/src/app/modules/main/wallet_section/overview/module.nim b/src/app/modules/main/wallet_section/overview/module.nim index 78771267aa..def0f7b6a7 100644 --- a/src/app/modules/main/wallet_section/overview/module.nim +++ b/src/app/modules/main/wallet_section/overview/module.nim @@ -5,7 +5,7 @@ import ../../../../core/eventemitter import ../../../../../app_service/service/currency/service as currency_service import ../../../../../app_service/service/wallet_account/service as wallet_account_service import ../../../../../app_service/service/network/service as network_service -import ../../../shared_models/currency_amount_utils +import ../../../shared/wallet_utils import ../../../shared_models/currency_amount import ./item diff --git a/src/app/modules/main/wallet_section/send/account_item.nim b/src/app/modules/main/wallet_section/send/account_item.nim index ab4cc7f6c2..03dffd00cc 100644 --- a/src/app/modules/main/wallet_section/send/account_item.nim +++ b/src/app/modules/main/wallet_section/send/account_item.nim @@ -1,18 +1,15 @@ import strformat -import ../../../shared_models/token_model as token_model +import ../../../shared_models/wallet_account_item +import ../../../shared_models/token_model import ../../../shared_models/currency_amount +export wallet_account_item + type - AccountItem* = object - name: string - address: string - color: string - walletType: string - emoji: string + AccountItem* = ref object of WalletAccountItem assets: token_model.Model currencyBalance: CurrencyAmount - proc initAccountItem*( name: string = "", address: string = "", @@ -22,42 +19,26 @@ proc initAccountItem*( assets: token_model.Model = nil, currencyBalance: CurrencyAmount = nil, ): AccountItem = - result.name = name - result.address = address - result.color = color - result.walletType = walletType - result.emoji = emoji + result = AccountItem() + result.WalletAccountItem.setup(name, + address, + color, + emoji, + walletType, + path = "", + keyUid = "") result.assets = assets result.currencyBalance = currencyBalance - + proc `$`*(self: AccountItem): string = - result = fmt"""WalletAccountItem( - name: {self.name}, - address: {self.address}, - color: {self.color}, - walletType: {self.walletType}, - emoji: {self.emoji}, - assets: {self.assets}, - currencyBalance: {self.currencyBalance}, - ]""" - -proc getName*(self: AccountItem): string = - return self.name - -proc getAddress*(self: AccountItem): string = - return self.address - -proc getEmoji*(self: AccountItem): string = - return self.emoji - -proc getColor*(self: AccountItem): string = - return self.color - -proc getWalletType*(self: AccountItem): string = - return self.walletType - -proc getAssets*(self: AccountItem): token_model.Model = + result = "WalletSection-Send-Item(" + result = result & $self.WalletAccountItem + result = result & "\nassets: " & $self.assets + result = result & "\ncurrencyBalance: " & $self.currencyBalance + result = result & ")" + +proc assets*(self: AccountItem): token_model.Model = return self.assets -proc getCurrencyBalance*(self: AccountItem): CurrencyAmount = - return self.currencyBalance \ No newline at end of file +proc currencyBalance*(self: AccountItem): CurrencyAmount = + return self.currencyBalance diff --git a/src/app/modules/main/wallet_section/send/accounts_model.nim b/src/app/modules/main/wallet_section/send/accounts_model.nim index fca2992b17..971a016368 100644 --- a/src/app/modules/main/wallet_section/send/accounts_model.nim +++ b/src/app/modules/main/wallet_section/send/accounts_model.nim @@ -75,17 +75,17 @@ QtObject: case enumRole: of ModelRole.Name: - result = newQVariant(item.getName()) + result = newQVariant(item.name()) of ModelRole.Address: - result = newQVariant(item.getAddress()) + result = newQVariant(item.address()) of ModelRole.Color: - result = newQVariant(item.getColor()) + result = newQVariant(item.color()) of ModelRole.WalletType: - result = newQVariant(item.getWalletType()) + result = newQVariant(item.walletType()) of ModelRole.Emoji: - result = newQVariant(item.getEmoji()) + result = newQVariant(item.emoji()) of ModelRole.Assets: - result = newQVariant(item.getAssets()) + result = newQVariant(item.assets()) of ModelRole.CurrencyBalance: - result = newQVariant(item.getCurrencyBalance()) + result = newQVariant(item.currencyBalance()) diff --git a/src/app/modules/main/wallet_section/send/controller.nim b/src/app/modules/main/wallet_section/send/controller.nim index 17b8f93dd1..2a0dd799d1 100644 --- a/src/app/modules/main/wallet_section/send/controller.nim +++ b/src/app/modules/main/wallet_section/send/controller.nim @@ -7,8 +7,8 @@ import ../../../../../app_service/service/currency/service as currency_service import ../../../../../app_service/service/currency/dto as currency_dto import ../../../shared_modules/keycard_popup/io_interface as keycard_shared_module +import ../../../shared/wallet_utils import ../../../shared_models/currency_amount -import ../../../shared_models/currency_amount_utils import ../../../../core/eventemitter diff --git a/src/app/modules/main/wallet_section/send/module.nim b/src/app/modules/main/wallet_section/send/module.nim index 24782b7b7c..876e7fa285 100644 --- a/src/app/modules/main/wallet_section/send/module.nim +++ b/src/app/modules/main/wallet_section/send/module.nim @@ -1,6 +1,6 @@ import tables, NimQml, sequtils, sugar, json, stint -import ./io_interface, ./view, ./controller, ./utils +import ./io_interface, ./view, ./controller import ../io_interface as delegate_interface import ../../../../global/global_singleton import ../../../../core/eventemitter @@ -8,6 +8,7 @@ import ../../../../../app_service/service/wallet_account/service as wallet_accou import ../../../../../app_service/service/network/service as network_service import ../../../../../app_service/service/currency/service as currency_service import ../../../../../app_service/service/transaction/service as transaction_service +import ../../../shared/wallet_utils export io_interface @@ -61,7 +62,7 @@ method refreshWalletAccounts*(self: Module) = let tokenFormats = collect(initTable()): for t in w.tokens: {t.symbol: self.controller.getCurrencyFormat(t.symbol)} - walletAccountToItem( + walletAccountToWalletSendAccountItem( w, chainIds, enabledChainIds, diff --git a/src/app/modules/main/wallet_section/send/utils.nim b/src/app/modules/main/wallet_section/send/utils.nim deleted file mode 100644 index 273e1d3c52..0000000000 --- a/src/app/modules/main/wallet_section/send/utils.nim +++ /dev/null @@ -1,30 +0,0 @@ -import Tables, sequtils, sugar -import ./account_item - -import ../../../../../app_service/service/wallet_account/dto -import ../../../../../app_service/service/currency/dto as currency_dto -import ../../../shared_models/currency_amount_utils -import ../../../shared_models/token_model as token_model -import ../../../shared_models/token_utils - -proc walletAccountToItem*( - w: WalletAccountDto, - chainIds: seq[int], - enabledChainIds: seq[int], - currency: string, - currencyFormat: CurrencyFormatDto, - tokenFormats: Table[string, CurrencyFormatDto], -) : account_item.AccountItem = - let assets = token_model.newModel() - assets.setItems( - w.tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, tokenFormats[t.symbol])) - ) - return account_item.initAccountItem( - w.name, - w.address, - w.color, - w.walletType, - w.emoji, - assets, - currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), - ) diff --git a/src/app/modules/main/wallet_section/send/view.nim b/src/app/modules/main/wallet_section/send/view.nim index aef8c518bc..3e22eea570 100644 --- a/src/app/modules/main/wallet_section/send/view.nim +++ b/src/app/modules/main/wallet_section/send/view.nim @@ -107,7 +107,7 @@ QtObject: proc hasGas*(self: View, address: string, chainId: int, nativeGasSymbol: string, requiredGas: float): bool {.slot.} = for account in self.accounts.items: - if account.getAddress() == address: - return account.getAssets().hasGas(chainId, nativeGasSymbol, requiredGas) + if account.address() == address: + return account.assets().hasGas(chainId, nativeGasSymbol, requiredGas) return false \ No newline at end of file diff --git a/src/app/modules/main/wallet_section/transactions/utils.nim b/src/app/modules/main/wallet_section/transactions/utils.nim index 5ef0d466f2..29c95fac95 100644 --- a/src/app/modules/main/wallet_section/transactions/utils.nim +++ b/src/app/modules/main/wallet_section/transactions/utils.nim @@ -4,8 +4,8 @@ import ../../../../global/global_singleton import ../../../../../app_service/service/transaction/dto import ../../../../../app_service/service/currency/dto as currency_dto import ../../../../../app_service/service/collectible/dto as collectible_dto +import ../../../shared/wallet_utils import ../../../shared_models/currency_amount -import ../../../shared_models/currency_amount_utils import ./item import ./multi_transaction_item diff --git a/src/app/modules/shared/wallet_utils.nim b/src/app/modules/shared/wallet_utils.nim new file mode 100644 index 0000000000..7d2d923cde --- /dev/null +++ b/src/app/modules/shared/wallet_utils.nim @@ -0,0 +1,116 @@ +import tables, sequtils, sugar + +import ../shared_models/[balance_item, currency_amount, token_item, token_model] + +import ../../../app_service/service/wallet_account/service as wallet_account_service +import ../../../app_service/service/currency/dto as currency_dto + +import ../main/wallet_section/accounts/item as wallet_accounts_item +import ../main/wallet_section/assets/item as wallet_assets_item +import ../main/wallet_section/send/account_item as wallet_send_account_item +import ../main/profile_section/wallet/accounts/item as wallet_settings_accounts_item +import ../main/profile_section/wallet/accounts/[related_account_item, related_accounts_model] + +proc currencyAmountToItem*(amount: float64, format: CurrencyFormatDto) : CurrencyAmount = + return newCurrencyAmount( + amount, + format.symbol, + int(format.displayDecimals), + format.stripTrailingZeroes + ) + +proc balanceToItemBalanceItem*(b: BalanceDto, format: CurrencyFormatDto) : balance_item.Item = + return balance_item.initItem( + currencyAmountToItem(b.balance, format), + b.address, + b.chainId + ) + +proc walletAccountToRelatedAccountItem*(w: WalletAccountDto) : related_account_item.Item = + return related_account_item.initItem( + w.name, + w.color, + w.emoji, + ) + +proc walletAccountToWalletSettingsAccountsItem*(w: WalletAccountDto): wallet_settings_accounts_item.Item = + let relatedAccounts = related_accounts_model.newModel() + if w.isNil: + return wallet_settings_accounts_item.initItem() + + relatedAccounts.setItems(w.relatedAccounts.map(x => walletAccountToRelatedAccountItem(x))) + + return wallet_settings_accounts_item.initItem( + w.name, + w.address, + w.path, + w.color, + w.walletType, + w.emoji, + relatedAccounts, + w.keyUid, + ) + +proc walletAccountToWalletAccountsItem*(w: WalletAccountDto, enabledChainIds: seq[int], currency: string, + currencyFormat: CurrencyFormatDto): wallet_accounts_item.Item = + return wallet_accounts_item.initItem( + w.name, + w.address, + w.path, + w.color, + w.walletType, + currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), + w.emoji, + w.keyUid, + w.assetsLoading, + ) + +proc walletAccountToWalletAssetsItem*(w: WalletAccountDto): wallet_assets_item.Item = + return wallet_assets_item.initItem( + w.assetsLoading, + ) + +proc walletTokenToItem*(t: WalletTokenDto, chainIds: seq[int], enabledChainIds: seq[int], currency: string, + currencyFormat: CurrencyFormatDto, tokenFormat: CurrencyFormatDto): token_item.Item = + let marketValues = t.marketValuesPerCurrency.getOrDefault(currency) + return token_item.initItem( + t.name, + t.symbol, + currencyAmountToItem(t.getBalance(chainIds), tokenFormat), + currencyAmountToItem(t.getCurrencyBalance(chainIds, currency), currencyFormat), + currencyAmountToItem(t.getBalance(enabledChainIds), tokenFormat), + currencyAmountToItem(t.getCurrencyBalance(enabledChainIds, currency), currencyFormat), + t.getVisibleForNetwork(enabledChainIds), + t.getVisibleForNetworkWithPositiveBalance(enabledChainIds), + t.getBalances(chainIds).map(b => balanceToItemBalanceItem(b, tokenFormat)), + t.description, + t.assetWebsiteUrl, + t.builtOn, + t.getAddress(), + currencyAmountToItem(marketValues.marketCap, currencyFormat), + currencyAmountToItem(marketValues.highDay, currencyFormat), + currencyAmountToItem(marketValues.lowDay, currencyFormat), + marketValues.changePctHour, + marketValues.changePctDay, + marketValues.changePct24hour, + marketValues.change24hour, + currencyAmountToItem(marketValues.price, currencyFormat), + t.decimals, + loading = false + ) + +proc walletAccountToWalletSendAccountItem*(w: WalletAccountDto, chainIds: seq[int], enabledChainIds: seq[int], currency: string, + currencyFormat: CurrencyFormatDto, tokenFormats: Table[string, CurrencyFormatDto]): wallet_send_account_item.AccountItem = + let assets = token_model.newModel() + assets.setItems( + w.tokens.map(t => walletTokenToItem(t, chainIds, enabledChainIds, currency, currencyFormat, tokenFormats[t.symbol])) + ) + return wallet_send_account_item.initAccountItem( + w.name, + w.address, + w.color, + w.walletType, + w.emoji, + assets, + currencyAmountToItem(w.getCurrencyBalance(enabledChainIds, currency), currencyFormat), + ) \ No newline at end of file diff --git a/src/app/modules/shared_models/balance_utils.nim b/src/app/modules/shared_models/balance_utils.nim deleted file mode 100644 index a6a5eb813e..0000000000 --- a/src/app/modules/shared_models/balance_utils.nim +++ /dev/null @@ -1,11 +0,0 @@ -import ../../../app_service/service/wallet_account/dto -import ../../../app_service/service/currency/dto as currency_dto -import ./currency_amount_utils -import ./balance_item - -proc balanceToItem*(b: BalanceDto, format: CurrencyFormatDto) : Item = - return initItem( - currencyAmountToItem(b.balance, format), - b.address, - b.chainId - ) diff --git a/src/app/modules/shared_models/currency_amount_utils.nim b/src/app/modules/shared_models/currency_amount_utils.nim deleted file mode 100644 index 93345a9c8a..0000000000 --- a/src/app/modules/shared_models/currency_amount_utils.nim +++ /dev/null @@ -1,10 +0,0 @@ -import ../../../app_service/service/currency/dto -import ./currency_amount - -proc currencyAmountToItem*(amount: float64, format: CurrencyFormatDto) : CurrencyAmount = - return newCurrencyAmount( - amount, - format.symbol, - int(format.displayDecimals), - format.stripTrailingZeroes - ) diff --git a/src/app/modules/shared_models/token_utils.nim b/src/app/modules/shared_models/token_utils.nim deleted file mode 100644 index 168cb790b9..0000000000 --- a/src/app/modules/shared_models/token_utils.nim +++ /dev/null @@ -1,42 +0,0 @@ -import tables, sequtils, sugar -import ../../../app_service/service/wallet_account/dto -import ../../../app_service/service/currency/dto as currency_dto -import ./currency_amount_utils -import ./balance_utils -import ./token_item - -proc walletTokenToItem*( - t: WalletTokenDto, - chainIds: seq[int], - enabledChainIds: seq[int], - currency: string, - currencyFormat: CurrencyFormatDto, - tokenFormat: CurrencyFormatDto, - ) : token_item.Item = - let marketValues = t.marketValuesPerCurrency.getOrDefault(currency) - - return token_item.initItem( - t.name, - t.symbol, - currencyAmountToItem(t.getBalance(chainIds), tokenFormat), - currencyAmountToItem(t.getCurrencyBalance(chainIds, currency), currencyFormat), - currencyAmountToItem(t.getBalance(enabledChainIds), tokenFormat), - currencyAmountToItem(t.getCurrencyBalance(enabledChainIds, currency), currencyFormat), - t.getVisibleForNetwork(enabledChainIds), - t.getVisibleForNetworkWithPositiveBalance(enabledChainIds), - t.getBalances(chainIds).map(b => balanceToItem(b, tokenFormat)), - t.description, - t.assetWebsiteUrl, - t.builtOn, - t.getAddress(), - currencyAmountToItem(marketValues.marketCap, currencyFormat), - currencyAmountToItem(marketValues.highDay, currencyFormat), - currencyAmountToItem(marketValues.lowDay, currencyFormat), - marketValues.changePctHour, - marketValues.changePctDay, - marketValues.changePct24hour, - marketValues.change24hour, - currencyAmountToItem(marketValues.price, currencyFormat), - t.decimals, - loading = false - ) diff --git a/src/app/modules/shared_models/wallet_account_item.nim b/src/app/modules/shared_models/wallet_account_item.nim new file mode 100644 index 0000000000..2e208495c1 --- /dev/null +++ b/src/app/modules/shared_models/wallet_account_item.nim @@ -0,0 +1,88 @@ +import strformat + +type + WalletAccountItem* = ref object of RootObj + name: string + address: string + color: string + emoji: string + walletType: string + path: string + keyUid: string + +proc setup*(self: WalletAccountItem, + name: string = "", + address: string = "", + color: string = "", + emoji: string = "", + walletType: string = "", + path: string = "", + keyUid: string = "" + ) = + self.name = name + self.address = address + self.color = color + self.emoji = emoji + self.walletType = walletType + self.path = path + self.keyUid = keyUid + +proc initWalletAccountItem*( + name: string = "", + address: string = "", + color: string = "", + emoji: string = "", + walletType: string = "", + path: string = "", + keyUid: string = "" + ): WalletAccountItem = + result = WalletAccountItem() + result.setup(name, + address, + color, + emoji, + walletType, + path, + keyUid) + + +proc `$`*(self: WalletAccountItem): string = + result = fmt"""WalletAccountItem( + name: {self.name}, + address: {self.address}, + color: {self.color}, + emoji: {self.emoji}, + walletType: {self.walletType}, + path: {self.path}, + keyUid: {self.keyUid}, + ]""" + +proc name*(self: WalletAccountItem): string {.inline.} = + return self.name + +proc `name=`*(self: WalletAccountItem, value: string) {.inline.} = + self.name = value + +proc address*(self: WalletAccountItem): string {.inline.} = + return self.address + +proc emoji*(self: WalletAccountItem): string {.inline.} = + return self.emoji + +proc `emoji=`*(self: WalletAccountItem, value: string) {.inline.} = + self.emoji = value + +proc color*(self: WalletAccountItem): string {.inline.} = + return self.color + +proc `color=`*(self: WalletAccountItem, value: string) {.inline.} = + self.color = value + +proc walletType*(self: WalletAccountItem): string {.inline.} = + return self.walletType + +proc path*(self: WalletAccountItem): string {.inline.} = + return self.path + +proc keyUid*(self: WalletAccountItem): string {.inline.} = + return self.keyUid \ No newline at end of file