From a5194e094f3b2cb34c65d0125cbdf5217be51824 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 9 Feb 2023 10:42:20 +0100 Subject: [PATCH] feat(@wallet): move query async --- .../buy_sell_crypto/controller.nim | 13 +++++--- .../buy_sell_crypto/io_interface.nim | 5 +++ .../wallet_section/buy_sell_crypto/module.nim | 25 +++++++------- src/app_service/service/token/service.nim | 12 ++++++- .../service/transaction/async_tasks.nim | 22 +++++++++++++ .../service/transaction/service.nim | 33 ++++++++++++++----- .../service/wallet_account/service.nim | 2 +- 7 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/app/modules/main/wallet_section/buy_sell_crypto/controller.nim b/src/app/modules/main/wallet_section/buy_sell_crypto/controller.nim index 9bbb89cc32..4c1ce291f0 100644 --- a/src/app/modules/main/wallet_section/buy_sell_crypto/controller.nim +++ b/src/app/modules/main/wallet_section/buy_sell_crypto/controller.nim @@ -2,26 +2,31 @@ import io_interface import ../../../../../app_service/service/transaction/service as transaction_service import ../../../../../app_service/service/transaction/cryptoRampDto - +import ../../../../core/eventemitter type Controller* = ref object of RootObj delegate: io_interface.AccessInterface transactionService: transaction_service.Service + events: EventEmitter proc newController*( delegate: io_interface.AccessInterface, + events: EventEmitter, transactionService: transaction_service.Service ): Controller = result = Controller() result.delegate = delegate + result.events = events result.transactionService = transactionService proc delete*(self: Controller) = discard proc init*(self: Controller) = - discard + self.events.on(SIGNAL_CRYPTO_SERVICES_READY) do(e:Args): + let args = CryptoServicesArgs(e) + self.delegate.updateCryptoServices(args.data) -proc fetchCryptoServices*(self: Controller): seq[CryptoRampDto] = - return self.transactionService.fetchCryptoServices() +proc fetchCryptoServices*(self: Controller) = + self.transactionService.fetchCryptoServices() diff --git a/src/app/modules/main/wallet_section/buy_sell_crypto/io_interface.nim b/src/app/modules/main/wallet_section/buy_sell_crypto/io_interface.nim index 27d1e05cf7..782838b676 100644 --- a/src/app/modules/main/wallet_section/buy_sell_crypto/io_interface.nim +++ b/src/app/modules/main/wallet_section/buy_sell_crypto/io_interface.nim @@ -1,3 +1,5 @@ +import ../../../../../app_service/service/transaction/cryptoRampDto + type AccessInterface* {.pure inheritable.} = ref object of RootObj ## Abstract class for any input/interaction with this module. @@ -11,6 +13,9 @@ method load*(self: AccessInterface) {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} = raise newException(ValueError, "No implementation available") +method updateCryptoServices*(self: AccessInterface, cryptoServices: seq[CryptoRampDto]) {.base.} = + raise newException(ValueError, "No implementation available") + # View Delegate Interface # Delegate for the view must be declared here due to use of QtObject and multi # inheritance, which is not well supported in Nim. diff --git a/src/app/modules/main/wallet_section/buy_sell_crypto/module.nim b/src/app/modules/main/wallet_section/buy_sell_crypto/module.nim index 8a4df23c3f..76bc587958 100644 --- a/src/app/modules/main/wallet_section/buy_sell_crypto/module.nim +++ b/src/app/modules/main/wallet_section/buy_sell_crypto/module.nim @@ -26,29 +26,27 @@ proc newModule*( result.delegate = delegate result.events = events result.view = newView(result) - result.controller = controller.newController(result, transactionService) + result.controller = controller.newController(result, events, transactionService) result.moduleLoaded = false method delete*(self: Module) = self.view.delete self.controller.delete -method fetchCryptoServices*(self: Module) = - let cryptoServices = self.controller.fetchCryptoServices() - - let items = cryptoServices.map(proc (w: CryptoRampDto): item.Item = - result = initItem( - w.name, - w.description, - w.fees, - w.logoUrl, - w.siteUrl, - w.hostname - )) +method updateCryptoServices*(self: Module, cryptoServices: seq[CryptoRampDto]) = + let items = cryptoServices.map(proc (w: CryptoRampDto): item.Item = result = initItem( + w.name, + w.description, + w.fees, + w.logoUrl, + w.siteUrl, + w.hostname + )) self.view.setItems(items) method load*(self: Module) = singletonInstance.engine.setRootContextProperty("walletSectionBuySellCrypto", newQVariant(self.view)) + self.controller.fetchCryptoServices() self.controller.init() self.view.load() @@ -56,6 +54,5 @@ method isLoaded*(self: Module): bool = return self.moduleLoaded method viewDidLoad*(self: Module) = - self.fetchCryptoServices() self.moduleLoaded = true self.delegate.buySellCryptoModuleDidLoad() diff --git a/src/app_service/service/token/service.nim b/src/app_service/service/token/service.nim index 7b9129be10..f8096dbbd7 100644 --- a/src/app_service/service/token/service.nim +++ b/src/app_service/service/token/service.nim @@ -69,7 +69,10 @@ QtObject: result.tokens = initTable[int, seq[TokenDto]]() result.priceCache = newTimedCache[float64]() - proc init*(self: Service) = + proc loadData*(self: Service) = + if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()): + return + try: let response = backend.getCachedPrices() let prices = jsonToPricesMap(response.result) @@ -100,6 +103,13 @@ QtObject: except Exception as e: error "Tokens init error", errDesription = e.msg + proc init*(self: Service) = + signalConnect(singletonInstance.localAccountSensitiveSettings, "isWalletEnabledChanged()", self, "onIsWalletEnabledChanged()", 2) + self.loadData() + + proc onIsWalletEnabledChanged*(self: Service) {.slot.} = + self.loadData() + proc findTokenBySymbol*(self: Service, network: NetworkDto, symbol: string): TokenDto = try: for token in self.tokens[network.chainId]: diff --git a/src/app_service/service/transaction/async_tasks.nim b/src/app_service/service/transaction/async_tasks.nim index da52d372de..5932b8b670 100644 --- a/src/app_service/service/transaction/async_tasks.nim +++ b/src/app_service/service/transaction/async_tasks.nim @@ -164,3 +164,25 @@ const watchTransactionTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} "trxType": arg.trxType, "isSuccessfull": false } + +type + GetCryptoServicesTaskArg* = ref object of QObjectTaskArg + discard + +const getCryptoServicesTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = + let arg = decode[GetCryptoServicesTaskArg](argEncoded) + + try: + let response = transactions.fetchCryptoServices() + + if not response.error.isNil: + raise newException(ValueError, "Error fetching crypto services" & response.error.message) + + arg.finish(%* { + "result": response.result, + }) + except Exception as e: + error "Error fetching crypto services", message = e.msg + arg.finish(%* { + "result": @[], + }) diff --git a/src/app_service/service/transaction/service.nim b/src/app_service/service/transaction/service.nim index 8708694bf9..213180ade6 100644 --- a/src/app_service/service/transaction/service.nim +++ b/src/app_service/service/transaction/service.nim @@ -11,6 +11,7 @@ import ../../common/conversion as common_conversion import ../../../app/core/[main] import ../../../app/core/signals/types import ../../../app/core/tasks/[qt, threadpool] +import ../../../app/global/global_singleton import ../wallet_account/service as wallet_account_service import ../network/service as network_service import ../token/service as token_service @@ -40,6 +41,7 @@ const SIGNAL_HISTORY_FETCHING* = "historyFetching" const SIGNAL_HISTORY_READY* = "historyReady" const SIGNAL_HISTORY_NON_ARCHIVAL_NODE* = "historyNonArchivalNode" const SIGNAL_HISTORY_ERROR* = "historyError" +const SIGNAL_CRYPTO_SERVICES_READY* = "cryptoServicesReady" const SIMPLE_TX_BRIDGE_NAME = "Simple" const HOP_TX_BRIDGE_NAME = "Hop" @@ -81,6 +83,11 @@ type PendingTxCompletedArgs* = ref object of Args txHash*: string +type + CryptoServicesArgs* = ref object of Args + data*: seq[CryptoRampDto] + + QtObject: type Service* = ref object of QObject events: EventEmitter @@ -113,6 +120,8 @@ QtObject: result.txCounter = initTable[string, seq[int]]() proc init*(self: Service) = + signalConnect(singletonInstance.localAccountSensitiveSettings, "isWalletEnabledChanged()", self, "onIsWalletEnabledChanged()", 2) + self.events.on(SignalType.Wallet.event) do(e:Args): var data = WalletSignal(e) case data.eventType: @@ -460,17 +469,20 @@ QtObject: ) self.threadpool.start(arg) - proc fetchCryptoServices*(self: Service): seq[CryptoRampDto] = - try: - let response = transactions.fetchCryptoServices() + proc onFetchCryptoServices*(self: Service, response: string) {.slot.} = + let cryptoServices = parseJson(response){"result"}.getElems().map(x => x.toCryptoRampDto()) + self.events.emit(SIGNAL_CRYPTO_SERVICES_READY, CryptoServicesArgs(data: cryptoServices)) - if not response.error.isNil: - raise newException(ValueError, "Error fetching crypto services" & response.error.message) + proc fetchCryptoServices*(self: Service) = + if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()): + return - return response.result.getElems().map(x => x.toCryptoRampDto()) - except Exception as e: - error "Error fetching crypto services", message = e.msg - return @[] + let arg = GetCryptoServicesTaskArg( + tptr: cast[ByteAddress](getCryptoServicesTask), + vptr: cast[ByteAddress](self.vptr), + slot: "onFetchCryptoServices", + ) + self.threadpool.start(arg) proc getEstimatedTime*(self: Service, chainId: int, maxFeePerGas: string): EstimatedTime = try: @@ -487,3 +499,6 @@ QtObject: except Exception as e: error "Error getting latest block number", message = e.msg return "" + + proc onIsWalletEnabledChanged*(self: Service) {.slot.} = + self.fetchCryptoServices() \ 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 index 0827f7e884..d4b5006fac 100644 --- a/src/app_service/service/wallet_account/service.nim +++ b/src/app_service/service/wallet_account/service.nim @@ -416,7 +416,7 @@ QtObject: method toggleTestNetworksEnabled*(self: Service) = discard self.settingsService.toggleTestNetworksEnabled() - self.tokenService.init() + self.tokenService.loadData() self.checkRecentHistory() self.events.emit(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED, NetwordkEnabledToggled())