abi

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 15 Imported by: 16

Documentation

Index

Constants

View Source
const WordLength = 32

WordLength is the number of bytes in an EVM word.

Variables

View Source
var (
	// MaxUint contains the maximum unsigned integer for each bit size.
	MaxUint = map[int]*big.Int{}

	// MaxInt contains the maximum signed integer for each bit size.
	MaxInt = map[int]*big.Int{}

	// MinInt contains the minimum signed integer for each bit size.
	MinInt = map[int]*big.Int{}
)
View Source
var Default = NewABI()

Default is the default ABI instance that is used by the package-level functions.

It is recommended to create a new ABI instance using NewABI rather than modifying the default instance, as this can potentially interfere with other packages that use the default ABI instance.

View Source
var Panic = NewError("Panic", NewTupleType(TupleTypeElem{Name: "error", Type: NewUintType(256)}))

Panic is the Error instance for panic responses.

View Source
var Revert = NewError("Error", NewTupleType(TupleTypeElem{Name: "error", Type: NewStringType()}))

Revert is the Error instance for revert responses.

Functions

func DecodePanic

func DecodePanic(data []byte) *big.Int

DecodePanic decodes the panic data returned by contract calls. If the data is not a valid panic message, it returns nil.

func DecodeRevert

func DecodeRevert(data []byte) string

DecodeRevert decodes the revert data returned by contract calls. If the data is not a valid revert message, it returns an empty string.

func DecodeValue

func DecodeValue(t Type, abi []byte, val any) error

DecodeValue decodes the given ABI-encoded data into the given value. Value must be a pointer to a struct or a map.

func DecodeValues

func DecodeValues(t Type, abi []byte, vals ...any) error

DecodeValues decodes the given ABI-encoded data into the given values. The t type must be a tuple type.

func EncodeValue

func EncodeValue(t Type, val any) ([]byte, error)

EncodeValue encodes a value to ABI encoding.

func EncodeValues

func EncodeValues(t Type, vals ...any) ([]byte, error)

EncodeValues encodes a list of values to ABI encoding. The t type must be a tuple type.

func IsPanic

func IsPanic(data []byte) bool

IsPanic returns true if the data has the panic prefix. It does not check whether the data is a valid panic message.

func IsRevert

func IsRevert(data []byte) bool

IsRevert returns true if the data has the revert prefix. It does not check whether the data is a valid revert message.

func MustEncodeValue

func MustEncodeValue(t Type, val any) []byte

MustEncodeValue is like EncodeValue but panics on error.

func MustEncodeValues

func MustEncodeValues(t Type, vals ...any) []byte

MustEncodeValues is like EncodeValues but panics on error.

Types

type ABI

type ABI struct {
	// Types is a map of known ABI types.
	// The key is the name of the type, and the value is the type.
	Types map[string]Type

	// Mapper is used to map values to and from ABI types.
	Mapper Mapper
}

ABI structure implements the Ethereum ABI (Application Binary Interface).

It provides methods for working with ABI, such as parsing, encoding and decoding data.

The package provides default ABI instance that is used by the package-level functions. It is possible to create custom ABI instances and use them instead of the default one.

func NewABI

func NewABI() *ABI

NewABI creates a new ABI instance.

For most use cases, the default ABI instance should be used instead of creating a new one.

func (*ABI) DecodeValue

func (a *ABI) DecodeValue(t Type, abi []byte, val any) error

DecodeValue decodes the given ABI-encoded data into the given value. Value must be a pointer to a struct or a map.

func (*ABI) DecodeValues

func (a *ABI) DecodeValues(t Type, abi []byte, vals ...any) error

DecodeValues decodes the given ABI-encoded data into the given values. The t type must be a tuple type.

func (*ABI) EncodeValue

func (a *ABI) EncodeValue(t Type, val any) ([]byte, error)

EncodeValue encodes a value to ABI encoding.

func (*ABI) EncodeValues

func (a *ABI) EncodeValues(t Type, vals ...any) ([]byte, error)

EncodeValues encodes a list of values to ABI encoding. The t type must be a tuple type.

func (*ABI) NewConstructor

func (a *ABI) NewConstructor(inputs *TupleType) *Constructor

NewConstructor creates a new Constructor instance.

func (*ABI) NewError

func (a *ABI) NewError(name string, inputs *TupleType) *Error

NewError creates a new Error instance.

func (*ABI) NewEvent

func (a *ABI) NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event

NewEvent creates a new Event instance.

func (*ABI) NewMethod

func (a *ABI) NewMethod(name string, inputs, outputs *TupleType) *Method

NewMethod creates a new Method instance.

func (*ABI) ParseConstructor

func (a *ABI) ParseConstructor(signature string) (*Constructor, error)

