fix(@wallet): move estimation time to status-go

This commit is contained in:
Anthony Laibe 2022-07-07 10:51:04 +02:00 committed by Anthony Laibe
parent 136921c0ad
commit ff12c85f07
11 changed files with 30 additions and 68 deletions

View File

@ -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)
proc getEstimatedTime*(self: Controller, chainId: int, maxFeePerGas: string): EstimatedTime =
return self.transactionService.getEstimatedTime(chainId, maxFeePerGas)

View File

@ -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

View File

@ -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
method getEstimatedTime*(self: Module, chainId: int, maxFeePerGas: string): int =
return self.controller.getEstimatedTime(chainId, maxFeePerGas).int

View File

@ -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)
proc getEstimatedTime*(self: View, chainId: int, maxFeePerGas: string): int {.slot.} =
return self.delegate.getEstimatedTime(chainId, maxFeePerGas)

View File

@ -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

View File

@ -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

View File

@ -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() {

View File

@ -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)
})
}

View File

@ -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

View File

@ -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")
}

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit f6c9ec7838b91aba325351bda2903f25a8ba5547
Subproject commit 35c4001e5727c3bd5f59995a4d1698ed2888f5f8