Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All functions in this file should have this check and obj[prop].kind == .... but when I did that I couldn't start the app (got crash) which undoubtedly means that in some places we have incorrect type usage or receiving incorrect json data. That should be fixed in another PR.

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we are ine post eip1559 case, both are present, we do not need to set gas price i believe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, that was my question about, here.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why gasPrice is used in this context (refer to TODO in the line above)?

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