feat(@wallet): move query async

This commit is contained in:
Anthony Laibe 2023-02-09 10:42:20 +01:00 committed by Anthony Laibe
parent 26baaa0e15
commit a5194e094f
7 changed files with 83 additions and 29 deletions

View File

@ -2,26 +2,31 @@ import io_interface
import ../../../../../app_service/service/transaction/service as transaction_service import ../../../../../app_service/service/transaction/service as transaction_service
import ../../../../../app_service/service/transaction/cryptoRampDto import ../../../../../app_service/service/transaction/cryptoRampDto
import ../../../../core/eventemitter
type type
Controller* = ref object of RootObj Controller* = ref object of RootObj
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
transactionService: transaction_service.Service transactionService: transaction_service.Service
events: EventEmitter
proc newController*( proc newController*(
delegate: io_interface.AccessInterface, delegate: io_interface.AccessInterface,
events: EventEmitter,
transactionService: transaction_service.Service transactionService: transaction_service.Service
): Controller = ): Controller =
result = Controller() result = Controller()
result.delegate = delegate result.delegate = delegate
result.events = events
result.transactionService = transactionService result.transactionService = transactionService
proc delete*(self: Controller) = proc delete*(self: Controller) =
discard discard
proc init*(self: Controller) = 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] = proc fetchCryptoServices*(self: Controller) =
return self.transactionService.fetchCryptoServices() self.transactionService.fetchCryptoServices()

View File

@ -1,3 +1,5 @@
import ../../../../../app_service/service/transaction/cryptoRampDto
type type
AccessInterface* {.pure inheritable.} = ref object of RootObj AccessInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for any input/interaction with this module. ## Abstract class for any input/interaction with this module.
@ -11,6 +13,9 @@ method load*(self: AccessInterface) {.base.} =
method isLoaded*(self: AccessInterface): bool {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method updateCryptoServices*(self: AccessInterface, cryptoServices: seq[CryptoRampDto]) {.base.} =
raise newException(ValueError, "No implementation available")
# View Delegate Interface # View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi # Delegate for the view must be declared here due to use of QtObject and multi
# inheritance, which is not well supported in Nim. # inheritance, which is not well supported in Nim.

View File

@ -26,29 +26,27 @@ proc newModule*(
result.delegate = delegate result.delegate = delegate
result.events = events result.events = events
result.view = newView(result) result.view = newView(result)
result.controller = controller.newController(result, transactionService) result.controller = controller.newController(result, events, transactionService)
result.moduleLoaded = false result.moduleLoaded = false
method delete*(self: Module) = method delete*(self: Module) =
self.view.delete self.view.delete
self.controller.delete self.controller.delete
method fetchCryptoServices*(self: Module) = method updateCryptoServices*(self: Module, cryptoServices: seq[CryptoRampDto]) =
let cryptoServices = self.controller.fetchCryptoServices() let items = cryptoServices.map(proc (w: CryptoRampDto): item.Item = result = initItem(
w.name,
let items = cryptoServices.map(proc (w: CryptoRampDto): item.Item = w.description,
result = initItem( w.fees,
w.name, w.logoUrl,
w.description, w.siteUrl,
w.fees, w.hostname
w.logoUrl, ))
w.siteUrl,
w.hostname
))
self.view.setItems(items) self.view.setItems(items)
method load*(self: Module) = method load*(self: Module) =
singletonInstance.engine.setRootContextProperty("walletSectionBuySellCrypto", newQVariant(self.view)) singletonInstance.engine.setRootContextProperty("walletSectionBuySellCrypto", newQVariant(self.view))
self.controller.fetchCryptoServices()
self.controller.init() self.controller.init()
self.view.load() self.view.load()
@ -56,6 +54,5 @@ method isLoaded*(self: Module): bool =
return self.moduleLoaded return self.moduleLoaded
method viewDidLoad*(self: Module) = method viewDidLoad*(self: Module) =
self.fetchCryptoServices()
self.moduleLoaded = true self.moduleLoaded = true
self.delegate.buySellCryptoModuleDidLoad() self.delegate.buySellCryptoModuleDidLoad()

View File

@ -69,7 +69,10 @@ QtObject:
result.tokens = initTable[int, seq[TokenDto]]() result.tokens = initTable[int, seq[TokenDto]]()
result.priceCache = newTimedCache[float64]() result.priceCache = newTimedCache[float64]()
proc init*(self: Service) = proc loadData*(self: Service) =
if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
try: try:
let response = backend.getCachedPrices() let response = backend.getCachedPrices()
let prices = jsonToPricesMap(response.result) let prices = jsonToPricesMap(response.result)
@ -100,6 +103,13 @@ QtObject:
except Exception as e: except Exception as e:
error "Tokens init error", errDesription = e.msg 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 = proc findTokenBySymbol*(self: Service, network: NetworkDto, symbol: string): TokenDto =
try: try:
for token in self.tokens[network.chainId]: for token in self.tokens[network.chainId]:

View File

@ -164,3 +164,25 @@ const watchTransactionTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.}
"trxType": arg.trxType, "trxType": arg.trxType,
"isSuccessfull": false "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": @[],
})

