feat(@wallet): move timer to status-go

This commit is contained in:
Anthony Laibe 2022-12-01 10:19:54 +01:00 committed by Anthony Laibe
parent 1238e91b09
commit 1e086de456
6 changed files with 27 additions and 97 deletions

View File

@ -62,8 +62,6 @@ proc init*(self: Controller) =
let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, false)
else:
echo "Unhandled wallet signal: ", data.eventType
self.events.on(SIGNAL_TRANSACTIONS_LOADED) do(e:Args):
let args = TransactionsLoadedArgs(e)
@ -87,9 +85,6 @@ proc init*(self: Controller) =
proc checkPendingTransactions*(self: Controller): seq[TransactionDto] =
return self.transactionService.checkPendingTransactions()
proc checkRecentHistory*(self: Controller, calledFromTimerOrInit = false) =
self.walletAccountService.checkRecentHistory(calledFromTimerOrInit)
proc getWalletAccounts*(self: Controller): seq[WalletAccountDto] =
self.walletAccountService.getWalletAccounts()

View File

@ -16,9 +16,6 @@ method load*(self: AccessInterface) {.base.} =
method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available")
method checkRecentHistory*(self: AccessInterface, calledFromTimerOrInit: bool) {.base.} =
raise newException(ValueError, "No implementation available")
method switchAccount*(self: AccessInterface, accountIndex: int) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -58,7 +58,6 @@ method isLoaded*(self: Module): bool =
return self.moduleLoaded
method viewDidLoad*(self: Module) =
self.controller.checkRecentHistory(calledFromTimerOrInit = true)
let accounts = self.getWalletAccounts()
self.moduleLoaded = true

View File

