Files
zeekay 084841011e feat(types): the JSON-RPC boundary carries 16- and 256-bit numbers too
The set was Uint32, Uint64, Float64. Two widths the chain actually uses were
missing, so callers reached past this package for both.

Uint16 is what platformvm's API already asks for today.

Uint256 is the width the EVM carries. A wei value, a token balance or a
supply does not fit in 64 bits — an 18-decimal balance passes 2^64 at about
18.4 tokens — so every such field arriving here had to become a bare string
or a hand-rolled big.Int. It is uint256.Int rather than big.Int because a
fixed-size value cannot be nil and does not allocate; the module was already
in the graph.

Every numeric here is a QUOTED decimal, which is the point of the package: a
JSON number is a float64, and a uint64 above 2^53 does not survive one. The
test asserts Uint64 REFUSES a value it cannot hold, which is the failure
Uint256 exists to prevent.

Unquoting was written out once per type and is now read in one place. Adding
two more types would have made five copies of it.
2026-08-28 21:22:30 -07:00

119 lines
2.5 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package types
import (
"strconv"
"github.com/holiman/uint256"
)
const Null = "null"
// payload strips the quotes JSON-RPC numerics are carried in and says whether
// the value was null. Every type below reads a quoted decimal, so the reading
// is stated once; each type differs only in how it parses what comes out.
func payload(b []byte) (string, bool) {
s := string(b)
if s == Null {
return "", true
}
if len(s) >= 2 {
if last := len(s) - 1; s[0] == '"' && s[last] == '"' {
s = s[1:last]
}
}
return s, false
}
func quote(s string) []byte { return []byte(`"` + s + `"`) }
type Uint16 uint16
func (u Uint16) MarshalJSON() ([]byte, error) {
return quote(strconv.FormatUint(uint64(u), 10)), nil
}
func (u *Uint16) UnmarshalJSON(b []byte) error {
s, null := payload(b)
if null {
return nil
}
val, err := strconv.ParseUint(s, 10, 16)
*u = Uint16(val)
return err
}
type Uint32 uint32
func (u Uint32) MarshalJSON() ([]byte, error) {
return quote(strconv.FormatUint(uint64(u), 10)), nil
}
func (u *Uint32) UnmarshalJSON(b []byte) error {
s, null := payload(b)
if null {
return nil
}
val, err := strconv.ParseUint(s, 10, 32)
*u = Uint32(val)
return err
}
type Uint64 uint64
func (u Uint64) MarshalJSON() ([]byte, error) {
return quote(strconv.FormatUint(uint64(u), 10)), nil
}
func (u *Uint64) UnmarshalJSON(b []byte) error {
s, null := payload(b)
if null {
return nil
}
val, err := strconv.ParseUint(s, 10, 64)
*u = Uint64(val)
return err
}
// Uint256 is the width the EVM actually carries — a balance, a wei value or a
// token amount does not fit in 64 bits, and every such field reaching this
// boundary had to be a string or a big.Int by hand. It is fixed-size rather
// than big.Int so it cannot be nil and cannot allocate on the hot path.
type Uint256 uint256.Int
func (u Uint256) MarshalJSON() ([]byte, error) {
v := uint256.Int(u)
return quote(v.Dec()), nil
}
func (u *Uint256) UnmarshalJSON(b []byte) error {
s, null := payload(b)
if null {
return nil
}
v, err := uint256.FromDecimal(s)
if err != nil {
return err
}
*u = Uint256(*v)
return nil
}
type Float64 float64
func (f Float64) MarshalJSON() ([]byte, error) {
return quote(strconv.FormatFloat(float64(f), 'f', 4, 64)), nil
}
func (f *Float64) UnmarshalJSON(b []byte) error {
s, null := payload(b)
if null {
return nil
}
val, err := strconv.ParseFloat(s, 64)
*f = Float64(val)
return err
}