From 0d129fa7fd675d5f22d034310047742847d3d78d Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 19 Oct 2021 11:03:52 +0200 Subject: [PATCH] refactor(@desktop/wallet): Add settings service and to module --- src/app/boot/app_controller.nim | 11 ++++-- src/app/modules/main/module.nim | 7 +++- .../modules/main/wallet_section/module.nim | 4 +- src/app_service/service/setting/dto.nim | 38 +++++++++++++++++++ src/app_service/service/setting/service.nim | 32 ++++++++++++++++ .../service/setting/service_interface.nim | 16 ++++++++ .../service/wallet_account/service.nim | 1 - vendor/status-lib | 2 +- 8 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 src/app_service/service/setting/dto.nim create mode 100644 src/app_service/service/setting/service.nim create mode 100644 src/app_service/service/setting/service_interface.nim diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 972bc4f8ae..c392961304 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -10,7 +10,7 @@ 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 ../../app_service/service/setting/service as setting_service import ../core/local_account_settings import ../modules/startup/module as startup_module @@ -66,6 +66,7 @@ type transactionService: transaction_service.Service collectibleService: collectible_service.Service walletAccountService: wallet_account_service.Service + settingService: setting_service.Service # Core localAccountSettings: LocalAccountSettings @@ -123,7 +124,7 @@ proc newAppController*(appService: AppService): AppController = result.transactionService = transaction_service.newService() result.collectibleService = collectible_service.newService() result.walletAccountService = wallet_account_service.newService() - + result.settingService = setting_service.newService() # Core result.localAccountSettingsVariant = newQVariant( @@ -146,7 +147,8 @@ proc newAppController*(appService: AppService): AppController = result.tokenService, result.transactionService, result.collectibleService, - result.walletAccountService + result.walletAccountService, + result.settingService ) ################################################# @@ -212,7 +214,8 @@ proc load*(self: AppController) = self.communityService.init() self.tokenService.init() self.walletAccountService.init() - + self.settingService.init() + self.mainModule.load() proc userLoggedIn*(self: AppController) = diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index 9a284fe0e0..4f71c87647 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -14,6 +14,7 @@ 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 ../../../app_service/service/setting/service as setting_service import eventemitter @@ -49,7 +50,8 @@ proc newModule*[T]( tokenService: token_service.Service, transactionService: transaction_service.Service, collectibleService: collectible_service.Service, - walletAccountService: wallet_account_service.Service + walletAccountService: wallet_account_service.Service, + settingService: setting_service.Service ): Module[T] = result = Module[T]() result.delegate = delegate @@ -74,7 +76,8 @@ proc newModule*[T]( tokenService, transactionService, collectible_service, - walletAccountService + walletAccountService, + settingService ) diff --git a/src/app/modules/main/wallet_section/module.nim b/src/app/modules/main/wallet_section/module.nim index efd7912e0f..0236956fcf 100644 --- a/src/app/modules/main/wallet_section/module.nim +++ b/src/app/modules/main/wallet_section/module.nim @@ -15,6 +15,7 @@ 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 ../../../../app_service/service/setting/service as setting_service import io_interface export io_interface @@ -38,7 +39,8 @@ proc newModule*[T]( tokenService: token_service.Service, transactionService: transaction_service.Service, collectibleService: collectible_service.Service, - walletAccountService: wallet_account_service.Service + walletAccountService: wallet_account_service.Service, + settingService: setting_service.Service ): Module[T] = result = Module[T]() result.delegate = delegate diff --git a/src/app_service/service/setting/dto.nim b/src/app_service/service/setting/dto.nim new file mode 100644 index 0000000000..5604c2f128 --- /dev/null +++ b/src/app_service/service/setting/dto.nim @@ -0,0 +1,38 @@ +import json + +include ../../common/json_utils + +const DEFAULT_NETWORK_ID = "mainnet_rpc" + +type NetworkDto* = ref object of RootObj + id*: string + etherscanLink*: string + name*: string + +type + SettingDto* = ref object of RootObj + currentNetwork*: NetworkDto + signingPhrase*: string + currency*: string + +proc toDto*(jsonObj: JsonNode): SettingDto = + result = SettingDto() + discard jsonObj.getProp("signing-phrase", result.signingPhrase) + discard jsonObj.getProp("currency", result.currency) + + var currentNetworkId: string + if not jsonObj.getProp("networks/current-network", currentNetworkId): + currentNetworkId = DEFAULT_NETWORK_ID + + var networks: JsonNode + discard jsonObj.getProp("networks/networks", networks) + for networkJson in networks.getElems(): + if networkJson{"id"}.getStr != currentNetworkId: + continue + + var networkDto = NetworkDto() + discard networkJson.getProp("id", networkDto.id) + discard networkJson.getProp("name", networkDto.name) + discard networkJson.getProp("etherscan-link", networkDto.etherscanLink) + result.currentNetwork = networkDto + break \ No newline at end of file diff --git a/src/app_service/service/setting/service.nim b/src/app_service/service/setting/service.nim new file mode 100644 index 0000000000..6b8e78889f --- /dev/null +++ b/src/app_service/service/setting/service.nim @@ -0,0 +1,32 @@ +import chronicles + +import ./service_interface, ./dto +import status/statusgo_backend_new/settings as status_go + +export service_interface + +logScope: + topics = "setting-service" + +type + Service* = ref object of service_interface.ServiceInterface + setting: SettingDto + +method delete*(self: Service) = + discard + +proc newService*(): Service = + result = Service() + +method init*(self: Service) = + try: + let response = status_go.getSettings() + self.setting = response.result.toDto() + echo self.setting.currentNetwork.etherscanLink + except Exception as e: + let errDesription = e.msg + error "error: ", errDesription + return + +method getSetting*(self: Service): SettingDto = + return self.setting \ No newline at end of file diff --git a/src/app_service/service/setting/service_interface.nim b/src/app_service/service/setting/service_interface.nim new file mode 100644 index 0000000000..e7e00caefc --- /dev/null +++ b/src/app_service/service/setting/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 getSetting*(self: ServiceInterface): seq[SettingDto] {.base.} = + raise newException(ValueError, "No implementation available") diff --git a/src/app_service/service/wallet_account/service.nim b/src/app_service/service/wallet_account/service.nim index 4207b00884..80d7d01746 100644 --- a/src/app_service/service/wallet_account/service.nim +++ b/src/app_service/service/wallet_account/service.nim @@ -26,7 +26,6 @@ method init*(self: Service) = for account in accounts: self.accounts[account.address] = account - echo account.address except Exception as e: let errDesription = e.msg error "error: ", errDesription diff --git a/vendor/status-lib b/vendor/status-lib index a507f8b579..f320378bd5 160000 --- a/vendor/status-lib +++ b/vendor/status-lib @@ -1 +1 @@ -Subproject commit a507f8b579756e149c09bec026dccb3e687d024a +Subproject commit f320378bd5fee8fa4b1af66e608ef352bbf0a215