Skip to content

Commit

Permalink
Replace eth_getBlockReceipts return type with Opt[T] instead of Optio…
Browse files Browse the repository at this point in the history
…n[T]

reason:
Option[T] failed to compile when using nim v2
it is related to ref object. But also hard to reproduce outside
combination of nim-json-serialization + nim-json-rpc + something
  • Loading branch information
jangko committed Feb 14, 2024
1 parent a67213a commit 4e49815
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
14 changes: 4 additions & 10 deletions tests/helpers/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions web3/conversions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import
faststreams/textio,
json_rpc/jsonmarshal,
json_serialization/std/options,
json_serialization/stew/results,
json_serialization,
./primitives,
./engine_api_types,
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion web3/eth_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4e49815

Please sign in to comment.