Skip to content

Commit

Permalink
fix: wallet connect - sake.lido.fi dApp - staking transaction gets stuck
Browse files Browse the repository at this point in the history
Fixes #16096
  • Loading branch information
saledjenic committed Sep 17, 2024
1 parent ff45e76 commit ea8827e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
45 changes: 33 additions & 12 deletions src/app/modules/shared_modules/wallet_connect/helpers.nim
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/app_service/common/json_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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

success

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

Expand Down
14 changes: 12 additions & 2 deletions test/nim/wallet_connect_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"}""")
10 changes: 4 additions & 6 deletions ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea8827e

Please sign in to comment.