diff --git a/tests/helpers/handlers.nim b/tests/helpers/handlers.nim index b3818ca..7225ae6 100644 --- a/tests/helpers/handlers.nim +++ b/tests/helpers/handlers.nim @@ -81,16 +81,10 @@ proc installHandlers*(server: RpcServer) = if x != "-1".JsonString: result = decodeFromString(x, Quantity) - when NimMajor >= 2: - server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> JsonString: - # TODO: cannot prove obj is not nil - let jsonBytes = JrpcConv.decode(x.string, string) - return jsonBytes.JsonString - else: - server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> Option[seq[ReceiptObject]]: - if x != "-1".JsonString: - let r = decodeFromString(x, Option[seq[ReceiptObject]]) - return r + server.rpc("eth_getBlockReceipts") do(x: JsonString, blockId: RtBlockIdentifier) -> Opt[seq[ReceiptObject]]: + if x != "-1".JsonString: + let r = decodeFromString(x, Opt[seq[ReceiptObject]]) + return r server.rpc("eth_getBlockByNumber") do(x: JsonString, blockId: RtBlockIdentifier, fullTransactions: bool) -> BlockObject: var blk: BlockObject diff --git a/web3/conversions.nim b/web3/conversions.nim index b6b5556..e077b9d 100644 --- a/web3/conversions.nim +++ b/web3/conversions.nim @@ -14,6 +14,7 @@ import faststreams/textio, json_rpc/jsonmarshal, json_serialization/std/options, + json_serialization/stew/results, json_serialization, ./primitives, ./engine_api_types, @@ -364,6 +365,26 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: SyncingStatus) else: w.writeValue(v.syncObject) +# Somehow nim2 refuse to generate automatically +proc readValue*(r: var JsonReader[JrpcConv], val: var Opt[seq[ReceiptObject]]) + {.gcsafe, raises: [IOError, SerializationError].} = + mixin readValue + + if r.tokKind == JsonValueKind.Null: + reset val + r.parseNull() + else: + val.ok r.readValue(seq[ReceiptObject]) + +proc writeValue*(w: var JsonWriter[JrpcConv], v: Opt[seq[ReceiptObject]]) + {.gcsafe, raises: [IOError].} = + mixin writeValue + + if v.isOk: + w.writeValue v.get + else: + w.writeValue JsonString("null") + func `$`*(v: Quantity): string {.inline.} = encodeQuantity(v.uint64) diff --git a/web3/eth_api.nim b/web3/eth_api.nim index fa3ccaa..b9ff903 100644 --- a/web3/eth_api.nim +++ b/web3/eth_api.nim @@ -39,7 +39,7 @@ createRpcSigsFromNim(RpcClient): proc eth_getTransactionCount(data: Address, blockId: BlockIdentifier): Quantity proc eth_getBlockTransactionCountByHash(data: BlockHash): Quantity proc eth_getBlockTransactionCountByNumber(blockId: BlockIdentifier): Quantity - proc eth_getBlockReceipts(blockId: BlockIdentifier): Option[seq[ReceiptObject]] + proc eth_getBlockReceipts(blockId: BlockIdentifier): Opt[seq[ReceiptObject]] proc eth_getUncleCountByBlockHash(data: BlockHash): Quantity proc eth_getUncleCountByBlockNumber(blockId: BlockIdentifier): Quantity proc eth_getCode(data: Address, blockId: BlockIdentifier): seq[byte]