View File

@ -11,6 +11,7 @@ import ../../common/conversion as common_conversion
import ../../../app/core/[main] import ../../../app/core/[main]
import ../../../app/core/signals/types import ../../../app/core/signals/types
import ../../../app/core/tasks/[qt, threadpool] import ../../../app/core/tasks/[qt, threadpool]
import ../../../app/global/global_singleton
import ../wallet_account/service as wallet_account_service import ../wallet_account/service as wallet_account_service
import ../network/service as network_service import ../network/service as network_service
import ../token/service as token_service import ../token/service as token_service
@ -40,6 +41,7 @@ const SIGNAL_HISTORY_FETCHING* = "historyFetching"
const SIGNAL_HISTORY_READY* = "historyReady" const SIGNAL_HISTORY_READY* = "historyReady"
const SIGNAL_HISTORY_NON_ARCHIVAL_NODE* = "historyNonArchivalNode" const SIGNAL_HISTORY_NON_ARCHIVAL_NODE* = "historyNonArchivalNode"
const SIGNAL_HISTORY_ERROR* = "historyError" const SIGNAL_HISTORY_ERROR* = "historyError"
const SIGNAL_CRYPTO_SERVICES_READY* = "cryptoServicesReady"
const SIMPLE_TX_BRIDGE_NAME = "Simple" const SIMPLE_TX_BRIDGE_NAME = "Simple"
const HOP_TX_BRIDGE_NAME = "Hop" const HOP_TX_BRIDGE_NAME = "Hop"
@ -81,6 +83,11 @@ type
PendingTxCompletedArgs* = ref object of Args PendingTxCompletedArgs* = ref object of Args
txHash*: string txHash*: string
type
CryptoServicesArgs* = ref object of Args
data*: seq[CryptoRampDto]
QtObject: QtObject:
type Service* = ref object of QObject type Service* = ref object of QObject
events: EventEmitter events: EventEmitter
@ -113,6 +120,8 @@ QtObject:
result.txCounter = initTable[string, seq[int]]() result.txCounter = initTable[string, seq[int]]()
proc init*(self: Service) = proc init*(self: Service) =
signalConnect(singletonInstance.localAccountSensitiveSettings, "isWalletEnabledChanged()", self, "onIsWalletEnabledChanged()", 2)
self.events.on(SignalType.Wallet.event) do(e:Args): self.events.on(SignalType.Wallet.event) do(e:Args):
var data = WalletSignal(e) var data = WalletSignal(e)
case data.eventType: case data.eventType:
@ -460,17 +469,20 @@ QtObject:
) )
self.threadpool.start(arg) self.threadpool.start(arg)
proc fetchCryptoServices*(self: Service): seq[CryptoRampDto] = proc onFetchCryptoServices*(self: Service, response: string) {.slot.} =
try: let cryptoServices = parseJson(response){"result"}.getElems().map(x => x.toCryptoRampDto())
let response = transactions.fetchCryptoServices() self.events.emit(SIGNAL_CRYPTO_SERVICES_READY, CryptoServicesArgs(data: cryptoServices))
if not response.error.isNil: proc fetchCryptoServices*(self: Service) =
raise newException(ValueError, "Error fetching crypto services" & response.error.message) if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
return response.result.getElems().map(x => x.toCryptoRampDto()) let arg = GetCryptoServicesTaskArg(
except Exception as e: tptr: cast[ByteAddress](getCryptoServicesTask),
error "Error fetching crypto services", message = e.msg vptr: cast[ByteAddress](self.vptr),
return @[] slot: "onFetchCryptoServices",
)
self.threadpool.start(arg)
proc getEstimatedTime*(self: Service, chainId: int, maxFeePerGas: string): EstimatedTime = proc getEstimatedTime*(self: Service, chainId: int, maxFeePerGas: string): EstimatedTime =
try: try:
@ -487,3 +499,6 @@ QtObject:
except Exception as e: except Exception as e:
error "Error getting latest block number", message = e.msg error "Error getting latest block number", message = e.msg
return "" return ""
proc onIsWalletEnabledChanged*(self: Service) {.slot.} =
self.fetchCryptoServices()

View File

@ -416,7 +416,7 @@ QtObject:
method toggleTestNetworksEnabled*(self: Service) = method toggleTestNetworksEnabled*(self: Service) =
discard self.settingsService.toggleTestNetworksEnabled() discard self.settingsService.toggleTestNetworksEnabled()
self.tokenService.init() self.tokenService.loadData()
self.checkRecentHistory() self.checkRecentHistory()
self.events.emit(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED, NetwordkEnabledToggled()) self.events.emit(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED, NetwordkEnabledToggled())