@ -95,10 +95,6 @@ type KeycardActivityArgs* = ref object of Args
keycardNewName*: string
keyPair*: KeyPairDto
const CheckBalanceSlotExecuteIntervalInSeconds = 15 * 60 # 15 mins
const CheckBalanceTimerIntervalInMilliseconds = 5000 # 5 sec
const CheckHistoryIntervalInMilliseconds = 20 * 60 * 1000 # 20 mins
include async_tasks
include ../../common/json_utils
@ -120,9 +116,9 @@ QtObject:
isHistoryFetchTimerAlreadyRunning: bool
# Forward declaration
proc buildAllTokens(self: Service, calledFromTimerOrInit = false)
proc startBuildingTokensTimer(self: Service, resetTimeToNow = true)
proc startFetchingHistoryTimer(self: Service, resetTimeToNow = true)
proc buildAllTokens(self: Service)
proc checkRecentHistory*(self: Service)
proc startWallet(self: Service)
proc delete*(self: Service) =
self.closingApp = true
@ -184,7 +180,9 @@ QtObject:
account.relatedAccounts = accounts.filter(x => not account.derivedFrom.isEmptyOrWhitespace and (cmpIgnoreCase(x.derivedFrom, account.derivedFrom) == 0))
self.walletAccounts[account.address] = account
self.buildAllTokens(true)
self.buildAllTokens()
self.checkRecentHistory()
self.startWallet()
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription
@ -200,17 +198,10 @@ QtObject:
self.events.on(SignalType.Wallet.event) do(e:Args):
var data = WalletSignal(e)
case data.eventType:
of "recent-history-ready":
# run timer again...
self.startFetchingHistoryTimer()
of "non-archival-node-detected":
# run timer again...
self.startFetchingHistoryTimer()
of "fetching-history-error":
# run timer again...
self.startFetchingHistoryTimer()
else:
echo "Unhandled wallet signal: ", data.eventType
of "wallet-tick-reload":
self.checkRecentHistory()
self.buildAllTokens()
proc getAccountByAddress*(self: Service, address: string): WalletAccountDto =
if not self.walletAccounts.hasKey(address):
@ -231,14 +222,16 @@ QtObject:
if(accounts[i].address == address):
return i
proc checkRecentHistory*(self: Service, calledFromTimerOrInit = false) =
proc startWallet(self: Service) =
if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
# Since we don't have a way to re-run TimerTaskArg (to stop it and run again), we introduced some flags which will
# just ignore buildAllTokens in case that proc is called by some action in the time window between two successive calls
# initiated by TimerTaskArg.
if not calledFromTimerOrInit:
self.ignoreTimeInitiatedHistoryFetchBuild = true
discard backend.startWallet()
proc checkRecentHistory*(self: Service) =
if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
try:
let addresses = self.getWalletAccounts().map(a => a.address)
let chainIds = self.networkService.getNetworks().map(a => a.chainId)
@ -450,32 +443,6 @@ QtObject:
data.error = e.msg
self.events.emit(SIGNAL_WALLET_ACCOUNT_DERIVED_ADDRESS_DETAILS_FETCHED, data)
proc onStartBuildingTokensTimer*(self: Service, response: string) {.slot.} =
if ((now().toTime().toUnix() - self.timerStartTimeInSeconds) < CheckBalanceSlotExecuteIntervalInSeconds):
self.startBuildingTokensTimer(resetTimeToNow = false)
return
if self.ignoreTimeInitiatedTokensBuild:
self.ignoreTimeInitiatedTokensBuild = false
return
self.buildAllTokens(true)
proc startBuildingTokensTimer(self: Service, resetTimeToNow = true) =
if(self.closingApp):
return
if (resetTimeToNow):
self.timerStartTimeInSeconds = now().toTime().toUnix()
let arg = TimerTaskArg(
tptr: cast[ByteAddress](timerTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onStartBuildingTokensTimer",
timeoutInMilliseconds: CheckBalanceTimerIntervalInMilliseconds
)
self.threadpool.start(arg)
proc onAllTokensBuilt*(self: Service, response: string) {.slot.} =
try:
let responseObj = response.parseJson
@ -495,19 +462,10 @@ QtObject:
except Exception as e:
error "error: ", procName="onAllTokensBuilt", errName = e.name, errDesription = e.msg
# run timer again...
self.startBuildingTokensTimer()
proc buildAllTokens(self: Service, calledFromTimerOrInit = false) =
if(self.closingApp or not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
proc buildAllTokens(self: Service) =
if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
# Since we don't have a way to re-run TimerTaskArg (to stop it and run again), we introduced some flags which will
# just ignore buildAllTokens in case that proc is called by some action in the time window between two successive calls
# initiated by TimerTaskArg.
if not calledFromTimerOrInit:
self.ignoreTimeInitiatedTokensBuild = true
let arg = BuildTokensTaskArg(
tptr: cast[ByteAddress](prepareTokensTask),
vptr: cast[ByteAddress](self.vptr),
@ -517,6 +475,8 @@ QtObject:
proc onIsWalletEnabledChanged*(self: Service) {.slot.} =
self.buildAllTokens()
self.checkRecentHistory()
self.startWallet()
proc getNetworkCurrencyBalance*(self: Service, network: NetworkDto): float64 =
for walletAccount in toSeq(self.walletAccounts.values):
@ -651,28 +611,4 @@ QtObject:
self.addNewAccountToLocalStore()
except Exception as e:
error "error: ", procName="deleteKeycard", errName = e.name, errDesription = e.msg
return "error: " & e.msg
proc onStartHistoryFetchingTimer*(self: Service, response: string) {.slot.} =
self.isHistoryFetchTimerAlreadyRunning = false
if self.ignoreTimeInitiatedHistoryFetchBuild:
self.ignoreTimeInitiatedHistoryFetchBuild = false
return
self.checkRecentHistory(true)
proc startFetchingHistoryTimer(self: Service, resetTimeToNow = true) =
if(self.closingApp or
not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled() or
self.isHistoryFetchTimerAlreadyRunning):
return
# TODO move this to status-go, because the 20 minutes timer leaves the app hanging when trying to leave
# self.isHistoryFetchTimerAlreadyRunning = true
# let arg = TimerTaskArg(
# tptr: cast[ByteAddress](timerTask),
# vptr: cast[ByteAddress](self.vptr),
# slot: "onStartHistoryFetchingTimer",
# timeoutInMilliseconds: CheckHistoryIntervalInMilliseconds
# )
# self.threadpool.start(arg)
return "error: " & e.msg

View File

@ -91,6 +91,9 @@ rpc(getPendingTransactionsByChainIDs, "wallet"):
rpc(getWalletToken, "wallet"):
discard
rpc(startWallet, "wallet"):
discard
rpc(getTransactionEstimatedTime, "wallet"):
chainId: int
maxFeePerGas: float

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 1501c4071734de0730849dd9af02f803c9d0da70
Subproject commit c735e2a6bbef9874173378121316798d3d6fe981