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 MustEncodeValue(t Type, val any) []byte
- func MustEncodeValues(t Type, vals ...any) []byte
- 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) 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) *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(m 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
- type Contract
- type Error
- func (m *Error) DecodeValue(data []byte, val any) error
- func (m *Error) DecodeValues(data []byte, vals ...any) error
- func (m *Error) FourBytes() FourBytes
- func (m *Error) Inputs() *TupleType
- func (m *Error) Is(data []byte) bool
- func (m *Error) Name() string
- func (m *Error) Signature() string
- func (m *Error) String() string
- 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) 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) 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(m 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) Name() string
- func (m *Method) Outputs() *TupleType
- func (m *Method) Signature() string
- func (m *Method) String() string
- 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 IsPanic ¶
IsPanic returns true if the data has the panic prefix. It does not check whether the data is a valid panic message.
func IsRevert ¶
IsRevert returns true if the data has the revert prefix. It does not check whether the data is a valid revert message.
func MustEncodeValue ¶
MustEncodeValue is like EncodeValue but panics on error.
func MustEncodeValues ¶
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 ¶
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) NewConstructor ¶
func (a *ABI) NewConstructor(inputs *TupleType) *Constructor
NewConstructor creates a new Constructor instance.
func (*ABI) NewEvent ¶
func (a *ABI) NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event
NewEvent creates a new Event 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.
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) 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(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 ¶
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.
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 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.
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.
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.
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) 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) 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.
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.
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 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 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, 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) DecodeArgs ¶
DecodeArgs decodes ABI-encoded arguments a method call.
func (*Method) DecodeValue ¶
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 ¶
DecodeValues decodes return values from a method call to a given values.
func (*Method) EncodeArg ¶
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 ¶
EncodeArgs encodes arguments for a method call.
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."
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) 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 { // 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
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.