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/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()

View File

@ -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.

View File

@ -26,18 +26,15 @@ 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(
method updateCryptoServices*(self: Module, cryptoServices: seq[CryptoRampDto]) =
let items = cryptoServices.map(proc (w: CryptoRampDto): item.Item = result = initItem(
w.name,
w.description,
w.fees,
@ -49,6 +46,7 @@ method fetchCryptoServices*(self: Module) =
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()

View File

@ -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]:

View File

@ -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": @[],
})

View File

@ -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()

View File

@ -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())