nim-ethers/ethers/providers/rpccalls/conversions.nim

45 lines
991 B
Nim
Raw Normal View History

2022-01-18 11:42:58 +00:00
import std/json
2022-01-20 11:56:18 +00:00
import pkg/stew/byteutils
2022-01-18 11:42:58 +00:00
import ../../basics
2022-01-20 11:56:18 +00:00
import ../../transaction
2022-01-24 11:14:31 +00:00
import ../../blocktag
2022-01-20 11:56:18 +00:00
# byte sequence
func `%`*(bytes: seq[byte]): JsonNode =
%("0x" & bytes.toHex)
func fromJson*(json: JsonNode, name: string, result: var seq[byte]) =
result = hexToSeqByte(json.getStr())
2022-01-18 11:42:58 +00:00
2022-01-18 13:26:41 +00:00
# Address
2022-01-18 11:42:58 +00:00
func `%`*(address: Address): JsonNode =
%($address)
2022-01-18 13:26:41 +00:00
func fromJson*(json: JsonNode, name: string, result: var Address) =
2022-01-18 11:42:58 +00:00
if address =? Address.init(json.getStr()):
result = address
else:
2022-01-18 13:26:41 +00:00
raise newException(ValueError, "\"" & name & "\"is not an Address")
# UInt256
func `%`*(integer: UInt256): JsonNode =
%toHex(integer)
func fromJson*(json: JsonNode, name: string, result: var UInt256) =
result = UInt256.fromHex(json.getStr())
2022-01-20 11:56:18 +00:00
# Transaction
func `%`*(tx: Transaction): JsonNode =
2022-01-24 13:40:47 +00:00
result = %{ "to": %tx.to, "data": %tx.data }
if sender =? tx.sender:
result["from"] = %sender
2022-01-24 11:14:31 +00:00
# BlockTag
func `%`*(blockTag: BlockTag): JsonNode =
%($blockTag)