Documentation ¶
Index ¶
- Constants
- Variables
- func DecodePanic(data []byte) *big.Int
- func DecodeRevert(data []byte) string
- func DecodeValue(t Type, abi []byte, val any) error
- func DecodeValues(t Type, abi []byte, vals ...any) error
- func EncodeValue(t Type, val any) ([]byte, error)
- func EncodeValues(t Type, vals ...any) ([]byte, error)
- func IsPanic(data []byte) bool
- func IsRevert(data []byte) bool
- func MustDecodeValue(t Type, abi []byte, val any)
- func MustDecodeValues(t Type, abi []byte, vals ...any)
- func MustEncodeValue(t Type, val any) []byte
- func MustEncodeValues(t Type, vals ...any) []byte
- func ToPanicError(data []byte) error
- func ToRevertError(data []byte) error
- type ABI
- func (a *ABI) DecodeValue(t Type, abi []byte, val any) error
- func (a *ABI) DecodeValues(t Type, abi []byte, vals ...any) error
- func (a *ABI) EncodeValue(t Type, val any) ([]byte, error)
- func (a *ABI) EncodeValues(t Type, vals ...any) ([]byte, error)
- func (a *ABI) LoadJSON(path string) (*Contract, error)
- func (a *ABI) MustDecodeValue(t Type, abi []byte, val any)
- func (a *ABI) MustDecodeValues(t Type, abi []byte, vals ...any)
- func (a *ABI) MustEncodeValue(t Type, val any) []byte
- func (a *ABI) MustEncodeValues(t Type, vals ...any) []byte
- func (a *ABI) MustLoadJSON(path string) *Contract
- func (a *ABI) MustParseConstructor(signature string) *Constructor
- func (a *ABI) MustParseError(signature string) *Error
- func (a *ABI) MustParseEvent(signature string) *Event
- func (a *ABI) MustParseJSON(data []byte) *Contract
- func (a *ABI) MustParseMethod(signature string) *Method
- func (a *ABI) MustParseSignatures(signatures ...string) *Contract
- func (a *ABI) MustParseStruct(definition string) Type
- func (a *ABI) MustParseType(signature string) Type
- func (a *ABI) NewConstructor(inputs *TupleType) *Constructor
- func (a *ABI) NewError(name string, inputs *TupleType) *Error
- func (a *ABI) NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event
- func (a *ABI) NewMethod(name string, inputs, outputs *TupleType, mutability StateMutability) *Method
- func (a *ABI) ParseConstructor(signature string) (*Constructor, error)
- func (a *ABI) ParseError(signature string) (*Error, error)
- func (a *ABI) ParseEvent(signature string) (*Event, error)
- func (a *ABI) ParseJSON(data []byte) (*Contract, error)
- func (a *ABI) ParseMethod(signature string) (*Method, error)
- func (a *ABI) ParseSignatures(signatures ...string) (*Contract, error)
- func (a *ABI) ParseStruct(definition string) (Type, error)
- func (a *ABI) ParseType(signature string) (Type, error)
- type AddressType
- type AddressValue
- func (a *AddressValue) Address() types.Address
- func (a *AddressValue) DecodeABI(words Words) (int, error)
- func (a *AddressValue) EncodeABI() (Words, error)
- func (a *AddressValue) IsDynamic() bool
- func (a *AddressValue) MapFrom(m Mapper, src any) error
- func (a *AddressValue) MapTo(_ Mapper, dst any) error
- func (a *AddressValue) SetAddress(v types.Address)
- type AliasType
- type ArrayType
- type ArrayValue
- type BoolType
- type BoolValue
- type BytesType
- type BytesValue
- func (b *BytesValue) Bytes() []byte
- func (b *BytesValue) DecodeABI(data Words) (int, error)
- func (b *BytesValue) EncodeABI() (Words, error)
- func (b *BytesValue) IsDynamic() bool
- func (b *BytesValue) MapFrom(m Mapper, src any) error
- func (b *BytesValue) MapTo(m Mapper, dst any) error
- func (b *BytesValue) SetBytes(data []byte)
- type Constructor
- func (m *Constructor) EncodeArg(code []byte, arg any) ([]byte, error)
- func (m *Constructor) EncodeArgs(code []byte, args ...any) ([]byte, error)
- func (m *Constructor) Inputs() *TupleType
- func (m *Constructor) MustEncodeArg(code []byte, arg any) []byte
- func (m *Constructor) MustEncodeArgs(code []byte, args ...any) []byte
- func (m *Constructor) String() string
- type Contract
- type CustomError
- type Error
- func (e *Error) DecodeValue(data []byte, val any) error
- func (e *Error) DecodeValues(data []byte, vals ...any) error
- func (e *Error) FourBytes() FourBytes
- func (e *Error) HandleError(err error) error
- func (e *Error) Inputs() *TupleType
- func (e *Error) Is(data []byte) bool
- func (e *Error) MustDecodeValue(data []byte, val any)
- func (e *Error) MustDecodeValues(data []byte, vals ...any)
- func (e *Error) Name() string
- func (e *Error) Signature() string
- func (e *Error) String() string
- func (e *Error) ToError(data []byte) error
- type Event
- func (e *Event) DecodeValue(topics []types.Hash, data []byte, val any) error
- func (e *Event) DecodeValues(topics []types.Hash, data []byte, vals ...any) error
- func (e *Event) Inputs() *EventTupleType
- func (e *Event) MustDecodeValue(topics []types.Hash, data []byte, val any)
- func (e *Event) MustDecodeValues(topics []types.Hash, data []byte, vals ...any)
- func (e *Event) Name() string
- func (e *Event) Signature() string
- func (e *Event) String() string
- func (e *Event) Topic0() types.Hash
- type EventTupleElem
- type EventTupleType
- func (t *EventTupleType) CanonicalType() string
- func (t *EventTupleType) DataSize() int
- func (t *EventTupleType) DataTuple() *TupleType
- func (t *EventTupleType) Elements() []EventTupleElem
- func (t *EventTupleType) IndexedSize() int
- func (t *EventTupleType) IsDynamic() bool
- func (t *EventTupleType) Size() int
- func (t *EventTupleType) String() string
- func (t *EventTupleType) TopicsTuple() *TupleType
- func (t *EventTupleType) Value() Value
- type FixedArrayType
- type FixedArrayValue
- type FixedBytesType
- type FixedBytesValue
- func (b *FixedBytesValue) Bytes() []byte
- func (b FixedBytesValue) DecodeABI(data Words) (int, error)
- func (b FixedBytesValue) EncodeABI() (Words, error)
- func (b FixedBytesValue) IsDynamic() bool
- func (b FixedBytesValue) MapFrom(m Mapper, src any) error
- func (b FixedBytesValue) MapTo(_ Mapper, dst any) error
- func (b *FixedBytesValue) SetBytes(data []byte) error
- type FourBytes
- type IntType
- type IntValue
- type MapFrom
- type MapTo
- type Mapper
- type Method
- func (m *Method) DecodeArg(data []byte, arg any) error
- func (m *Method) DecodeArgs(data []byte, args ...any) error
- func (m *Method) DecodeValue(data []byte, val any) error
- func (m *Method) DecodeValues(data []byte, vals ...any) error
- func (m *Method) EncodeArg(arg any) ([]byte, error)
- func (m *Method) EncodeArgs(args ...any) ([]byte, error)
- func (m *Method) FourBytes() FourBytes
- func (m *Method) Inputs() *TupleType
- func (m *Method) MustDecodeArg(data []byte, arg any)
- func (m *Method) MustDecodeArgs(data []byte, args ...any)
- func (m *Method) MustDecodeValue(data []byte, val any)
- func (m *Method) MustDecodeValues(data []byte, vals ...any)
- func (m *Method) MustEncodeArg(arg any) []byte
- func (m *Method) MustEncodeArgs(args ...any) []byte
- func (m *Method) Name() string
- func (m *Method) Outputs() *TupleType
- func (m *Method) Signature() string
- func (m *Method) StateMutability() StateMutability
- func (m *Method) String() string
- type PanicError
- type RevertError
- type StateMutability
- type StringType
- type StringValue
- func (s *StringValue) DecodeABI(data Words) (int, error)
- func (s *StringValue) EncodeABI() (Words, error)
- func (s *StringValue) IsDynamic() bool
- func (s *StringValue) MapFrom(m Mapper, src any) error
- func (s *StringValue) MapTo(m Mapper, dst any) error
- func (s *StringValue) SetString(str string)
- func (s *StringValue) String() string
- type TupleType
- type TupleTypeElem
- type TupleValue
- type TupleValueElem
- type Type
- type UintType
- type UintValue
- type Value
- type Word
- type Words
Constants ¶
const WordLength = 32
WordLength is the number of bytes in an EVM word.
Variables ¶
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{} )
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.
var Panic = NewError("Panic", NewTupleType(TupleTypeElem{Name: "error", Type: NewUintType(256)}))
Panic is the Error instance for panic responses.
var Revert = NewError("Error", NewTupleType(TupleTypeElem{Name: "error", Type: NewStringType()}))
Revert is the Error instance for revert responses.
Functions ¶
func DecodePanic ¶
DecodePanic decodes the panic data returned by contract calls. If the data is not a valid panic message, it returns nil.
func DecodeRevert ¶
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 ¶
DecodeValue decodes the given ABI-encoded data into the given value. Value must be a pointer to a struct or a map.
func DecodeValues ¶
DecodeValues decodes the given ABI-encoded data into the given values. The t type must be a tuple type.
func EncodeValue ¶
EncodeValue encodes a value to ABI encoding.
func EncodeValues ¶
EncodeValues encodes a list of values to ABI encoding. The t type must be a tuple type.
func MustDecodeValue ¶ added in v0.5.0
MustDecodeValue is like DecodeValue but panics on error.
func MustDecodeValues ¶ added in v0.5.0
MustDecodeValues is like DecodeValues but panics on error.
func MustEncodeValue ¶
MustEncodeValue is like EncodeValue but panics on error.
func MustEncodeValues ¶
MustEncodeValues is like EncodeValues but panics on error.
func ToPanicError ¶ added in v0.4.2
ToPanicError converts the panic data returned by contract calls into a PanicError. If the data does not contain a valid panic message, it returns nil.
func ToRevertError ¶ added in v0.4.2
ToRevertError converts the revert data returned by contract calls into a RevertError. If the data does not contain a valid revert message, it returns nil.
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 ¶
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 ¶
DecodeValues decodes the given ABI-encoded data into the given values. The t type must be a tuple type.
func (*ABI) EncodeValue ¶
EncodeValue encodes a value to ABI encoding.
func (*ABI) EncodeValues ¶
EncodeValues encodes a list of values to ABI encoding. The t type must be a tuple type.
func (*ABI) LoadJSON ¶ added in v0.5.0
LoadJSON loads the ABI from the given JSON file and returns a Contract instance.
func (*ABI) MustDecodeValue ¶ added in v0.5.0
MustDecodeValue is like DecodeValue but panics on error.
func (*ABI) MustDecodeValues ¶ added in v0.5.0
MustDecodeValues is like DecodeValues but panics on error.
func (*ABI) MustEncodeValue ¶ added in v0.5.0
MustEncodeValue is like EncodeValue but panics on error.
func (*ABI) MustEncodeValues ¶ added in v0.5.0
MustEncodeValues is like EncodeValues but panics on error.
func (*ABI) MustLoadJSON ¶ added in v0.5.0
MustLoadJSON is like LoadJSON but panics on error.
func (*ABI) MustParseConstructor ¶ added in v0.5.0
func (a *ABI) MustParseConstructor(signature string) *Constructor
MustParseConstructor is like ParseConstructor but panics on error.
func (*ABI) MustParseError ¶ added in v0.5.0
MustParseError is like ParseError but panics on error.
func (*ABI) MustParseEvent ¶ added in v0.5.0
MustParseEvent is like ParseEvent but panics on error.
func (*ABI) MustParseJSON ¶ added in v0.5.0
MustParseJSON is like ParseJSON but panics on error.
func (*ABI) MustParseMethod ¶ added in v0.5.0
MustParseMethod is like ParseMethod but panics on error.
func (*ABI) MustParseSignatures ¶ added in v0.5.0
MustParseSignatures is like ParseSignatures but panics on error.
func (*ABI) MustParseStruct ¶ added in v0.5.0
MustParseStruct is like ParseStruct but panics on error.
func (*ABI) MustParseType ¶ added in v0.5.0
MustParseType is like ParseType but panics on error.
func (*ABI) NewConstructor ¶
func (a *ABI) NewConstructor(inputs *TupleType) *Constructor
NewConstructor creates a new Constructor instance.
func (*ABI) NewError ¶
NewError creates a new Error instance.
This method is rarely used, see ParseError for a more convenient way to create a new Error.
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, mutability StateMutability) *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 ¶
ParseError parses an error signature and returns a new Error.
See ParseError for more information.
func (*ABI) ParseEvent ¶
ParseEvent parses an event signature and returns a new Event.
See ParseEvent for more information.
func (*ABI) ParseMethod ¶
ParseMethod parses a method signature and returns a new Method.
See ParseMethod for more information.
func (*ABI) ParseSignatures ¶
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.
In case of duplicate function, event or error names, a counter will be appended to the name starting from 2.
func (*ABI) ParseStruct ¶ added in v0.1.1
ParseStruct parses a struct definition and returns a new Type.
See ParseStruct 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) IsDynamic ¶ added in v0.5.0
func (a *AddressType) IsDynamic() bool
IsDynamic 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 ¶
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(_ 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 ¶
NewAliasType creates a new alias type.
func (*AliasType) CanonicalType ¶
CanonicalType implements the Type interface.
type ArrayType ¶
type ArrayType struct {
// contains filtered or unexported fields
}
ArrayType represents an unbounded array type.
func NewArrayType ¶
NewArrayType creates a dynamic array type with the given element type.
func (*ArrayType) CanonicalType ¶
CanonicalType implements the Type interface.
func (*ArrayType) ElementType ¶
ElementType returns the type of the array elements.
type ArrayValue ¶
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.
type BoolType ¶
type BoolType struct{}
BoolType represents a boolean type.
func (*BoolType) CanonicalType ¶
CanonicalType 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.
type BytesType ¶
type BytesType struct{}
BytesType represents a bytes type.
func (*BytesType) CanonicalType ¶
CanonicalType 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.
This method is rarely used, see ParseConstructor for a more convenient way to create a new Constructor.
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(code []byte, arg any) ([]byte, error)
EncodeArg encodes an argument for a contract deployment. The map or structure must have fields with the same names as the constructor arguments.
func (*Constructor) EncodeArgs ¶
func (m *Constructor) EncodeArgs(code []byte, args ...any) ([]byte, error)
EncodeArgs encodes arguments for a contract deployment. The map or structure must have fields with the same names as the constructor arguments.
func (*Constructor) Inputs ¶
func (m *Constructor) Inputs() *TupleType
Inputs returns the input arguments of the constructor as a tuple type.
func (*Constructor) MustEncodeArg ¶ added in v0.5.0
func (m *Constructor) MustEncodeArg(code []byte, arg any) []byte
MustEncodeArg is like EncodeArg but panics on error.
func (*Constructor) MustEncodeArgs ¶ added in v0.5.0
func (m *Constructor) MustEncodeArgs(code []byte, args ...any) []byte
MustEncodeArgs is like EncodeArgs but panics on error.
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 Types map[string]Type // Types defined in the ABI (structs, enums and user-defined Value Types) }
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 MustLoadJSON ¶ added in v0.5.0
MustLoadJSON is like LoadJSON but panics on error.
func MustParseJSON ¶
MustParseJSON is like ParseJSON but panics on error.
func MustParseSignatures ¶
MustParseSignatures is like ParseSignatures but panics on error.
func ParseSignatures ¶
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.
func (*Contract) HandleError ¶ added in v0.5.0
HandleError converts an error returned by a contract call to a RevertError, PanicError, or CustomError if applicable. If not, it returns the original error.
func (*Contract) IsError ¶ added in v0.4.2
IsError returns true if the given error data, returned by a contract call, corresponds to a revert, panic, or custom error.
func (*Contract) RegisterTypes ¶ added in v0.5.0
RegisterTypes registers types defined in the contract to the given ABI instance. This enables the use of types defined in the contract in all Parse* methods.
If the type name already exists, it will be overwritten.
type CustomError ¶ added in v0.4.2
type CustomError struct { Type *Error // The error type. Data []byte // The error data returned by the contract call. }
CustomError represents a custom error returned by a contract call.
func (CustomError) Error ¶ added in v0.4.2
func (e CustomError) Error() string
Error implements the error interface.
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 ¶
MustParseError is like ParseError but panics on error.
func ParseError ¶
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 ¶
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 ¶
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 ¶
FourBytes is the first four bytes of the Keccak256 hash of the error signature.
func (*Error) HandleError ¶ added in v0.5.0
HandleError converts an error returned by a contract call to a custom error if possible. If provider error is nil, it returns nil.
func (*Error) MustDecodeValue ¶ added in v0.5.0
MustDecodeValue is like DecodeValue but panics on error.
func (*Error) MustDecodeValues ¶ added in v0.5.0
MustDecodeValues is like DecodeValues but panics on error.
func (*Error) Signature ¶
Signature returns the error signature, that is, the error name and the canonical type of error arguments.
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 ¶
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.
This method is rarely used, see ParseEvent for a more convenient way to create a new Event.
func ParseEvent ¶
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 ¶
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 ¶
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) MustDecodeValue ¶ added in v0.5.0
MustDecodeValue is like DecodeValue but panics on error.
func (*Event) MustDecodeValues ¶ added in v0.5.0
MustDecodeValues is like DecodeValues but panics on error.
func (*Event) Signature ¶
Signature returns the event signature, that is, the event name and the canonical type of the input arguments.
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) IsDynamic ¶ added in v0.5.0
func (t *EventTupleType) IsDynamic() bool
IsDynamic implements the Type interface.
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.
If the type is indexed and dynamic, the type will be converted to bytes32.
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) IsDynamic ¶ added in v0.5.0
func (f *FixedArrayType) IsDynamic() bool
IsDynamic implements the Type interface.
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.
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) IsDynamic ¶ added in v0.5.0
func (f *FixedBytesType) IsDynamic() bool
IsDynamic 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(_ 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.
type IntType ¶
type IntType struct {
// contains filtered or unexported fields
}
IntType represents a signed integer type.
func NewIntType ¶
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 ¶
CanonicalType implements the Type interface.
type IntValue ¶
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.
type MapFrom ¶
type MapFrom interface { // MapFrom maps the value from the ABI Value. // // src is never a pointer. MapFrom(m Mapper, src any) error }
MapFrom maps the value from the ABI Value.
type MapTo ¶
type MapTo interface { // MapTo maps the value to the ABI Value. // // dst is always an initialized pointer. MapTo(m Mapper, dst any) error }
MapTo maps the value to the ABI Value.
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 ¶
MustParseMethod is like ParseMethod but panics on error.
func NewMethod ¶
func NewMethod(name string, inputs, outputs *TupleType, mutability StateMutability) *Method
NewMethod creates a new Method instance.
This method is rarely used, see ParseMethod for a more convenient way to create a new Method.
func ParseMethod ¶
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. Tuple types are indicated by parentheses, with the optional keyword "tuple" before the parentheses.
If argument names are omitted, the default "argN" is used, where N is the argument index. Similarly, if return value names are omitted, "argN" is also used.
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 ¶
DecodeArg decodes an ABI-encoded data into a provided map or struct.
Provided struct or map must have fields that match the names of the method's arguments.
Provided data must be prefixed with the method selector.
func (*Method) DecodeArgs ¶
DecodeArgs decodes an ABI-encoded data into a provided list of arguments.
Provided data must be prefixed with the method selector.
func (*Method) DecodeValue ¶
DecodeValue decodes an ABI-encoded data into a provided map or struct.
Provided struct or map must have fields that match the names of the method's return values.
func (*Method) DecodeValues ¶
DecodeValues decodes an ABI-encoded data into a provided list of return variables.
func (*Method) EncodeArg ¶
EncodeArg encodes arguments for a method call using a provided map or structure.
Provided struct or map must have fields that match the names of the method's arguments.
The return value is a ABI-encoded data prefixed with the method selector.
func (*Method) EncodeArgs ¶
EncodeArgs encodes arguments for a method call using a provided list of arguments.
The return value is a ABI-encoded data prefixed with the method selector.
func (*Method) 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) MustDecodeArg ¶ added in v0.5.0
MustDecodeArg is like DecodeArg but panics on error.
func (*Method) MustDecodeArgs ¶ added in v0.5.0
MustDecodeArgs is like DecodeArgs but panics on error.
func (*Method) MustDecodeValue ¶ added in v0.5.0
MustDecodeValue is like DecodeValue but panics on error.
func (*Method) MustDecodeValues ¶ added in v0.5.0
MustDecodeValues is like DecodeValues but panics on error.
func (*Method) MustEncodeArg ¶ added in v0.5.0
MustEncodeArg is like EncodeArg but panics on error.
func (*Method) MustEncodeArgs ¶ added in v0.5.0
MustEncodeArgs is like EncodeArgs but panics on error.
func (*Method) Signature ¶
Signature returns the method signature, that is, the method name and the canonical types of the input arguments.
func (*Method) StateMutability ¶ added in v0.5.0
func (m *Method) StateMutability() StateMutability
StateMutability returns the state mutability of the method.
type PanicError ¶ added in v0.4.2
PanicError represents an error returned by contract calls when the call panics.
func (PanicError) Error ¶ added in v0.4.2
func (e PanicError) Error() string
Error implements the error interface.
type RevertError ¶ added in v0.4.2
type RevertError struct {
Reason string
}
RevertError represents an error returned by contract calls when the call reverts.
func (RevertError) Error ¶ added in v0.4.2
func (e RevertError) Error() string
Error implements the error interface.
type StateMutability ¶ added in v0.5.0
type StateMutability int
const ( StateMutabilityUnknown StateMutability = iota StateMutabilityPure StateMutabilityView StateMutabilityNonPayable StateMutabilityPayable )
func StateMutabilityFromString ¶ added in v0.5.0
func StateMutabilityFromString(s string) StateMutability
func (StateMutability) String ¶ added in v0.5.0
func (m StateMutability) String() string
type StringType ¶
type StringType struct{}
StringType represents a string type.
func (*StringType) CanonicalType ¶
func (s *StringType) CanonicalType() string
CanonicalType implements the Type interface.
func (*StringType) IsDynamic ¶ added in v0.5.0
func (s *StringType) IsDynamic() bool
IsDynamic implements the Type interface.
func (*StringType) String ¶
func (s *StringType) String() string
Type 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 ¶
CanonicalType implements the Type interface.
func (*TupleType) Elements ¶
func (t *TupleType) Elements() []TupleTypeElem
Elements returns the tuple elements.
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.
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 { // IsDynamic indicates whether the type is dynamic. IsDynamic() bool // CanonicalType is the type as it would appear in the ABI. // // Only the types defined in the ABI specification are allowed: // https://docs.soliditylang.org/en/latest/abi-spec.html // // In case of a tuple, the canonical type is the canonical type 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
MustParseStruct is like ParseStruct but panics on error.
func MustParseType ¶
MustParseType is like ParseType but panics on error.
func ParseStruct ¶ added in v0.1.1
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 ¶
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 ¶
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 ¶
CanonicalType implements the Type interface.
type UintValue ¶
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.
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 (Word) LeadingZeros ¶
LeadingZeros returns the number of leading zero bits.
func (*Word) SetBytesPadLeft ¶
SetBytesPadLeft sets the word to the given bytes, padded on the left.
func (*Word) SetBytesPadRight ¶
SetBytesPadRight sets the word to the given bytes, padded on the right.
func (Word) TrailingZeros ¶
TrailingZeros returns the number of trailing zero bits.
type Words ¶
type Words []Word
Words is a slice of words.
func (*Words) AppendBytes ¶
AppendBytes appends the given bytes to the words.