refactor: make tx history fetch async again

This commit is contained in:
Jonathan Rainville 2021-11-01 14:08:55 -04:00 committed by Iuri Matias
parent 8aec0c62e7
commit 8dcc63bdf6
6 changed files with 106 additions and 85 deletions

View File

@ -133,7 +133,7 @@ proc newAppController*(appService: AppService): AppController =
result.walletAccountService = wallet_account_service.newService( result.walletAccountService = wallet_account_service.newService(
appService.status.events, result.settingService, result.tokenService appService.status.events, result.settingService, result.tokenService
) )
result.transactionService = transaction_service.newService(result.walletAccountService) result.transactionService = transaction_service.newService(appService.status.events, appService.threadpool, result.walletAccountService)
result.bookmarkService = bookmark_service.newService() result.bookmarkService = bookmark_service.newService()
result.profileService = profile_service.newService() result.profileService = profile_service.newService()
result.settingsService = settings_service.newService() result.settingsService = settings_service.newService()

View File

@ -41,7 +41,7 @@ proc newModule*[T](
delegate: T, delegate: T,
events: EventEmitter, events: EventEmitter,
tokenService: token_service.ServiceInterface, tokenService: token_service.ServiceInterface,
transactionService: transaction_service.ServiceInterface, transactionService: transaction_service.Service,
collectibleService: collectible_service.ServiceInterface, collectibleService: collectible_service.ServiceInterface,
walletAccountService: wallet_account_service.ServiceInterface, walletAccountService: wallet_account_service.ServiceInterface,
settingService: setting_service.ServiceInterface settingService: setting_service.ServiceInterface

View File

@ -19,7 +19,7 @@ type
Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
events: EventEmitter events: EventEmitter
transactionService: transaction_service.ServiceInterface transactionService: transaction_service.Service
walletAccountService: wallet_account_service.ServiceInterface walletAccountService: wallet_account_service.ServiceInterface
# Forward declaration # Forward declaration
@ -29,7 +29,7 @@ method getWalletAccounts*[T](self: Controller[T]): seq[WalletAccountDto]
proc newController*[T]( proc newController*[T](
delegate: io_interface.AccessInterface, delegate: io_interface.AccessInterface,
events: EventEmitter, events: EventEmitter,
transactionService: transaction_service.ServiceInterface, transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.ServiceInterface walletAccountService: wallet_account_service.ServiceInterface
): Controller[T] = ): Controller[T] =
result = Controller[T]() result = Controller[T]()
@ -62,6 +62,10 @@ method init*[T](self: Controller[T]) =
else: else:
echo "Unhandled wallet signal: ", data.eventType echo "Unhandled wallet signal: ", data.eventType
self.events.on(SIGNAL_TRANSACTIONS_LOADED) do(e:Args):
let args = TransactionsLoadedArgs(e)
self.delegate.setTrxHistoryResult(args.transactions, args.address, args.wasFetchMore)
method checkRecentHistory*[T](self: Controller[T]) = method checkRecentHistory*[T](self: Controller[T]) =
self.transactionService.checkRecentHistory() self.transactionService.checkRecentHistory()
@ -75,26 +79,4 @@ method getAccountByAddress*[T](self: Controller[T], address: string): WalletAcco
self.walletAccountService.getAccountByAddress(address) self.walletAccountService.getAccountByAddress(address)
method loadTransactions*[T](self: Controller[T], address: string, toBlock: Uint256, limit: int = 20, loadMore: bool = false) = method loadTransactions*[T](self: Controller[T], address: string, toBlock: Uint256, limit: int = 20, loadMore: bool = false) =
let transactions = self.transactionService.getTransfersByAddressTemp(address, toBlock, limit, loadMore) self.transactionService.loadTransactions(address, toBlock, limit, loadMore)
self.setTrxHistoryResult(transactions)
# TODO reimplement thread task
# let arg = LoadTransactionsTaskArg(
# address: address,
# tptr: cast[ByteAddress](loadTransactionsTask),
# vptr: cast[ByteAddress](self.vptr),
# slot: "setTrxHistoryResult",
# toBlock: toBlock,
# limit: limit,
# loadMore: loadMore
# )
# self.appService.threadpool.start(arg)
method setTrxHistoryResult*[T](self: Controller[T], historyJSON: string) {.slot.} =
let historyData = parseJson(historyJSON)
let address = historyData["address"].getStr
let wasFetchMore = historyData["loadMore"].getBool
var transactions: seq[TransactionDto] = @[]
for tx in historyData["history"]["result"].getElems():
transactions.add(tx.toTransactionDto())
self.delegate.setTrxHistoryResult(transactions, address, wasFetchMore)

View File

@ -22,7 +22,7 @@ method loadTransactions*[T](self: Module[T], address: string, toBlock: string =
proc newModule*[T]( proc newModule*[T](
delegate: T, delegate: T,
events: EventEmitter, events: EventEmitter,
transactionService: transaction_service.ServiceInterface, transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.ServiceInterface walletAccountService: wallet_account_service.ServiceInterface
): Module[T] = ): Module[T] =
result = Module[T]() result = Module[T]()

View File

@ -0,0 +1,23 @@
include ../../common/json_utils
include ../../tasks/common
#################################################
# Async load transactions
#################################################
type
LoadTransactionsTaskArg* = ref object of QObjectTaskArg
address: string
toBlock: Uint256
limit: int
loadMore: bool
const loadTransactionsTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let
arg = decode[LoadTransactionsTaskArg](argEncoded)
output = %*{
"address": arg.address,
"history": transactions.getTransfersByAddress(arg.address, arg.toBlock, arg.limit, arg.loadMore),
"loadMore": arg.loadMore
}
arg.finish(output)

View File

@ -1,6 +1,9 @@
import chronicles, sequtils, sugar, stint, json import NimQml, chronicles, sequtils, sugar, stint, json
import status/statusgo_backend_new/transactions as transactions import status/statusgo_backend_new/transactions as transactions
import eventemitter
import ../../tasks/[qt, threadpool]
import ../wallet_account/service as wallet_account_service import ../wallet_account/service as wallet_account_service
import ./service_interface, ./dto import ./service_interface, ./dto
@ -12,38 +15,37 @@ logScope:
import ../../../app_service/[main] import ../../../app_service/[main]
import ../../../app_service/tasks/[qt, threadpool] import ../../../app_service/tasks/[qt, threadpool]
type include async_tasks
LoadTransactionsTaskArg* = ref object of QObjectTaskArg
address: string
toBlock: Uint256
limit: int
loadMore: bool
const loadTransactionsTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} = # Signals which may be emitted by this service:
let const SIGNAL_TRANSACTIONS_LOADED* = "SIGNAL_TRANSACTIONS_LOADED"
arg = decode[LoadTransactionsTaskArg](argEncoded)
output = %*{
"address": arg.address,
"history": transactions.getTransfersByAddress(arg.address, arg.toBlock, arg.limit, arg.loadMore),
"loadMore": arg.loadMore
}
arg.finish(output)
type type
Service* = ref object of service_interface.ServiceInterface TransactionsLoadedArgs* = ref object of Args
transactions*: seq[TransactionDto]
address*: string
wasFetchMore*: bool
QtObject:
type Service* = ref object of QObject
events: EventEmitter
threadpool: ThreadPool
walletAccountService: wallet_account_service.ServiceInterface walletAccountService: wallet_account_service.ServiceInterface
method delete*(self: Service) = proc delete*(self: Service) =
discard self.QObject.delete
proc newService*(walletAccountService: wallet_account_service.ServiceInterface): Service = proc newService*(events: EventEmitter, threadpool: ThreadPool, walletAccountService: wallet_account_service.ServiceInterface): Service =
result = Service() new(result, delete)
result.QObject.setup
result.events = events
result.threadpool = threadpool
result.walletAccountService = walletAccountService result.walletAccountService = walletAccountService
method init*(self: Service) = proc init*(self: Service) =
discard discard
method checkRecentHistory*(self: Service) = proc checkRecentHistory*(self: Service) =
try: try:
let addresses = self.walletAccountService.getWalletAccounts().map(a => a.address) let addresses = self.walletAccountService.getWalletAccounts().map(a => a.address)
transactions.checkRecentHistory(addresses) transactions.checkRecentHistory(addresses)
@ -52,7 +54,7 @@ method checkRecentHistory*(self: Service) =
error "error: ", errDesription error "error: ", errDesription
return return
method getTransfersByAddress*(self: Service, address: string, toBlock: Uint256, limit: int, loadMore: bool = false): seq[TransactionDto] = proc getTransfersByAddress*(self: Service, address: string, toBlock: Uint256, limit: int, loadMore: bool = false): seq[TransactionDto] =
try: try:
let response = transactions.getTransfersByAddress(address, toBlock, limit, loadMore) let response = transactions.getTransfersByAddress(address, toBlock, limit, loadMore)
@ -65,15 +67,29 @@ method getTransfersByAddress*(self: Service, address: string, toBlock: Uint256,
error "error: ", errDesription error "error: ", errDesription
return return
method getTransfersByAddressTemp*(self: Service, address: string, toBlock: Uint256, limit: int, loadMore: bool = false): string = proc setTrxHistoryResult*(self: Service, historyJSON: string) {.slot.} =
try: let historyData = parseJson(historyJSON)
let resp = transactions.getTransfersByAddress(address, toBlock, limit, loadMore) let address = historyData["address"].getStr
return $(%*{ let wasFetchMore = historyData["loadMore"].getBool
"address": address, var transactions: seq[TransactionDto] = @[]
"history": resp, for tx in historyData["history"]["result"].getElems():
"loadMore": loadMore transactions.add(tx.toTransactionDto())
})
except Exception as e: # emit event
let errDesription = e.msg self.events.emit(SIGNAL_TRANSACTIONS_LOADED, TransactionsLoadedArgs(
error "error: ", errDesription transactions: transactions,
return address: address,
wasFetchMore: wasFetchMore
))
proc loadTransactions*(self: Service, address: string, toBlock: Uint256, limit: int = 20, loadMore: bool = false) =
let arg = LoadTransactionsTaskArg(
address: address,
tptr: cast[ByteAddress](loadTransactionsTask),
vptr: cast[ByteAddress](self.vptr),
slot: "setTrxHistoryResult",
toBlock: toBlock,
limit: limit,
loadMore: loadMore
)
self.threadpool.start(arg)