fix: wallet connect - sake.lido.fi dApp - staking transaction gets stuck

Fixes #16096
This commit is contained in:
Sale Djenic 2024-09-16 19:19:36 +02:00 committed by saledjenic
parent ff45e76782
commit ea8827ec97
4 changed files with 51 additions and 22 deletions

View File

@ -1,21 +1,42 @@
import stint, json, strutils
import stint, json, strutils, chronicles
include app_service/common/json_utils
proc hexToDec*(hex: string): string =
return stint.parse(hex, UInt256, 16).toString()
proc getFloatFromJson(jsonObj: JsonNode, key: string): float =
if jsonObj.contains(key):
case jsonObj[key].kind
of JFloat:
result = jsonObj[key].getFloat
of JString:
result = parseFloat(jsonObj[key].getStr)
of JInt:
result = float(jsonObj[key].getInt)
else:
raise newException(CatchableError, "cannot resolve value for key: " & key)
proc convertFeesInfoToHex*(feesInfoJson: string): string =
let parsedJson = parseJson(feesInfoJson)
try:
if feesInfoJson.len == 0:
raise newException(CatchableError, "feesInfoJson is empty")
let maxFeeFloat = parsedJson["maxFeePerGas"].getFloat()
let maxFeeWei = int64(maxFeeFloat * 1e9)
let
parsedJson = parseJson(feesInfoJson)
let maxPriorityFeeFloat = parsedJson["maxPriorityFeePerGas"].getFloat()
let maxPriorityFeeWei = int64(maxPriorityFeeFloat * 1e9)
maxFeePerGasFloat = getFloatFromJson(parsedJson, "maxFeePerGas")
a = maxFeePerGasFloat * 1e9
maxFeePerGasWei = uint64(maxFeePerGasFloat * 1e9)
maxPriorityFeePerGasFloat = getFloatFromJson(parsedJson, "maxPriorityFeePerGas")
maxPriorityFeePerGasWei = uint64(maxPriorityFeePerGasFloat * 1e9)
# Assemble the JSON and return it
var resultJson = %* {
"maxFeePerGas": "0x" & toHex(maxFeeWei).strip(chars = {'0'}, trailing = false),
"maxPriorityFeePerGas": "0x" & toHex(maxPriorityFeeWei).strip(chars = {'0'}, trailing = false)
"maxFeePerGas": "0x" & toHex(maxFeePerGasWei).strip(chars = {'0'}, trailing = false),
"maxPriorityFeePerGas": "0x" & toHex(maxPriorityFeePerGasWei).strip(chars = {'0'}, trailing = false)
}
return $resultJson
except Exception as e:
error "cannot convert fees info to hex: ", msg=e.msg

View File

@ -36,7 +36,7 @@ template getProp(obj: JsonNode, prop: string, value: var typedesc[uint64]): bool
template getProp(obj: JsonNode, prop: string, value: var typedesc[string]): bool =
var success = false
if (obj.kind == JObject and obj.contains(prop)):
if (obj.kind == JObject and obj.contains(prop) and obj[prop].kind == JString):
value = obj[prop].getStr
success = true
@ -44,7 +44,7 @@ template getProp(obj: JsonNode, prop: string, value: var typedesc[string]): bool
template getProp(obj: JsonNode, prop: string, value: var typedesc[float]): bool =
var success = false
if (obj.kind == JObject and obj.contains(prop)):
if (obj.kind == JObject and obj.contains(prop) and obj[prop].kind == JFloat):
value = obj[prop].getFloat
success = true

View File

@ -8,7 +8,17 @@ suite "wallet connect":
check(hexToDec("0x3") == "3")
check(hexToDec("f") == "15")
test "convertFeesInfoToHex":
const feesInfoJson = "{\"maxFees\":\"24528.282681\",\"maxFeePerGas\":1.168013461,\"maxPriorityFeePerGas\":0.036572351,\"gasPrice\":\"1.168013461\"}"
test "convertFloatFeesInfoToHex":
const feesInfoJson = "{\"maxFees\":24528.282681,\"maxFeePerGas\":1.168013461,\"maxPriorityFeePerGas\":0.036572351,\"gasPrice\":1.168013461}"
check(convertFeesInfoToHex(feesInfoJson) == """{"maxFeePerGas":"0x459E7895","maxPriorityFeePerGas":"0x22E0CBF"}""")
test "convertTextFeesInfoToHex":
const feesInfoJson = "{\"maxFees\":\"24528.282681\",\"maxFeePerGas\":\"1.168013461\",\"maxPriorityFeePerGas\":\"0.036572351\",\"gasPrice\":\"1.168013461\"}"
check(convertFeesInfoToHex(feesInfoJson) == """{"maxFeePerGas":"0x459E7895","maxPriorityFeePerGas":"0x22E0CBF"}""")
test "convertMixedTextAndFloatFeesInfoToHex":
const feesInfoJson = "{\"maxFees\":\"24528.282681\",\"maxFeePerGas\":1.168013461,\"maxPriorityFeePerGas\":\"0.036572351\",\"gasPrice\":\"1.168013461\"}"
check(convertFeesInfoToHex(feesInfoJson) == """{"maxFeePerGas":"0x459E7895","maxPriorityFeePerGas":"0x22E0CBF"}""")

View File

@ -484,13 +484,11 @@ SQUtils.QObject {
// Beware, the tx values are standard blockchain hex big number values; the fees values are nim's float64 values, hence the complex conversions
if (!!tx.maxFeePerGas && !!tx.maxPriorityFeePerGas) {
let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas)
const gasPriceInWei = BigOps.fromString(maxFeePerGasDec)
maxFeePerGas = hexToGwei(tx.maxFeePerGas)
maxPriorityFeePerGas = hexToGwei(tx.maxPriorityFeePerGas)
// TODO: check why we need to set gasPrice here and why if it's not checked we cannot send the tx and fees are unknown????
gasPrice = hexToGwei(tx.maxFeePerGas)
// Source fees info from the incoming transaction for when we process it
maxFeePerGas = root.store.hexToDec(tx.maxFeePerGas)
let maxPriorityFeePerGasDec = hexToGwei(tx.maxPriorityFeePerGas)
maxPriorityFeePerGas = maxPriorityFeePerGasDec
} else {
let fees = root.store.getSuggestedFees(chainId)
maxPriorityFeePerGas = fees.maxPriorityFeePerGas