ParseConstructor parses a constructor signature and returns a new Constructor.

See ParseConstructor for more information.

func (*ABI) ParseError

func (a *ABI) ParseError(signature string) (*Error, error)

ParseError parses an error signature and returns a new Error.

See ParseError for more information.

func (*ABI) ParseEvent

func (a *ABI) ParseEvent(signature string) (*Event, error)

ParseEvent parses an event signature and returns a new Event.

See ParseEvent for more information.

func (*ABI) ParseJSON

func (a *ABI) ParseJSON(data []byte) (*Contract, error)

ParseJSON parses the given ABI JSON and returns a Contract instance.

func (*ABI) ParseMethod

func (a *ABI) ParseMethod(signature string) (*Method, error)

ParseMethod parses a method signature and returns a new Method.

See ParseMethod for more information.

func (*ABI) ParseSignatures

func (a *ABI) ParseSignatures(signatures ...string) (*Contract, error)

ParseSignatures parses list of signatures and returns a Contract instance. Signatures must be prefixed with the kind, e.g. "constructor" or "event". For functions, the "function" prefix can be omitted.

func (*ABI) ParseStruct added in v0.1.1

func (a *ABI) ParseStruct(definition string) (Type, error)

ParseStruct parses a struct definition and returns a new Type.

See ParseStruct for more information.

func (*ABI) ParseType

func (a *ABI) ParseType(signature string) (Type, error)

ParseType parses a type signature and returns a new Type.

See ParseType for more information.

type AddressType

type AddressType struct{}

AddressType represents an address type.

func NewAddressType

func NewAddressType() *AddressType

NewAddressType creates a new "address" type.

func (*AddressType) CanonicalType

func (a *AddressType) CanonicalType() string

CanonicalType implements the Type interface.

func (*AddressType) String

func (a *AddressType) String() string

String implements the Type interface.

func (*AddressType) Value

func (a *AddressType) Value() Value

Value implements the Type interface.

type AddressValue

type AddressValue types.Address

AddressValue is a value of address type.

During encoding, the AddressValue can be mapped to the types.Address type, string as a hex-encoded address. For other types, the rules for []byte slice described in the documentation of anymapper package are used.

func (*AddressValue) Address

func (a *AddressValue) Address() types.Address

Address returns the address value.

func (*AddressValue) DecodeABI

func (a *AddressValue) DecodeABI(words Words) (int, error)

DecodeABI implements the Value interface.

func (*AddressValue) EncodeABI

func (a *AddressValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*AddressValue) IsDynamic

func (a *AddressValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*AddressValue) MapFrom

func (a *AddressValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*AddressValue) MapTo

