fix: wallet connect - sake.lido.fi dApp - staking transaction gets stuck
Fixes #16096
This commit is contained in:
parent
ff45e76782
commit
ea8827ec97
|
@ -1,21 +1,42 @@
|
||||||
import stint, json, strutils
|
import stint, json, strutils, chronicles
|
||||||
|
|
||||||
|
include app_service/common/json_utils
|
||||||
|
|
||||||
proc hexToDec*(hex: string): string =
|
proc hexToDec*(hex: string): string =
|
||||||
return stint.parse(hex, UInt256, 16).toString()
|
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 =
|
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
|
||||||
let maxFeeWei = int64(maxFeeFloat * 1e9)
|
parsedJson = parseJson(feesInfoJson)
|
||||||
|
|
||||||
let maxPriorityFeeFloat = parsedJson["maxPriorityFeePerGas"].getFloat()
|
maxFeePerGasFloat = getFloatFromJson(parsedJson, "maxFeePerGas")
|
||||||
let maxPriorityFeeWei = int64(maxPriorityFeeFloat * 1e9)
|
a = maxFeePerGasFloat * 1e9
|
||||||
|
maxFeePerGasWei = uint64(maxFeePerGasFloat * 1e9)
|
||||||
|
|
||||||
|
maxPriorityFeePerGasFloat = getFloatFromJson(parsedJson, "maxPriorityFeePerGas")
|
||||||
|
maxPriorityFeePerGasWei = uint64(maxPriorityFeePerGasFloat * 1e9)
|
||||||
|
|
||||||
# Assemble the JSON and return it
|
# Assemble the JSON and return it
|
||||||
var resultJson = %* {
|
var resultJson = %* {
|
||||||
"maxFeePerGas": "0x" & toHex(maxFeeWei).strip(chars = {'0'}, trailing = false),
|
"maxFeePerGas": "0x" & toHex(maxFeePerGasWei).strip(chars = {'0'}, trailing = false),
|
||||||
"maxPriorityFeePerGas": "0x" & toHex(maxPriorityFeeWei).strip(chars = {'0'}, trailing = false)
|
"maxPriorityFeePerGas": "0x" & toHex(maxPriorityFeePerGasWei).strip(chars = {'0'}, trailing = false)
|
||||||
}
|
}
|
||||||
return $resultJson
|
return $resultJson
|
||||||
|
except Exception as e:
|
||||||
|
error "cannot convert fees info to hex: ", msg=e.msg
|
||||||
|
|
|
@ -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 =
|
template getProp(obj: JsonNode, prop: string, value: var typedesc[string]): bool =
|
||||||
var success = false
|
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
|
value = obj[prop].getStr
|
||||||
success = true
|
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 =
|
template getProp(obj: JsonNode, prop: string, value: var typedesc[float]): bool =
|
||||||
var success = false
|
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
|
value = obj[prop].getFloat
|
||||||
success = true
|
success = true
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,17 @@ suite "wallet connect":
|
||||||
check(hexToDec("0x3") == "3")
|
check(hexToDec("0x3") == "3")
|
||||||
check(hexToDec("f") == "15")
|
check(hexToDec("f") == "15")
|
||||||
|
|
||||||
test "convertFeesInfoToHex":
|
test "convertFloatFeesInfoToHex":
|
||||||
const feesInfoJson = "{\"maxFees\":\"24528.282681\",\"maxFeePerGas\":1.168013461,\"maxPriorityFeePerGas\":0.036572351,\"gasPrice\":\"1.168013461\"}"
|
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"}""")
|
check(convertFeesInfoToHex(feesInfoJson) == """{"maxFeePerGas":"0x459E7895","maxPriorityFeePerGas":"0x22E0CBF"}""")
|
||||||
|
|
|
@ -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
|
// 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) {
|
if (!!tx.maxFeePerGas && !!tx.maxPriorityFeePerGas) {
|
||||||
let maxFeePerGasDec = root.store.hexToDec(tx.maxFeePerGas)
|
maxFeePerGas = hexToGwei(tx.maxFeePerGas)
|
||||||
const gasPriceInWei = BigOps.fromString(maxFeePerGasDec)
|
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)
|
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 {
|
} else {
|
||||||
let fees = root.store.getSuggestedFees(chainId)
|
let fees = root.store.getSuggestedFees(chainId)
|
||||||
maxPriorityFeePerGas = fees.maxPriorityFeePerGas
|
maxPriorityFeePerGas = fees.maxPriorityFeePerGas
|
||||||
|
|
Loading…
Reference in New Issue