From f33fcda9ef065dde20f64a4fd89040a50e5659f4 Mon Sep 17 00:00:00 2001 From: Djadih <13135116+Djadih@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:18:30 -0500 Subject: [PATCH] Implement marshaling for BlockNumber --- rpc/types.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rpc/types.go b/rpc/types.go index 4176922111..feaeecd358 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -98,6 +98,22 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { return nil } +// MarshalText implements encoding.TextMarshaler. It marshals: +// - "latest", "earliest" or "pending" as strings +// - other numbers as hex +func (bn BlockNumber) MarshalText() ([]byte, error) { + switch bn { + case EarliestBlockNumber: + return []byte("earliest"), nil + case LatestBlockNumber: + return []byte("latest"), nil + case PendingBlockNumber: + return []byte("pending"), nil + default: + return hexutil.Uint64(bn).MarshalText() + } +} + func (bn BlockNumber) Int64() int64 { return (int64)(bn) }