status-lib/status/statusgo_backend/conversions.nim

35 lines
1.1 KiB
Nim
Raw Normal View History

2021-09-08 18:05:39 +00:00
import
json, options, strutils
import
web3/[conversions, ethtypes], stint
2021-09-10 17:27:49 +00:00
import ../types/transaction
2021-09-08 18:05:39 +00:00
# TODO: make this public in nim-web3 lib
template stripLeadingZeros*(value: string): string =
var cidx = 0
# ignore the last character so we retain '0' on zero value
while cidx < value.len - 1 and value[cidx] == '0':
cidx.inc
value[cidx .. ^1]
2021-09-10 17:27:49 +00:00
proc `%`*(x: TransactionData): JsonNode =
2021-09-08 18:05:39 +00:00
result = newJobject()
result["from"] = %x.source
2021-09-10 17:27:49 +00:00
result["type"] = %x.txType
2021-09-08 18:05:39 +00:00
if x.to.isSome:
result["to"] = %x.to.unsafeGet
if x.gas.isSome:
result["gas"] = %x.gas.unsafeGet
if x.gasPrice.isSome:
result["gasPrice"] = %("0x" & x.gasPrice.unsafeGet.toHex.stripLeadingZeros)
2021-09-10 17:27:49 +00:00
if x.maxFeePerGas.isSome:
result["maxFeePerGas"] = %("0x" & x.maxFeePerGas.unsafeGet.toHex)
if x.maxPriorityFeePerGas.isSome:
result["maxPriorityFeePerGas"] = %("0x" & x.maxPriorityFeePerGas.unsafeGet.toHex)
2021-09-08 18:05:39 +00:00
if x.value.isSome:
result["value"] = %("0x" & x.value.unsafeGet.toHex)
result["data"] = %x.data
if x.nonce.isSome:
result["nonce"] = %x.nonce.unsafeGet