Documentation
¶
Overview ¶
Package fastabi provides low-level ABI encoding/decoding primitives for Ethereum.
Scope: Address, Hash, U256, Encoder, Decoder, ParamType, Kind. Domain types (Pool, Token, Edge, PoolUpdate, Opportunity, DEXType) live in internal/types — they are not part of this library.
Index ¶
- func BytesToHex(b []byte) string
- func Decode(ts []ParamType, data []byte) ([]any, error)
- func Decode1(t ParamType, data []byte) (any, error)
- func DecodeSignature(sig string) ([32]byte, error)
- func Encode(ts []ParamType, vals []any) []byte
- func Encode1(t ParamType, val any) []byte
- func HexToBytes(s string) ([]byte, error)
- func IsWhitespace(s string) bool
- func MustDecodeSignature(sig string) [32]byte
- func PaddedLen(n int) int
- func ParamNames(ts []ParamType) []string
- func ParseHexByte(hi, lo byte) (byte, error)
- func ParseHexUint64(s string) (uint64, error)
- func PutDecoder(d *Decoder)
- func PutEncoder(e *Encoder)
- func PutU256(u *U256)
- func RAYInt() *uint256.Int
- func ReadUint64At(data []byte, offset int) uint64
- type Address
- type Decoder
- func (d *Decoder) DecodeAddress() [20]byte
- func (d *Decoder) DecodeBigInt() *big.Int
- func (d *Decoder) DecodeBool() bool
- func (d *Decoder) DecodeBytes32() [32]byte
- func (d *Decoder) DecodeInt256() *big.Int
- func (d *Decoder) DecodeUint8() uint8
- func (d *Decoder) DecodeUint16() uint16
- func (d *Decoder) DecodeUint32() uint32
- func (d *Decoder) DecodeUint64() uint64
- func (d *Decoder) DecodeUint256() *U256
- func (d *Decoder) Len() int
- func (d *Decoder) Offset() int
- func (d *Decoder) Reset()
- func (d *Decoder) SetData(data []byte)
- func (d *Decoder) Skip(n int) bool
- type Encoder
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) EncodeAddress(addr [20]byte) *Encoder
- func (e *Encoder) EncodeBigInt(v *big.Int) *Encoder
- func (e *Encoder) EncodeBool(v bool) *Encoder
- func (e *Encoder) EncodeBytes32(v [32]byte) *Encoder
- func (e *Encoder) EncodeMethodID(id MethodID) *Encoder
- func (e *Encoder) EncodeUint8(v uint8) *Encoder
- func (e *Encoder) EncodeUint16(v uint16) *Encoder
- func (e *Encoder) EncodeUint24(v uint32) *Encoder
- func (e *Encoder) EncodeUint32(v uint32) *Encoder
- func (e *Encoder) EncodeUint64(v uint64) *Encoder
- func (e *Encoder) EncodeUint256(u *U256) *Encoder
- func (e *Encoder) Hex() string
- func (e *Encoder) Reset()
- type Hash
- type Kind
- type MethodID
- type ParamType
- func Named(name string, t ParamType) ParamType
- func ParseFunction(sig string) (name string, inputs []ParamType, err error)
- func ParseType(s string) (ParamType, error)
- func ParseTypes(s string) ([]ParamType, error)
- func TAddress() ParamType
- func TArray(elem ParamType) ParamType
- func TBool() ParamType
- func TBytes() ParamType
- func TBytes32() ParamType
- func TFixedArray(elem ParamType, n int) ParamType
- func TInt8() ParamType
- func TInt16() ParamType
- func TInt24() ParamType
- func TInt32() ParamType
- func TInt64() ParamType
- func TInt128() ParamType
- func TInt256() ParamType
- func TString() ParamType
- func TTuple(fields ...ParamType) ParamType
- func TUint8() ParamType
- func TUint16() ParamType
- func TUint24() ParamType
- func TUint32() ParamType
- func TUint64() ParamType
- func TUint128() ParamType
- func TUint256() ParamType
- type U256
- func GetU256() *U256
- func HALF_WAD() *U256
- func IntToU256(v int64) *U256
- func MaxU256() *U256
- func NewU64(v uint64) *U256
- func NewU256(v uint64) *U256
- func NewU256FromBig(b *big.Int) *U256
- func NewU256FromHex(s string) (*U256, error)
- func WAD() *U256
- func WadDiv(a, b *U256) *U256
- func WadLn(x *U256) *U256
- func WadMul(a, b *U256) *U256
- func (u *U256) Add(b *U256) *U256
- func (u *U256) Bytes() []byte
- func (u *U256) Bytes32() [32]byte
- func (u *U256) Clone() *U256
- func (u *U256) Cmp(b *U256) int
- func (u *U256) Div(b *U256) *U256
- func (u *U256) Eq(b *U256) bool
- func (u *U256) Gt(b *U256) bool
- func (u *U256) Hex() string
- func (u *U256) Inner() *uint256.Int
- func (u *U256) IsPositive() bool
- func (u *U256) IsZero() bool
- func (u *U256) Lt(b *U256) bool
- func (u *U256) Mul(b *U256) *U256
- func (u *U256) MulDiv(b, c *U256) *U256
- func (u *U256) MulDivRoundingUp(b, c *U256) *U256
- func (u *U256) Rsh(n uint) *U256
- func (u *U256) Set(b *U256) *U256
- func (u *U256) SetBytes(b []byte) *U256
- func (u *U256) SetBytes32(b [32]byte) *U256
- func (u *U256) SetUint64(v uint64) *U256
- func (u *U256) Sqrt() *U256
- func (u *U256) String() string
- func (u *U256) Sub(b *U256) *U256
- func (u *U256) ToBig() *big.Int
- func (u *U256) ToUint64() uint64
- func (u *U256) Uint64() uint64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToHex ¶
BytesToHex converts bytes to a hex string (without 0x prefix).
func DecodeSignature ¶
DecodeSignature hashes a function/event signature string into a 32-byte topic0.
func Encode ¶
Encode generic ABI values. Returns ABI-encoded bytes. Each value must match the corresponding ParamType.
func HexToBytes ¶
HexToBytes converts a hex string to bytes.
func IsWhitespace ¶
IsWhitespace checks if a string is only whitespace.
func MustDecodeSignature ¶
MustDecodeSignature hashes a signature string, panicking on error.
func ParamNames ¶
ParamNames extracts names from a ParamType tree.
func ParseHexByte ¶
ParseHexByte parses two hex characters into a byte.
func ParseHexUint64 ¶
ParseHexUint64 parses a hex string to uint64.
func ReadUint64At ¶
ReadUint64At reads a uint64 from the last 8 bytes of a 32-byte ABI word at the given offset.
Types ¶
type Address ¶
type Address [20]byte
Address is a 20-byte Ethereum address.
func AddressFromUint64 ¶
AddressFromUint64 creates an Address from a uint64 value (big-endian, right-aligned). Useful in tests and benchmarks to generate unique addresses cheaply.
func MustParseAddress ¶
MustParseAddress parses an address or panics.
func ParseAddress ¶
ParseAddress parses an address from hex string.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is a reusable ABI decoder for hot-path static types. Uses zero-copy SetData() and sync.Pool for minimal allocations.
func NewDecoder ¶
func NewDecoder() *Decoder
NewDecoder returns a pooled Decoder (alias for GetDecoder).
func (*Decoder) DecodeAddress ¶
DecodeAddress decodes an Ethereum address from ABI slot.
func (*Decoder) DecodeBigInt ¶
DecodeBigInt decodes unsigned uint256 into big.Int.
func (*Decoder) DecodeBool ¶
DecodeBool decodes bool from ABI slot.
func (*Decoder) DecodeBytes32 ¶
DecodeBytes32 decodes bytes32 from ABI slot.
func (*Decoder) DecodeInt256 ¶
DecodeInt256 decodes signed int256 (two's complement).
func (*Decoder) DecodeUint8 ¶
DecodeUint8 decodes uint8 from ABI slot.
func (*Decoder) DecodeUint16 ¶
DecodeUint16 decodes uint16 from ABI slot.
func (*Decoder) DecodeUint32 ¶
DecodeUint32 decodes uint32 from ABI slot.
func (*Decoder) DecodeUint64 ¶
DecodeUint64 decodes uint64 from ABI slot.
func (*Decoder) DecodeUint256 ¶
DecodeUint256 decodes uint256 from a 32-byte ABI slot.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is a reusable ABI encoder for hot-path static types. Uses sync.Pool for minimal allocations.
func NewEncoder ¶
func NewEncoder() *Encoder
NewEncoder returns a pooled Encoder (alias for GetEncoder).
func (*Encoder) EncodeAddress ¶
EncodeAddress encodes address into ABI slot.
func (*Encoder) EncodeBigInt ¶
EncodeBigInt encodes signed int256.
func (*Encoder) EncodeBool ¶
EncodeBool encodes bool into ABI slot.
func (*Encoder) EncodeBytes32 ¶
EncodeBytes32 encodes bytes32 into ABI slot.
func (*Encoder) EncodeMethodID ¶
EncodeMethodID encodes a 4-byte method ID.
func (*Encoder) EncodeUint8 ¶
EncodeUint8 encodes uint8 into ABI slot.
func (*Encoder) EncodeUint16 ¶
EncodeUint16 encodes uint16 into ABI slot.
func (*Encoder) EncodeUint24 ¶
EncodeUint24 encodes uint24 into ABI slot.
func (*Encoder) EncodeUint32 ¶
EncodeUint32 encodes uint32 into ABI slot.
func (*Encoder) EncodeUint64 ¶
EncodeUint64 encodes uint64 into ABI slot.
func (*Encoder) EncodeUint256 ¶
EncodeUint256 encodes uint256 into ABI slot.
type Hash ¶
type Hash [32]byte
Hash is a 32-byte Ethereum hash (transaction hash, block hash, topic, etc.).
type ParamType ¶
type ParamType struct {
Kind Kind
Size int // bit-width for uint/int, length for fixedArray/fixedBytes
Elem *ParamType // element type for arrays
TupleEl []ParamType
Name string // optional field name
}
ParamType describes a single ABI parameter.
func ParseFunction ¶
ParseFunction parses a function signature like "transfer(address,uint256)" or "balanceOf(address):(uint256)" and returns the function name and input types.
func ParseType ¶
ParseType parses a human-readable ABI type string into a ParamType. Examples: "uint256", "address", "(uint8,address,bytes)[]", "string", "bytes32[3]"
func ParseTypes ¶
ParseTypes parses a comma-separated list of types.
func TFixedArray ¶
func (ParamType) StaticSize ¶
StaticSize returns the byte size for static types, 0 for dynamic.
type U256 ¶
type U256 struct {
// contains filtered or unexported fields
}
U256 is a 256-bit unsigned integer for Ethereum-compatible operations. It embeds uint256.Int inline (32 bytes, no pointers) so structs containing U256 value fields (not pointers) are GC-friendly (noscan).
func HALF_WAD ¶
func HALF_WAD() *U256
HALF_WAD returns 5e17 as a read-only *U256. Do NOT mutate the returned value.
func NewU256FromBig ¶
NewU256FromBig creates U256 from big.Int.
func NewU256FromHex ¶
NewU256FromHex creates U256 from hex string.
func WAD ¶
func WAD() *U256
WAD returns 1e18 as a read-only *U256. Do NOT mutate the returned value.
func WadDiv ¶
WadDiv performs wad division: (a * WAD + b/2) / b Uses reusable scratch buffers to avoid allocations in hot paths.
func WadMul ¶
WadMul performs wad multiplication: (a * b + 0.5*WAD) / WAD Uses reusable scratch buffers to avoid allocations in hot paths.
func (*U256) MulDivRoundingUp ¶
MulDivRoundingUp calculates (u * b) / c with rounding up using scratch buffers.
func (*U256) SetBytes32 ¶
SetBytes32 sets the value from [32]byte array.