From ea8827ec97168ffe2df4588f1c138f9a948311c1 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Mon, 16 Sep 2024 19:19:36 +0200 Subject: [PATCH] fix: wallet connect - sake.lido.fi dApp - staking transaction gets stuck Fixes #16096 --- .../shared_modules/wallet_connect/helpers.nim | 45 ++++++++++++++----- src/app_service/common/json_utils.nim | 4 +- test/nim/wallet_connect_tests.nim | 14 +++++- .../services/dapps/DAppsRequestHandler.qml | 10 ++--- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/app/modules/shared_modules/wallet_connect/helpers.nim b/src/app/modules/shared_modules/wallet_connect/helpers.nim index 6927b26a4d5..d6f4adbe761 100644 --- a/src/app/modules/shared_modules/wallet_connect/helpers.nim +++ b/src/app/modules/shared_modules/wallet_connect/helpers.nim @@ -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) - # 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) - } - return $resultJson + maxPriorityFeePerGasFloat = getFloatFromJson(parsedJson, "maxPriorityFeePerGas") + maxPriorityFeePerGasWei = uint64(maxPriorityFeePerGasFloat * 1e9) + # Assemble the JSON and return it + var resultJson = %* { + "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 diff --git a/src/app_service/common/json_utils.nim b/src/app_service/common/json_utils.nim index 2531effa4ad..745b8bd92f8 100644 --- a/src/app_service/common/json_utils.nim +++ b/src/app_service/common/json_utils.nim @@ -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 diff --git a/test/nim/wallet_connect_tests.nim b/test/nim/wallet_connect_tests.nim index 2cf6db4a924..7514e1dd6b3 100644 --- a/test/nim/wallet_connect_tests.nim +++ b/test/nim/wallet_connect_tests.nim @@ -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"}""") diff --git a/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml b/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml index 87f5ba507a3..e4829d0b6c4 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml @@ -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