func (a *AddressValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

func (*AddressValue) SetAddress

func (a *AddressValue) SetAddress(v types.Address)

SetAddress sets the value of the AddressValue.

type AliasType

type AliasType struct {
	// contains filtered or unexported fields
}

AliasType wraps another type and gives it a different type name. The canonical type name is the same as the wrapped type.

func NewAliasType

func NewAliasType(alias string, typ Type) *AliasType

NewAliasType creates a new alias type.

func (*AliasType) CanonicalType

func (a *AliasType) CanonicalType() string

CanonicalType implements the Type interface.

func (*AliasType) String

func (a *AliasType) String() string

String implements the Type interface.

func (*AliasType) Value

func (a *AliasType) Value() Value

Value implements the Type interface.

type ArrayType

type ArrayType struct {
	// contains filtered or unexported fields
}

ArrayType represents an unbounded array type.

func NewArrayType

func NewArrayType(typ Type) *ArrayType

NewArrayType creates a dynamic array type with the given element type.

func (*ArrayType) CanonicalType

func (a *ArrayType) CanonicalType() string

CanonicalType implements the Type interface.

func (*ArrayType) ElementType

func (a *ArrayType) ElementType() Type

ElementType returns the type of the array elements.

func (*ArrayType) String

func (a *ArrayType) String() string

String implements the Type interface.

func (*ArrayType) Value

func (a *ArrayType) Value() Value

Value implements the Type interface.

type ArrayValue

type ArrayValue struct {
	Elems []Value
	Type  Type
}

ArrayValue is a value of array type.

During encoding, the ArrayValue can be mapped from a slice or an array.

During decoding the ArrayValue is mapped to a slice or an array of the same size.

func (*ArrayValue) DecodeABI

func (a *ArrayValue) DecodeABI(data Words) (int, error)

DecodeABI implements the Value interface.

func (*ArrayValue) EncodeABI

func (a *ArrayValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*ArrayValue) IsDynamic

func (a *ArrayValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*ArrayValue) MapFrom

func (a *ArrayValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*ArrayValue) MapTo

func (a *ArrayValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

type BoolType

type BoolType struct{}

BoolType represents a boolean type.

func NewBoolType

func NewBoolType() *BoolType

NewBoolType creates a new "bool" type.

func (*BoolType) CanonicalType

func (b *BoolType) CanonicalType() string

CanonicalType implements the Type interface.

func (*BoolType) String

func (b *BoolType) String() string

String implements the Type interface.

func (*BoolType) Value

func (b *BoolType) Value() Value

Value implements the Type interface.

type BoolValue

type BoolValue bool

BoolValue is a value of bool type.

During encoding and decoding, the BoolValue is mapped using the bool rules described in the documentation of anymapper package.

func (*BoolValue) DecodeABI

func (b *BoolValue) DecodeABI(words Words) (int, error)

DecodeABI implements the Value interface.

func (*BoolValue) EncodeABI

func (b *BoolValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*BoolValue) IsDynamic

func (b *BoolValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*BoolValue) MapFrom

func (b *BoolValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*BoolValue) MapTo

func (b *BoolValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

func (*BoolValue) SetBool

func (b *BoolValue) SetBool(v bool)

SetBool sets the value of the BoolValue.

type BytesType

type BytesType struct{}

BytesType represents a bytes type.

func NewBytesType

func NewBytesType() *BytesType

NewBytesType creates a new "bytes" type.

func (*BytesType) CanonicalType

func (b *BytesType) CanonicalType() string

CanonicalType implements the Type interface.

func (*BytesType) String

func (b *BytesType) String() string

String implements the Type interface.

func (*BytesType) Value

func (b *BytesType) Value() Value

Value implements the Type interface.

type BytesValue

type BytesValue []byte

BytesValue is a value of bytes type.

During encoding ad decoding, the BytesValue can be mapped using the slice rules described in the documentation of anymapper package.

func (*BytesValue) Bytes

func (b *BytesValue) Bytes() []byte

Bytes returns the value of the BytesValue.

func (*BytesValue) DecodeABI

func (b *BytesValue) DecodeABI(data Words) (int, error)

DecodeABI implements the Value interface.

func (*BytesValue) EncodeABI

func (b *BytesValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*BytesValue) IsDynamic

func (b *BytesValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*BytesValue) MapFrom

func (b *BytesValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*BytesValue) MapTo

func (b *BytesValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

func (*BytesValue) SetBytes

func (b *BytesValue) SetBytes(data []byte)

SetBytes sets the value of the BytesValue.

type Constructor

type Constructor struct {
	// contains filtered or unexported fields
}

Constructor represents a constructor in an Contract. The constructor can be used to encode arguments for a constructor call.

func MustParseConstructor

func MustParseConstructor(signature string) *Constructor

MustParseConstructor is like ParseConstructor but panics on error.

func NewConstructor

func NewConstructor(inputs *TupleType) *Constructor

NewConstructor creates a new Constructor instance.

func ParseConstructor

func ParseConstructor(signature string) (*Constructor, error)

ParseConstructor parses a constructor signature and returns a new Constructor.

A constructor signature is similar to a method signature, but it does not have a name and returns no values. It can be optionally prefixed with the "constructor" keyword.

The following examples are valid signatures:

((uint256,bytes32)[])
((uint256 a, bytes32 b)[] c)
constructor(tuple(uint256 a, bytes32 b)[] memory c)

This function is equivalent to calling Parser.ParseConstructor with the default configuration.

func (*Constructor) EncodeArg

func (m *Constructor) EncodeArg(arg any) ([]byte, error)

EncodeArg encodes arguments for a constructor call using a provided map or structure. The map or structure must have fields with the same names as the constructor arguments.

func (*Constructor) EncodeArgs

func (m *Constructor) EncodeArgs(args ...any) ([]byte, error)

EncodeArgs encodes arguments for a constructor call.

func (*Constructor) Inputs

func (m *Constructor) Inputs() *TupleType

Inputs returns the input arguments of the constructor as a tuple type.

func (*Constructor) String

func (m *Constructor) String() string

String returns the human-readable signature of the constructor.

type Contract

type Contract struct {
	Constructor        *Constructor
	Methods            map[string]*Method
	MethodsBySignature map[string]*Method
	Events             map[string]*Event
	Errors             map[string]*Error
}

Contract provides a high-level API for interacting with a contract. It can be created from a JSON ABI definition using the ParseJSON function or from a list of signatures using the ParseSignatures function.

func LoadJSON

func LoadJSON(path string) (*Contract, error)

LoadJSON loads the ABI from the given JSON file and returns a Contract instance.

func MustParseJSON

func MustParseJSON(data []byte) *Contract

MustParseJSON is like ParseJSON but panics on error.

func MustParseSignatures

func MustParseSignatures(signatures ...string) *Contract

MustParseSignatures is like ParseSignatures but panics on error.

func ParseJSON

func ParseJSON(data []byte) (*Contract, error)

ParseJSON parses the given ABI JSON and returns a Contract instance.

func ParseSignatures

func ParseSignatures(signatures ...string) (*Contract, error)

ParseSignatures parses list of signatures and returns a Contract instance. Signatures must be prefixed with the kind, e.g. "function" or "event".

It accepts signatures in the same format as ParseConstructor, ParseMethod, ParseEvent, and ParseError functions.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents an error in an ABI. The error can be used to decode errors returned by a contract call.

func MustParseError

func MustParseError(signature string) *Error

MustParseError is like ParseError but panics on error.

func NewError

func NewError(name string, inputs *TupleType) *Error

NewError creates a new Error instance.

func ParseError

func ParseError(signature string) (*Error, error)

ParseError parses an error signature and returns a new Error.

An error signature is similar to a method signature, but returns no values. It can be optionally prefixed with the "error" keyword.

The following examples are valid signatures:

foo((uint256,bytes32)[])
foo((uint256 a, bytes32 b)[] c)
error foo(tuple(uint256 a, bytes32 b)[] c)

This function is equivalent to calling Parser.ParseError with the default configuration.

func (*Error) DecodeValue

func (m *Error) DecodeValue(data []byte, val any) error

DecodeValue decodes the error into a map or structure. If a structure is given, it must have fields with the same names as error arguments.

func (*Error) DecodeValues

func (m *Error) DecodeValues(data []byte, vals ...any) error

DecodeValues decodes the error into a map or structure. If a structure is given, it must have fields with the same names as error arguments.

func (*Error) FourBytes

func (m *Error) FourBytes() FourBytes

FourBytes is the first four bytes of the Keccak256 hash of the error signature.

func (*Error) Inputs

func (m *Error) Inputs() *TupleType

Inputs returns the input arguments of the error as a tuple type.

func (*Error) Is

func (m *Error) Is(data []byte) bool

Is returns true if the ABI encoded data is an error of this type.

func (*Error) Name

func (m *Error) Name() string

Name returns the name of the error.

func (*Error) Signature

func (m *Error) Signature() string

Signature returns the error signature, that is, the error name and the canonical type of error arguments.

func (*Error) String

func (m *Error) String() string

String returns the human-readable signature of the error.

type Event

type Event struct {
	// contains filtered or unexported fields
}

Event represents an event in an ABI. The event can be used to decode events emitted by a contract.

func MustParseEvent

func MustParseEvent(signature string) *Event

MustParseEvent is like ParseEvent but panics on error.

func NewEvent

func NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event

NewEvent creates a new Event instance.

func ParseEvent

func ParseEvent(signature string) (*Event, error)

ParseEvent parses an event signature and returns a new Event.

An event signature is similar to a method signature, but returns no values. It can be optionally prefixed with the "event" keyword.

The following examples are valid signatures:

foo(int indexed,(uint256,bytes32)[])
foo(int indexed a, (uint256 b, bytes32 c)[] d)
event foo(int indexed a tuple(uint256 b, bytes32 c)[] d)

This function is equivalent to calling Parser.ParseEvent with the default configuration.

func (*Event) DecodeValue

func (e *Event) DecodeValue(topics []types.Hash, data []byte, val any) error

DecodeValue decodes the event into a map or structure. If a structure is given, it must have fields with the same names as the event arguments.

func (*Event) DecodeValues

func (e *Event) DecodeValues(topics []types.Hash, data []byte, vals ...any) error

DecodeValues decodes the event into a map or structure. If a structure is given, it must have fields with the same names as the event arguments.

func (*Event) Inputs

func (e *Event) Inputs() *EventTupleType

Inputs returns the input arguments of the event as a tuple type.

func (*Event) Name

func (e *Event) Name() string

Name returns the name of the event.

func (*Event) Signature

func (e *Event) Signature() string

Signature returns the event signature, that is, the event name and the canonical type of the input arguments.

func (*Event) String

func (e *Event) String() string

String returns the human-readable signature of the event.

func (*Event) Topic0

func (e *Event) Topic0() types.Hash

Topic0 returns the first topic of the event, that is, the Keccak256 hash of the event signature.

type EventTupleElem

type EventTupleElem struct {
	// Name of the tuple element. It is used when mapping values from and to
	// maps and structures. If the name is empty, when creating a new value,
	// the name will be set to topicN or dataN, where N is the index of the
	// topic or data element. Topics are counted from 1 because the first topic
	// is the event signature.
	Name string

	// Indexed indicates whether the element is indexed.
	Indexed bool

	// Type is the type of the element.
	Type Type
}

EventTupleElem is an element of an event tuple.

type EventTupleType

type EventTupleType struct {
	// contains filtered or unexported fields
}

EventTupleType represents a tuple type for event inputs. It works just like TupleType, but elements can be marked as indexed. When creating a new value, the indexed elements will be created first, followed by the non-indexed elements.

func NewEventTupleType

func NewEventTupleType(elems ...EventTupleElem) *EventTupleType

NewEventTupleType creates a new tuple type with the given elements.

func (*EventTupleType) CanonicalType

func (t *EventTupleType) CanonicalType() string

CanonicalType implements the Type interface.

func (*EventTupleType) DataSize

func (t *EventTupleType) DataSize() int

DataSize returns the number of non-indexed elements in the tuple.

func (*EventTupleType) DataTuple added in v0.2.0

func (t *EventTupleType) DataTuple() *TupleType

DataTuple returns the tuple of non-indexed arguments.

func (*EventTupleType) Elements

func (t *EventTupleType) Elements() []EventTupleElem

Elements returns the tuple elements.

func (*EventTupleType) IndexedSize

func (t *EventTupleType) IndexedSize() int

IndexedSize returns the number of indexed elements in the tuple.

func (*EventTupleType) Size

func (t *EventTupleType) Size() int

Size returns the number of elements in the tuple.

func (*EventTupleType) String

func (t *EventTupleType) String() string

String implements the Type interface.

func (*EventTupleType) TopicsTuple added in v0.2.0

func (t *EventTupleType) TopicsTuple() *TupleType

TopicsTuple returns the tuple of indexed arguments.

func (*EventTupleType) Value

func (t *EventTupleType) Value() Value

Value implements the Type interface.

type FixedArrayType

type FixedArrayType struct {
	// contains filtered or unexported fields
}

FixedArrayType represents a fixed-size array type.

func NewFixedArrayType

func NewFixedArrayType(typ Type, size int) *FixedArrayType

NewFixedArrayType creates a new fixed array type with the given element type and size.

func (*FixedArrayType) CanonicalType

func (f *FixedArrayType) CanonicalType() string

CanonicalType implements the Type interface.

func (*FixedArrayType) ElementType

func (f *FixedArrayType) ElementType() Type

ElementType returns the type of the array elements.

func (*FixedArrayType) Size

func (f *FixedArrayType) Size() int

Size returns the size of the array.

func (*FixedArrayType) String

func (f *FixedArrayType) String() string

String implements the Type interface.

func (*FixedArrayType) Value

func (f *FixedArrayType) Value() Value

Value implements the Type interface.

type FixedArrayValue

type FixedArrayValue []Value

FixedArrayValue is a value of fixed array type. The size of a slice is assumed to be equal to the size of the type.

During encoding, the FixedArrayValue can be mapped from a slice or an array.

During decoding the FixedArrayValue is mapped to a slice or an array of the same size.

func (FixedArrayValue) DecodeABI

func (a FixedArrayValue) DecodeABI(data Words) (int, error)

DecodeABI implements the Value interface.

func (FixedArrayValue) EncodeABI

func (a FixedArrayValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (FixedArrayValue) IsDynamic

func (a FixedArrayValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (FixedArrayValue) MapFrom

func (a FixedArrayValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (FixedArrayValue) MapTo

func (a FixedArrayValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

type FixedBytesType

type FixedBytesType struct {
	// contains filtered or unexported fields
}

FixedBytesType represents a fixed-size bytes type.

func NewFixedBytesType

func NewFixedBytesType(size int) *FixedBytesType

NewFixedBytesType creates a new fixed-size bytes type with the given size. The size must be between 1 and 32.

func (*FixedBytesType) CanonicalType

func (f *FixedBytesType) CanonicalType() string

CanonicalType implements the Type interface.

func (*FixedBytesType) Size

func (f *FixedBytesType) Size() int

Size returns the size of the bytes type.

func (*FixedBytesType) String

func (f *FixedBytesType) String() string

String implements the Type interface.

func (*FixedBytesType) Value

func (f *FixedBytesType) Value() Value

Value implements the Type interface.

type FixedBytesValue

type FixedBytesValue []byte

FixedBytesValue is a value of fixed bytes type. The size of a slice is assumed to be equal to the size of the bytesN type.

During encoding and decoding, the FixedBytesValue is mapped using the slice rules described in the documentation of anymapper package. Both values must have the same size.

func (*FixedBytesValue) Bytes

func (b *FixedBytesValue) Bytes() []byte

Bytes returns the value of the FixedBytesValue.

func (FixedBytesValue) DecodeABI

func (b FixedBytesValue) DecodeABI(data Words) (int, error)

DecodeABI implements the Value interface.

func (FixedBytesValue) EncodeABI

func (b FixedBytesValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (FixedBytesValue) IsDynamic

func (b FixedBytesValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (FixedBytesValue) MapFrom

func (b FixedBytesValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (FixedBytesValue) MapTo

func (b FixedBytesValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

func (*FixedBytesValue) SetBytes

func (b *FixedBytesValue) SetBytes(data []byte) error

SetBytes sets the value of the FixedBytesValue.

type FourBytes

type FourBytes [4]byte

FourBytes is a 4-byte method selector.

func (FourBytes) Bytes

func (f FourBytes) Bytes() []byte

Bytes returns the four bytes as a byte slice.

func (FourBytes) Hex

func (f FourBytes) Hex() string

Hex returns the four bytes as a hex string.

func (FourBytes) Match

func (f FourBytes) Match(data []byte) bool

Match returns true if the given ABI data matches the four byte selector.

func (FourBytes) String

func (f FourBytes) String() string

String returns the four bytes as a hex string.

type IntType

type IntType struct {
	// contains filtered or unexported fields
}

IntType represents a signed integer type.

func NewIntType

func NewIntType(size int) *IntType

NewIntType creates a new "int" type with the given size. The size must be between 8 and 256 and a multiple of 8.

func (*IntType) CanonicalType

func (i *IntType) CanonicalType() string

CanonicalType implements the Type interface.

func (*IntType) Size

func (i *IntType) Size() int

Size returns the size of the int type.

func (*IntType) String

func (i *IntType) String() string

Type implements the Type interface.

func (*IntType) Value

func (i *IntType) Value() Value

Value implements the Type interface.

type IntValue

type IntValue struct {
	big.Int
	Size int
}

IntValue is a value of intN types.

During encoding, the IntValue is mapped to the *big.Int type using the rules described in the documentation of anymapper package.

During decoding, the IntValue is mapped from the *big.Int type using the rules described in the documentation of anymapper package.

func (*IntValue) DecodeABI

func (i *IntValue) DecodeABI(words Words) (int, error)

DecodeABI implements the Value interface.

func (*IntValue) EncodeABI

func (i *IntValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*IntValue) IsDynamic

func (i *IntValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*IntValue) MapFrom

func (i *IntValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*IntValue) MapTo

func (i *IntValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

type MapFrom

type MapFrom interface {
	MapFrom(m Mapper, src any) error
}

MapFrom maps the value from the ABI Value.

type MapTo

type MapTo interface {
	MapTo(m Mapper, dst any) error
}

MapTo maps the value to the ABI Value.

type Mapper

type Mapper interface {
	Map(src any, dst any) error
}

Mapper used to map values to and from ABI types.

type Method

type Method struct {
	// contains filtered or unexported fields
}

Method represents a method in an ABI. The method can be used to encode arguments for a method call and decode return values from a method call.

func MustParseMethod

func MustParseMethod(signature string) *Method

MustParseMethod is like ParseMethod but panics on error.

func NewMethod

func NewMethod(name string, inputs, outputs *TupleType) *Method

NewMethod creates a new Method instance.

func ParseMethod

func ParseMethod(signature string) (*Method, error)

ParseMethod parses a method signature and returns a new Method.

The method accepts Solidity method signatures, but allows to omit the "function" keyword, argument names and the "returns" keyword. Method modifiers and argument data location specifiers are allowed, but ignored. Tuple types are indicated by parentheses, with the optional keyword "tuple" before the parentheses.

The following examples are valid signatures:

foo((uint256,bytes32)[])(uint256)
foo((uint256 a, bytes32 b)[] c)(uint256 d)
function foo(tuple(uint256 a, bytes32 b)[] memory c) pure returns (uint256 d)

This function is equivalent to calling Parser.ParseMethod with the default configuration.

func (*Method) DecodeArg

func (m *Method) DecodeArg(data []byte, arg any) error

DecodeArg decodes ABI-encoded arguments a method call.

func (*Method) DecodeArgs

func (m *Method) DecodeArgs(data []byte, args ...any) error

DecodeArgs decodes ABI-encoded arguments a method call.

func (*Method) DecodeValue

func (m *Method) DecodeValue(data []byte, val any) error

DecodeValue decodes the values returned by a method call into a map or structure. If a structure is given, it must have fields with the same names as the values returned by the method.

func (*Method) DecodeValues

func (m *Method) DecodeValues(data []byte, vals ...any) error

DecodeValues decodes return values from a method call to a given values.

func (*Method) EncodeArg

func (m *Method) EncodeArg(arg any) ([]byte, error)

EncodeArg encodes arguments for a method call using a provided map or structure. The map or structure must have fields with the same names as the method arguments.

func (*Method) EncodeArgs

func (m *Method) EncodeArgs(args ...any) ([]byte, error)

EncodeArgs encodes arguments for a method call.

func (*Method) FourBytes

func (m *Method) FourBytes() FourBytes

FourBytes is the first four bytes of the Keccak256 hash of the method signature. It is also known as a "function selector."

func (*Method) Inputs

func (m *Method) Inputs() *TupleType

Inputs returns the input arguments of the method as a tuple type.

func (*Method) Name

func (m *Method) Name() string

Name returns the name of the method.

func (*Method) Outputs

func (m *Method) Outputs() *TupleType

Outputs returns the output values of the method as a tuple type.

func (*Method) Signature

func (m *Method) Signature() string

Signature returns the method signature, that is, the method name and the canonical types of the input arguments.

func (*Method) String

func (m *Method) String() string

String returns the human-readable signature of the method.

type StringType

type StringType struct{}

StringType represents a string type.

func NewStringType

func NewStringType() *StringType

NewStringType creates a new "string" type.

func (*StringType) CanonicalType

func (s *StringType) CanonicalType() string

CanonicalType implements the Type interface.

func (*StringType) String

func (s *StringType) String() string

Type implements the Type interface.

func (*StringType) Value

func (s *StringType) Value() Value

Value implements the Type interface.

type StringValue

type StringValue string

StringValue is a value of bytes type.

During encoding ad decoding, the StringValue is mapped using the string rules described in the documentation of anymapper package.

func (*StringValue) DecodeABI

func (s *StringValue) DecodeABI(data Words) (int, error)

DecodeABI implements the Value interface.

func (*StringValue) EncodeABI

func (s *StringValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*StringValue) IsDynamic

func (s *StringValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*StringValue) MapFrom

func (s *StringValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*StringValue) MapTo

func (s *StringValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

func (*StringValue) SetString

func (s *StringValue) SetString(str string)

SetString sets the value of the StringValue.

func (*StringValue) String

func (s *StringValue) String() string

String returns the value of the StringValue.

type TupleType

type TupleType struct {
	// contains filtered or unexported fields
}

TupleType represents a tuple type.

func NewTupleType

func NewTupleType(elems ...TupleTypeElem) *TupleType

NewTupleType creates a new tuple type with the given elements.

func (*TupleType) CanonicalType

func (t *TupleType) CanonicalType() string

CanonicalType implements the Type interface.

func (*TupleType) Elements

func (t *TupleType) Elements() []TupleTypeElem

Elements returns the tuple elements.

func (*TupleType) Size

func (t *TupleType) Size() int

Size returns the number of elements in the tuple.

func (*TupleType) String

func (t *TupleType) String() string

String implements the Type interface.

func (*TupleType) Value

func (t *TupleType) Value() Value

Value implements the Type interface.

type TupleTypeElem

type TupleTypeElem struct {
	// Name of the tuple element. It is used when mapping values from and to
	// maps and structures. If the name is empty, when creating a new value
	// the name will be set to argN, where N is the index of the element.
	Name string

	// Type is the type of the element.
	Type Type
}

TupleTypeElem is an element of a tuple.

type TupleValue

type TupleValue []TupleValueElem

TupleValue is a value of tuple type.

During encoding, the TupleValue can be mapped from a struct or a map where keys or struct fields are used as tuple element names.

During decoding, the TupleValue can be mapped to a struct or a map where tuple element names are used as keys or struct fields.

func (*TupleValue) DecodeABI

func (t *TupleValue) DecodeABI(words Words) (int, error)

DecodeABI implements the Value interface.

func (*TupleValue) EncodeABI

func (t *TupleValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*TupleValue) IsDynamic

func (t *TupleValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*TupleValue) MapFrom

func (t *TupleValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*TupleValue) MapTo

func (t *TupleValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

type TupleValueElem

type TupleValueElem struct {
	// Name of the tuple element. It is used when mapping values from and to
	// maps and structures.
	Name string

	// Value is the value of the tuple element. It is used to encode and decode
	// the ABI data.
	Value Value
}

TupleValueElem is an element of tuple value.

type Type

type Type interface {
	// CanonicalType returns the canonical name of the type. In case of a
	// tuple, the canonical name is the canonical name of the tuple's
	// elements, separated by commas and enclosed in parentheses. Arrays
	// are represented by the canonical name of the element type followed
	// by square brackets with the array size.
	CanonicalType() string

	// String returns the user-friendly name of the type.
	String() string

	// Value creates a new zero value for the type.
	Value() Value
}

Type is a representation of a type like uint256 or address. The type can be used to create a new value of that type, but it cannot store a value.

func MustParseStruct added in v0.1.1

func MustParseStruct(definition string) Type

MustParseStruct is like ParseStruct but panics on error.

func MustParseType

func MustParseType(signature string) Type

MustParseType is like ParseType but panics on error.

func ParseStruct added in v0.1.1

func ParseStruct(definition string) (Type, error)

ParseStruct parses a struct definition and returns a new Type.

It is similar to ParseType, but accepts a struct definition instead of a type signature.

For example, the following two calls are equivalent:

ParseType("(uint256 a, bytes32 b)")
ParseStruct("struct { uint256 a; bytes32 b; }")

func ParseType

func ParseType(signature string) (Type, error)

ParseType parses a type signature and returns a new Type.

A type can be either an elementary type like uint256 or a tuple type. Tuple types are denoted by parentheses, with the optional keyword "tuple" before the parentheses. Parameter names are optional.

The generated types can be used to create new values, which can then be used to encode or decode ABI data.

Custom types may be added to the ABI.Types, this will allow the parser to handle them.

The following examples are valid type signatures:

uint256
(uint256 a,bytes32 b)
tuple(uint256 a, bytes32 b)[]

This function is equivalent to calling Parser.ParseType with the default configuration.

type UintType

type UintType struct {
	// contains filtered or unexported fields
}

UintType represents an unsigned integer type.

func NewUintType

func NewUintType(size int) *UintType

NewUintType creates a new "uint" type with the given size. The size must be between 8 and 256 and a multiple of 8.

func (*UintType) CanonicalType

func (u *UintType) CanonicalType() string

CanonicalType implements the Type interface.

func (*UintType) Size

func (u *UintType) Size() int

Size returns the size of the uint type.

func (*UintType) String

func (u *UintType) String() string

String implements the Type interface.

func (*UintType) Value

func (u *UintType) Value() Value

Value implements the Type interface.

type UintValue

type UintValue struct {
	big.Int
	Size int
}

UintValue is a value of uintN types.

During encoding, the UintValue is mapped to the *big.Int type using the rules described in the documentation of anymapper package.

During decoding, the UintValue is mapped from the *big.Int type using the rules described in the documentation of anymapper package.

func (*UintValue) DecodeABI

func (u *UintValue) DecodeABI(words Words) (int, error)

DecodeABI implements the Value interface.

func (*UintValue) EncodeABI

func (u *UintValue) EncodeABI() (Words, error)

EncodeABI implements the Value interface.

func (*UintValue) IsDynamic

func (u *UintValue) IsDynamic() bool

IsDynamic implements the Value interface.

func (*UintValue) MapFrom

func (u *UintValue) MapFrom(m Mapper, src any) error

MapFrom implements the anymapper.MapFrom interface.

func (*UintValue) MapTo

func (u *UintValue) MapTo(m Mapper, dst any) error

MapTo implements the anymapper.MapTo interface.

type Value

type Value interface {
	// IsDynamic indicates whether the type is dynamic.
	IsDynamic() bool

	// EncodeABI returns the ABI encoding of the value.
	EncodeABI() (Words, error)

	// DecodeABI sets the value from the ABI encoded data.
	DecodeABI(Words) (int, error)
}

Value represents a value that can be encoded to and from ABI.

Values are used as an intermediate representation during encoding and decoding ABI data. Usually, they are not used outside the abi package.

When data is encoded using Encoder, the values provided to Encoder are mapped to Value instances using the anymapper package, and then they are used to encode the ABI data. When the data is decoded using Decoder, the Value instances are used to decode the ABI data, and then the values are mapped to the target types.

type Word

type Word [WordLength]byte

Word represents a 32-bytes EVM word.

func BytesToWords

func BytesToWords(b []byte) []Word

func (Word) Bytes

func (w Word) Bytes() []byte

Bytes returns the word as a byte slice.

func (Word) IsZero

func (w Word) IsZero() bool

IsZero returns true if all bytes in then word are zeros.

func (Word) LeadingZeros

func (w Word) LeadingZeros() int

LeadingZeros returns the number of leading zero bits.

func (*Word) SetBytesPadLeft

func (w *Word) SetBytesPadLeft(b []byte) error

SetBytesPadLeft sets the word to the given bytes, padded on the left.

func (*Word) SetBytesPadRight

func (w *Word) SetBytesPadRight(b []byte) error

SetBytesPadRight sets the word to the given bytes, padded on the right.

func (Word) TrailingZeros

func (w Word) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits.

type Words

type Words []Word

Words is a slice of words.

func (*Words) AppendBytes

func (w *Words) AppendBytes(b []byte)

AppendBytes appends the given bytes to the words.

func (Words) Bytes

func (w Words) Bytes() []byte

Bytes returns the words as a byte slice.

func (*Words) SetBytes

func (w *Words) SetBytes(b []byte)

SetBytes sets the words to the given bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL