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(
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.profileService = profile_service.newService()
result.settingsService = settings_service.newService()

View File

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

View File

@ -19,7 +19,7 @@ type
Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface
delegate: io_interface.AccessInterface
events: EventEmitter
transactionService: transaction_service.ServiceInterface
transactionService: transaction_service.Service
walletAccountService: wallet_account_service.ServiceInterface
# Forward declaration
@ -29,7 +29,7 @@ method getWalletAccounts*[T](self: Controller[T]): seq[WalletAccountDto]
proc newController*[T](
delegate: io_interface.AccessInterface,
events: EventEmitter,
transactionService: transaction_service.ServiceInterface,
transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.ServiceInterface
): Controller[T] =
result = Controller[T]()
@ -62,6 +62,10 @@ method init*[T](self: Controller[T]) =
else:
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]) =
self.transactionService.checkRecentHistory()
@ -75,26 +79,4 @@ method getAccountByAddress*[T](self: Controller[T], address: string): WalletAcco
self.walletAccountService.getAccountByAddress(address)
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.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)
self.transactionService.loadTransactions(address, toBlock, limit, loadMore)

View File

@ -22,7 +22,7 @@ method loadTransactions*[T](self: Module[T], address: string, toBlock: string =
proc newModule*[T](
delegate: T,
events: EventEmitter,
transactionService: transaction_service.ServiceInterface,
transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.ServiceInterface
): 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 eventemitter
import ../../tasks/[qt, threadpool]
import ../wallet_account/service as wallet_account_service
import ./service_interface, ./dto
@ -12,38 +15,37 @@ logScope:
import ../../../app_service/[main]
import ../../../app_service/tasks/[qt, threadpool]
type
LoadTransactionsTaskArg* = ref object of QObjectTaskArg
address: string
toBlock: Uint256
limit: int
loadMore: bool
include async_tasks
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)
# Signals which may be emitted by this service:
const SIGNAL_TRANSACTIONS_LOADED* = "SIGNAL_TRANSACTIONS_LOADED"
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
method delete*(self: Service) =
discard
proc delete*(self: Service) =
self.QObject.delete
proc newService*(walletAccountService: wallet_account_service.ServiceInterface): Service =
result = Service()
proc newService*(events: EventEmitter, threadpool: ThreadPool, walletAccountService: wallet_account_service.ServiceInterface): Service =
new(result, delete)
result.QObject.setup
result.events = events
result.threadpool = threadpool
result.walletAccountService = walletAccountService
method init*(self: Service) =
proc init*(self: Service) =
discard
method checkRecentHistory*(self: Service) =
proc checkRecentHistory*(self: Service) =
try:
let addresses = self.walletAccountService.getWalletAccounts().map(a => a.address)
transactions.checkRecentHistory(addresses)
@ -52,7 +54,7 @@ method checkRecentHistory*(self: Service) =
error "error: ", errDesription
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:
let response = transactions.getTransfersByAddress(address, toBlock, limit, loadMore)
@ -65,15 +67,29 @@ method getTransfersByAddress*(self: Service, address: string, toBlock: Uint256,
error "error: ", errDesription
return
method getTransfersByAddressTemp*(self: Service, address: string, toBlock: Uint256, limit: int, loadMore: bool = false): string =
try:
let resp = transactions.getTransfersByAddress(address, toBlock, limit, loadMore)
return $(%*{
"address": address,
"history": resp,
"loadMore": loadMore
})
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription
return
proc setTrxHistoryResult*(self: Service, 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())
# emit event
self.events.emit(SIGNAL_TRANSACTIONS_LOADED, TransactionsLoadedArgs(
transactions: transactions,
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)