From a876a4b9ce6650de93513067e82209b4d425c3da Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 18 Oct 2021 13:00:56 +0200 Subject: [PATCH] refactor(@desktop/wallet): Add wallet account service --- src/app/boot/app_controller.nim | 8 +++- src/app/modules/main/module.nim | 6 ++- .../modules/main/wallet_section/module.nim | 4 +- src/app_service/service/token/dto.nim | 2 +- .../service/wallet_account/dto.nim | 46 +++++++++++++++++++ .../service/wallet_account/service.nim | 36 +++++++++++++++ .../wallet_account/service_interface.nim | 16 +++++++ 7 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 src/app_service/service/wallet_account/dto.nim create mode 100644 src/app_service/service/wallet_account/service.nim create mode 100644 src/app_service/service/wallet_account/service_interface.nim diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 7b6871361f..b418a4f7e1 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -9,6 +9,7 @@ import ../../app_service/service/community/service as community_service import ../../app_service/service/token/service as token_service import ../../app_service/service/transaction/service as transaction_service import ../../app_service/service/collectible/service as collectible_service +import ../../app_service/service/wallet_account/service as wallet_account_service import ../core/local_account_settings @@ -64,6 +65,7 @@ type tokenService: token_service.Service transactionService: transaction_service.Service collectibleService: collectible_service.Service + walletAccountService: wallet_account_service.Service # Core localAccountSettings: LocalAccountSettings @@ -119,6 +121,8 @@ proc newAppController*(appService: AppService): AppController = result.tokenService = token_service.newService() result.transactionService = transaction_service.newService() result.collectibleService = collectible_service.newService() + result.walletAccountService = wallet_account_service.newService() + # Core result.localAccountSettingsVariant = newQVariant( @@ -136,7 +140,8 @@ proc newAppController*(appService: AppService): AppController = result.communityService, result.tokenService, result.transactionService, - result.collectibleService + result.collectibleService, + result.walletAccountService ) ################################################# @@ -197,6 +202,7 @@ proc load*(self: AppController) = self.chatService.init() self.communityService.init() self.tokenService.init() + self.walletAccountService.init() self.mainModule.load() diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index 1dbd70cae2..5a45bdcf1b 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -13,6 +13,7 @@ import ../../../app_service/service/community/service as community_service import ../../../app_service/service/token/service as token_service import ../../../app_service/service/transaction/service as transaction_service import ../../../app_service/service/collectible/service as collectible_service +import ../../../app_service/service/wallet_account/service as wallet_account_service import eventemitter @@ -47,7 +48,8 @@ proc newModule*[T]( communityService: community_service.Service, tokenService: token_service.Service, transactionService: transaction_service.Service, - collectibleService: collectible_service.Service + collectibleService: collectible_service.Service, + walletAccountService: wallet_account_service.Service ): Module[T] = result = Module[T]() result.delegate = delegate @@ -67,7 +69,7 @@ proc newModule*[T]( ) result.walletSectionModule = wallet_section_module.newModule[Module[T]]( - result, tokenService, transactionService, collectible_service + result, tokenService, transactionService, collectible_service, walletAccountService ) diff --git a/src/app/modules/main/wallet_section/module.nim b/src/app/modules/main/wallet_section/module.nim index f3ef008ebb..809986f8f6 100644 --- a/src/app/modules/main/wallet_section/module.nim +++ b/src/app/modules/main/wallet_section/module.nim @@ -12,6 +12,7 @@ import ./transactions/module as transactions_module import ../../../../app_service/service/token/service as token_service import ../../../../app_service/service/transaction/service as transaction_service import ../../../../app_service/service/collectible/service as collectible_service +import ../../../../app_service/service/wallet_account/service as wallet_account_service import io_interface export io_interface @@ -33,7 +34,8 @@ proc newModule*[T]( delegate: T, tokenService: token_service.Service, transactionService: transaction_service.Service, - collectibleService: collectible_service.Service + collectibleService: collectible_service.Service, + walletAccountService: wallet_account_service.Service ): Module[T] = result = Module[T]() result.delegate = delegate diff --git a/src/app_service/service/token/dto.nim b/src/app_service/service/token/dto.nim index 9afc60691e..a2af238147 100644 --- a/src/app_service/service/token/dto.nim +++ b/src/app_service/service/token/dto.nim @@ -20,7 +20,7 @@ type proc newDto*( name: string, chainId: int, address: Address, symbol: string, decimals: int, hasIcon: bool, isCustom: bool = false ): Dto = - Dto( + return Dto( name: name, chainId: chainId, address: address, symbol: symbol, decimals: decimals, hasIcon: hasIcon, isCustom: isCustom ) diff --git a/src/app_service/service/wallet_account/dto.nim b/src/app_service/service/wallet_account/dto.nim new file mode 100644 index 0000000000..7fcf35e5c0 --- /dev/null +++ b/src/app_service/service/wallet_account/dto.nim @@ -0,0 +1,46 @@ +import json + +include ../../common/json_utils + +type + WalletAccountDto* = ref object of RootObj + name*: string + address*: string + path*: string + color*: string + publicKey*: string + walletType*: string + isWallet*: bool + isChat*: bool + +proc newDto*( + name: string, + address: string, + path: string, + color: string, + publicKey: string, + walletType: string, + isWallet: bool, + isChat: bool +): WalletAccountDto = + return WalletAccountDto( + name: name, + address: address, + path: path, + color: color, + publicKey: publicKey, + walletType: walletType, + isWallet: isWallet, + isChat: isChat + ) + +proc toDto*(jsonObj: JsonNode): WalletAccountDto = + result = WalletAccountDto() + discard jsonObj.getProp("name", result.name) + discard jsonObj.getProp("address", result.address) + discard jsonObj.getProp("path", result.path) + discard jsonObj.getProp("color", result.color) + discard jsonObj.getProp("wallet", result.isWallet) + discard jsonObj.getProp("chat", result.isChat) + discard jsonObj.getProp("public-key", result.publicKey) + discard jsonObj.getProp("type", result.walletType) \ No newline at end of file diff --git a/src/app_service/service/wallet_account/service.nim b/src/app_service/service/wallet_account/service.nim new file mode 100644 index 0000000000..4207b00884 --- /dev/null +++ b/src/app_service/service/wallet_account/service.nim @@ -0,0 +1,36 @@ +import Tables, json, sequtils, sugar, chronicles + +import ./service_interface, ./dto +import status/statusgo_backend_new/accounts as status_go + +export service_interface + +logScope: + topics = "wallet-account-service" + +type + Service* = ref object of service_interface.ServiceInterface + accounts: Table[string, WalletAccountDto] + +method delete*(self: Service) = + discard + +proc newService*(): Service = + result = Service() + result.accounts = initTable[string, WalletAccountDto]() + +method init*(self: Service) = + try: + let response = status_go.getAccounts() + let accounts = map(response.result.getElems(), proc(x: JsonNode): WalletAccountDto = x.toDto()) + + for account in accounts: + self.accounts[account.address] = account + echo account.address + except Exception as e: + let errDesription = e.msg + error "error: ", errDesription + return + +method getAccounts*(self: Service): seq[WalletAccountDto] = + return toSeq(self.accounts.values).filter(a => a.isChat) \ No newline at end of file diff --git a/src/app_service/service/wallet_account/service_interface.nim b/src/app_service/service/wallet_account/service_interface.nim new file mode 100644 index 0000000000..3a9f957604 --- /dev/null +++ b/src/app_service/service/wallet_account/service_interface.nim @@ -0,0 +1,16 @@ +import ./dto + +export dto + +type + ServiceInterface* {.pure inheritable.} = ref object of RootObj + ## Abstract class for this service access. + +method delete*(self: ServiceInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method init*(self: ServiceInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method getAccounts*(self: ServiceInterface): seq[WalletAccountDto] {.base.} = + raise newException(ValueError, "No implementation available")