From ff12c85f070bd5647c70087064414ae29a4ea3f7 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 7 Jul 2022 10:51:04 +0200 Subject: [PATCH] fix(@wallet): move estimation time to status-go --- .../transactions/controller.nim | 4 +- .../transactions/io_interface.nim | 2 +- .../wallet_section/transactions/module.nim | 4 +- .../main/wallet_section/transactions/view.nim | 4 +- .../service/transaction/service.nim | 59 ++----------------- src/backend/backend.nim | 4 ++ ui/app/AppLayouts/stores/RootStore.qml | 4 +- ui/imports/shared/controls/GasSelector.qml | 2 +- ui/imports/utils/Constants.qml | 9 +-- ui/imports/utils/Utils.qml | 4 ++ vendor/status-go | 2 +- 11 files changed, 30 insertions(+), 68 deletions(-) diff --git a/src/app/modules/main/wallet_section/transactions/controller.nim b/src/app/modules/main/wallet_section/transactions/controller.nim index cd1319a49a..c88cf980ad 100644 --- a/src/app/modules/main/wallet_section/transactions/controller.nim +++ b/src/app/modules/main/wallet_section/transactions/controller.nim @@ -107,5 +107,5 @@ proc getChainIdForChat*(self: Controller): int = proc getChainIdForBrowser*(self: Controller): int = return self.networkService.getNetworkForBrowser().chainId -proc getEstimatedTime*(self: Controller, chainId: int, priorityFeePerGas: string, maxFeePerGas: string): EstimatedTime = - return self.transactionService.getEstimatedTime(chainId, priorityFeePerGas, maxFeePerGas) \ No newline at end of file +proc getEstimatedTime*(self: Controller, chainId: int, maxFeePerGas: string): EstimatedTime = + return self.transactionService.getEstimatedTime(chainId, maxFeePerGas) \ No newline at end of file diff --git a/src/app/modules/main/wallet_section/transactions/io_interface.nim b/src/app/modules/main/wallet_section/transactions/io_interface.nim index cca04e5da8..1df8934297 100644 --- a/src/app/modules/main/wallet_section/transactions/io_interface.nim +++ b/src/app/modules/main/wallet_section/transactions/io_interface.nim @@ -64,7 +64,7 @@ method getChainIdForChat*(self: AccessInterface): int = method getChainIdForBrowser*(self: AccessInterface): int = raise newException(ValueError, "No implementation available") -method getEstimatedTime*(self: AccessInterface, chainId: int, priorityFeePerGas: string, maxFeePerGas: string): int {.base.} = +method getEstimatedTime*(self: AccessInterface, chainId: int, maxFeePerGas: string): int {.base.} = raise newException(ValueError, "No implementation available") # View Delegate Interface diff --git a/src/app/modules/main/wallet_section/transactions/module.nim b/src/app/modules/main/wallet_section/transactions/module.nim index 0cad5ec552..44c0b6bee2 100644 --- a/src/app/modules/main/wallet_section/transactions/module.nim +++ b/src/app/modules/main/wallet_section/transactions/module.nim @@ -111,5 +111,5 @@ method getChainIdForChat*(self: Module): int = method getChainIdForBrowser*(self: Module): int = return self.controller.getChainIdForBrowser() -method getEstimatedTime*(self: Module, chainId: int, priorityFeePerGas: string, maxFeePerGas: string): int = - return self.controller.getEstimatedTime(chainId, priorityFeePerGas, maxFeePerGas).int \ No newline at end of file +method getEstimatedTime*(self: Module, chainId: int, maxFeePerGas: string): int = + return self.controller.getEstimatedTime(chainId, maxFeePerGas).int \ No newline at end of file diff --git a/src/app/modules/main/wallet_section/transactions/view.nim b/src/app/modules/main/wallet_section/transactions/view.nim index e3e9eb06ae..f9c55390e3 100644 --- a/src/app/modules/main/wallet_section/transactions/view.nim +++ b/src/app/modules/main/wallet_section/transactions/view.nim @@ -141,5 +141,5 @@ QtObject: proc getChainIdForBrowser*(self: View): int {.slot.} = return self.delegate.getChainIdForBrowser() - proc getEstimatedTime*(self: View, chainId: int, priorityFeePerGas: string, maxFeePerGas: string): int {.slot.} = - return self.delegate.getEstimatedTime(chainId, priorityFeePerGas, maxFeePerGas) \ No newline at end of file + proc getEstimatedTime*(self: View, chainId: int, maxFeePerGas: string): int {.slot.} = + return self.delegate.getEstimatedTime(chainId, maxFeePerGas) \ No newline at end of file diff --git a/src/app_service/service/transaction/service.nim b/src/app_service/service/transaction/service.nim index 1c674f4018..9d03a5dca9 100644 --- a/src/app_service/service/transaction/service.nim +++ b/src/app_service/service/transaction/service.nim @@ -35,10 +35,11 @@ const SIGNAL_TRANSACTION_SENT* = "transactionSent" type EstimatedTime* {.pure.} = enum - Unknown = -1 + Unknown = 0 LessThanOneMin LessThanThreeMins LessThanFiveMins + MoreThanFiveMins type TransactionMinedArgs* = ref object of Args @@ -396,58 +397,10 @@ QtObject: if gasPrice < myTip: numOfTransactionWithTipLessThanMine.inc - proc getEstimatedTime*(self: Service, chainId: int, priorityFeePerGas: string, maxFeePerGas: string): EstimatedTime = - let priorityFeePerGasF = priorityFeePerGas.parseFloat - let maxFeePerGasF = maxFeePerGas.parseFloat - var transactionsProcessed = 0 - var numOfTransactionWithTipLessThanMine = 0 - var latestBlockNumber: Option[Uint256] - var expectedBaseFeeForNextBlock: float + proc getEstimatedTime*(self: Service, chainId: int, maxFeePerGas: string): EstimatedTime = try: - let response = eth.getBlockByNumber(chainId, "latest", true) - if response.error.isNil: - let transactionsJson = response.result{"transactions"} - self.addToAllTransactionsAndSetNewMinMax(priorityFeePerGasF, numOfTransactionWithTipLessThanMine, transactionsJson) - transactionsProcessed = transactionsJson.len - latestBlockNumber = some(stint.fromHex(Uint256, response.result{"number"}.getStr)) - let latestBlockBaseFeePerGasUnparsed = $fromHex(Stuint[256], response.result{"baseFeePerGas"}.getStr) - let latestBlockBaseFeePerGas = parseFloat(wei2gwei(latestBlockBaseFeePerGasUnparsed)) - let latestBlockGasUsedUnparsed = $fromHex(Stuint[256], response.result{"gasUsed"}.getStr) - let latestBlockGasUsed = parseFloat(wei2gwei(latestBlockGasUsedUnparsed)) - let latestBlockGasLimitUnparsed = $fromHex(Stuint[256], response.result{"gasLimit"}.getStr) - let latestBlockGasLimit = parseFloat(wei2gwei(latestBlockGasLimitUnparsed)) - - let ratio = latestBlockGasUsed / latestBlockGasLimit * 0.01 - let maxFeeChange = latestBlockBaseFeePerGas * 0.125 - if(ratio > 50): - expectedBaseFeeForNextBlock = latestBlockBaseFeePerGas + maxFeeChange * (ratio - 50) * 0.01 - else: - expectedBaseFeeForNextBlock = latestBlockBaseFeePerGas - maxFeeChange * (50 - ratio) * 0.01 + let response = backend.getTransactionEstimatedTime(chainId, maxFeePerGas.parseFloat).result.getInt + return EstimatedTime(response) except Exception as e: - error "error fetching latest block", msg=e.msg - - if latestBlockNumber.isNone or - priorityFeePerGasF + expectedBaseFeeForNextBlock > maxFeePerGasF: - return EstimatedTime.Unknown - - var blockNumber = latestBlockNumber.get - while (transactionsProcessed < 100 and latestBlockNumber.get < blockNumber + 10): - blockNumber = blockNumber - 1 - try: - let hexPrevNum = "0x" & stint.toHex(blockNumber) - let response = getBlockByNumber(chainId, hexPrevNum, true) - let transactionsJson = response.result{"transactions"} - self.addToAllTransactionsAndSetNewMinMax(priorityFeePerGasF, numOfTransactionWithTipLessThanMine, transactionsJson) - transactionsProcessed += transactionsJson.len - except Exception as e: - error "error fetching block number", blockNumber=blockNumber, msg=e.msg - - let p = numOfTransactionWithTipLessThanMine / transactionsProcessed - if p > 0.5: - return EstimatedTime.LessThanOneMin - elif p > 0.35: - return EstimatedTime.LessThanThreeMins - elif p > 0.15: - return EstimatedTime.LessThanFiveMins - else: + error "Error estimating transaction time", message = e.msg return EstimatedTime.Unknown \ No newline at end of file diff --git a/src/backend/backend.nim b/src/backend/backend.nim index 8bcd70f6a5..6688210dab 100644 --- a/src/backend/backend.nim +++ b/src/backend/backend.nim @@ -104,6 +104,10 @@ rpc(toggleVisibleToken, "wallet"): chainId: int address: string +rpc(getTransactionEstimatedTime, "wallet"): + chainId: int + maxFeePerGas: float + rpc(fetchPrices, "wallet"): symbols: seq[string] currency: string diff --git a/ui/app/AppLayouts/stores/RootStore.qml b/ui/app/AppLayouts/stores/RootStore.qml index e58767ecd7..77e9f9e3c9 100644 --- a/ui/app/AppLayouts/stores/RootStore.qml +++ b/ui/app/AppLayouts/stores/RootStore.qml @@ -113,8 +113,8 @@ QtObject { return JSON.parse(walletSectionTransactions.suggestedFees(chainId)) } - function getEstimatedTime(chainId, priorityFeePerGas, maxFeePerGas) { - return walletSectionTransactions.getEstimatedTime(chainId, priorityFeePerGas, maxFeePerGas) + function getEstimatedTime(chainId, maxFeePerGas) { + return walletSectionTransactions.getEstimatedTime(chainId, maxFeePerGas) } function getChainIdForChat() { diff --git a/ui/imports/shared/controls/GasSelector.qml b/ui/imports/shared/controls/GasSelector.qml index b53c91784b..f109bcecbd 100644 --- a/ui/imports/shared/controls/GasSelector.qml +++ b/ui/imports/shared/controls/GasSelector.qml @@ -68,7 +68,7 @@ Item { let fiatValue = root.getFiatValue(ethValue, "ETH", root.defaultCurrency) selectedGasEthValue = ethValue selectedGasFiatValue = fiatValue - root.estimatedTxTimeFlag = root.getEstimatedTime(root.chainId, inputPerGasTipLimit.text, inputGasPrice.text) + root.estimatedTxTimeFlag = root.getEstimatedTime(root.chainId, inputGasPrice.text) }) } diff --git a/ui/imports/utils/Constants.qml b/ui/imports/utils/Constants.qml index b05aced8b8..d63ec9314a 100644 --- a/ui/imports/utils/Constants.qml +++ b/ui/imports/utils/Constants.qml @@ -204,10 +204,11 @@ QtObject { } readonly property QtObject transactionEstimatedTime: QtObject { - readonly property int unknown: -1 - readonly property int lessThanOneMin: 0 - readonly property int lessThanThreeMins: 1 - readonly property int lessThanFiveMins: 2 + readonly property int unknown: 0 + readonly property int lessThanOneMin: 1 + readonly property int lessThanThreeMins: 2 + readonly property int lessThanFiveMins: 3 + readonly property int moreThanFiveMins: 4 } readonly property int communityImported: 0 diff --git a/ui/imports/utils/Utils.qml b/ui/imports/utils/Utils.qml index c6d0ae9442..efc682d861 100644 --- a/ui/imports/utils/Utils.qml +++ b/ui/imports/utils/Utils.qml @@ -561,6 +561,10 @@ QtObject { /* Validation section end */ function getLabelForEstimatedTxTime(estimatedFlag) { + if (estimatedFlag === Constants.transactionEstimatedTime.unknown) { + return qsTr("Unknown") + } + if (estimatedFlag === Constants.transactionEstimatedTime.lessThanOneMin) { return qsTr("< 1 min") } diff --git a/vendor/status-go b/vendor/status-go index f6c9ec7838..35c4001e57 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit f6c9ec7838b91aba325351bda2903f25a8ba5547 +Subproject commit 35c4001e5727c3bd5f59995a4d1698ed2888f5f8