fix(@desktop/wallet): Calculate block number approximation (#11522)

This commit is contained in:
Cuteivist 2023-07-18 12:10:01 +02:00 committed by GitHub
parent 4b6066c955
commit 313e5a5b69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 4 deletions

View File

@ -86,4 +86,7 @@ method onAddAccountModuleLoaded*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method destroyAddAccountPopup*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getNetworkLayer*(self: AccessInterface, chainId: int): string {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -325,3 +325,6 @@ method getAddAccountModule*(self: Module): QVariant =
method onAddAccountModuleLoaded*(self: Module) =
self.view.emitDisplayAddAccountPopup()
method getNetworkLayer*(self: Module, chainId: int): string =
return self.networksModule.getNetworkLayer(chainId)

View File

@ -23,4 +23,7 @@ method setNetworksState*(self: AccessInterface, chainIds: seq[int], enable: bool
raise newException(ValueError, "No implementation available")
method refreshNetworks*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getNetworkLayer*(self: AccessInterface, chainId: int): string {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -61,4 +61,7 @@ method viewDidLoad*(self: Module) =
self.checkIfModuleDidLoad()
method setNetworksState*(self: Module, chainIds: seq[int], enabled: bool) =
self.controller.setNetworksState(chainIds, enabled)
self.controller.setNetworksState(chainIds, enabled)
method getNetworkLayer*(self: Module, chainId: int): string =
return self.view.getNetworkLayer(chainId)

View File

@ -139,3 +139,6 @@ proc networkEnabledToUxEnabledState(enabled: bool, allEnabled: bool): UxEnabledS
proc areAllEnabled(networks: seq[NetworkDto]): bool =
return networks.allIt(it.enabled)
proc getNetworkLayer*(self: View, chainId: int): string =
return self.all.getNetworkLayer(chainId)

View File

@ -70,4 +70,7 @@ method getLatestBlockNumber*(self: AccessInterface, chainId: int): string {.base
raise newException(ValueError, "No implementation available")
method transactionsToItems*(self: AccessInterface, transactions: seq[TransactionDto], collectibles: seq[CollectibleDto]): seq[Item] {.base.} =
raise newException(ValueError, "No implementation available")
method getNetworkLayer*(self: AccessInterface, chainId: int): string {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -143,3 +143,6 @@ method fetchDecodedTxData*(self: Module, txHash: string, data: string) =
method txDecoded*(self: Module, txHash: string, dataDecoded: string) =
self.view.txDecoded(txHash, dataDecoded)
method getNetworkLayer*(self: Module, chainId: int): string =
return self.delegate.getNetworkLayer(chainId)

View File

@ -1,12 +1,20 @@
import NimQml, tables, stint, json, strformat, sequtils, strutils, sugar
import NimQml, tables, stint, json, strformat, sequtils, strutils, sugar, times, math
import ./item
import ./model
import ./io_interface
import ../../../../global/global_singleton
import ../../../../../app_service/common/conversion as common_conversion
import ../../../../../app_service/service/wallet_account/dto
type
LatestBlockData* = ref object of RootObj
blockNumber*: int
datetime*: DateTime
isLayer1*: bool
QtObject:
type
View* = ref object of QObject
@ -18,6 +26,7 @@ QtObject:
enabledChainIds: seq[int]
isNonArchivalNode: bool
tempAddress: string
latestBlockNumbers: Table[int, LatestBlockData]
proc delete*(self: View) =
self.model.delete
@ -138,8 +147,33 @@ QtObject:
proc getChainIdForBrowser*(self: View): int {.slot.} =
return self.delegate.getChainIdForBrowser()
proc getLatestBlockNumber*(self: View, chainId: int): string {.slot.} =
return self.delegate.getLatestBlockNumber(chainId)
proc getLatestBlockNumber*(self: View, chainId: int): int {.slot.} =
if self.latestBlockNumbers.hasKey(chainId):
let blockData = self.latestBlockNumbers[chainId]
let timeDiff = (now() - blockData.datetime)
var multiplier = -1.0
if blockData.isLayer1:
multiplier = 0.083 # 1 every 12 seconds
else:
case chainId:
of 10: # Optimism
multiplier = 0.5 # 1 every 2 second
of 42161: # Arbitrum
multiplier = 3.96 # around 4 every 1 second
else:
multiplier = -1.0
if multiplier >= 0.0 and timeDiff < initDuration(minutes = 10):
let blockNumDiff = inSeconds(timeDiff).float64 * multiplier
return (blockData.blockNumber + (int)round(blockNumDiff))
let latestBlockNumber = self.delegate.getLatestBlockNumber(chainId)
let latestBlockNumberNumber = parseInt(singletonInstance.utils.hex2Dec(latestBlockNumber))
self.latestBlockNumbers[chainId] = LatestBlockData(
blockNumber: latestBlockNumberNumber,
datetime: now(),
isLayer1: self.delegate.getNetworkLayer(chainId) == "1"
)
return latestBlockNumberNumber
proc setPendingTx*(self: View, pendingTx: seq[Item]) =
for tx in pendingTx: