Skip to content

Commit

Permalink
Implement marshaling for BlockNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Apr 26, 2023
1 parent 74b6e65 commit f33fcda
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rpc/types.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit f33fcda

Please sign in to comment.