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

37 lines
835 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
# 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 =
%{ "to": %tx.to, "data": %tx.data }