Documentation ¶
Overview ¶
Package abi implements the Ethereum ABI (Application Binary Interface).
The Ethereum ABI is strongly typed, known at compile time and static. This ABI will handle basic type casting; unsigned to signed and visa versa. It does not handle slice casting such as unsigned slice to signed slice. Bit size type casting is also handled. ints with a bit size of 32 will be properly cast to int256, etc.
Index ¶
- Constants
- Variables
- func ReadFixedBytes(t Type, word []byte) (interface{}, error)
- func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{}
- func ToCamelCase(input string) string
- func U256(n *big.Int) []byte
- func UnpackRevert(data []byte) (string, error)
- type ABI
- func (abi *ABI) EventByID(topic common.Hash) (*Event, error)
- func (abi *ABI) MethodById(sigdata []byte) (*Method, error)
- func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error)
- func (abi *ABI) UnmarshalJSON(data []byte) error
- func (abi ABI) Unpack(v interface{}, name string, data []byte) (err error)
- func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error)
- type Argument
- type ArgumentMarshaling
- type Arguments
- func (arguments Arguments) LengthNonIndexed() int
- func (arguments Arguments) NonIndexed() Arguments
- func (arguments Arguments) Pack(args ...interface{}) ([]byte, error)
- func (arguments Arguments) PackValues(args []interface{}) ([]byte, error)
- func (arguments Arguments) Unpack(v interface{}, data []byte) error
- func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error
- func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error)
- type Event
- type Method
- type Type
Examples ¶
Constants ¶
const ( IntTy byte = iota UintTy BoolTy StringTy SliceTy ArrayTy TupleTy AddressTy FixedBytesTy BytesTy HashTy FixedPointTy FunctionTy )
Type enumerator
Variables ¶
var ( // MaxUint256 is the maximum value that can be represented by a uint256 MaxUint256 = big.NewInt(0).Add( big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(-1)) // MaxInt256 is the maximum value that can be represented by a int256 MaxInt256 = big.NewInt(0).Add( big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(-1)) )
var RevertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
UsingOVM Both RevertSelector and UnpackRevert were pulled from upstream geth as they were not present in the version of geth that this codebase was forked from. These are useful for displaying revert messages to users when they use `eth_call` RevertSelector is a special function selector for revert reason unpacking.
Functions ¶
func ReadFixedBytes ¶
ReadFixedBytes uses reflection to create a fixed array to be read from
func ReadInteger ¶
ReadInteger reads the integer based on its kind and returns the appropriate value
func ToCamelCase ¶
ToCamelCase converts an under-score string to a camel-case string
func UnpackRevert ¶
UnpackRevert resolves the abi-encoded revert reason. According to the solidity docs https://docs.soliditylang.org/en/v0.8.4/control-structures.html#revert, the provided revert reason is abi-encoded as if it were a call to a function `Error(string)`. So it's a special tool for it.
Types ¶
type ABI ¶
The ABI holds information about a contract's context and available invokable methods. It will allow you to type check function calls and packs data accordingly.
func JSON ¶
JSON returns a parsed ABI interface and error if it failed.
Example ¶
const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]` abi, err := JSON(strings.NewReader(definition)) if err != nil { panic(err) } out, err := abi.Pack("isBar", common.HexToAddress("01")) if err != nil { panic(err) } fmt.Printf("%x\n", out)
Output: 1f2c40920000000000000000000000000000000000000000000000000000000000000001
func (*ABI) EventByID ¶
EventByID looks an event up by its topic hash in the ABI and returns nil if none found.
func (*ABI) MethodById ¶
MethodById looks up a method by the 4-byte id returns nil if none found
func (ABI) Pack ¶
Pack the given method name to conform the ABI. Method call's data will consist of method_id, args0, arg1, ... argN. Method id consists of 4 bytes and arguments are all 32 bytes. Method ids are created from the first 4 bytes of the hash of the methods string signature. (signature = baz(uint32,string32))
func (*ABI) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface
type Argument ¶
Argument holds the name of the argument and the corresponding type. Types are used when packing and testing arguments.
func (*Argument) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface
type ArgumentMarshaling ¶
type ArgumentMarshaling struct { Name string Type string InternalType string Components []ArgumentMarshaling Indexed bool }
type Arguments ¶
type Arguments []Argument
func (Arguments) LengthNonIndexed ¶
LengthNonIndexed returns the number of arguments when not counting 'indexed' ones. Only events can ever have 'indexed' arguments, it should always be false on arguments for method input/output
func (Arguments) NonIndexed ¶
NonIndexed returns the arguments with indexed arguments filtered out
func (Arguments) PackValues ¶
PackValues performs the operation Go format -> Hexdata It is the semantic opposite of UnpackValues
func (Arguments) UnpackIntoMap ¶
UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value
func (Arguments) UnpackValues ¶
UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification, without supplying a struct to unpack into. Instead, this method returns a list containing the values. An atomic argument will be a list with one element.
type Event ¶
type Event struct { // Name is the event name used for internal representation. It's derived from // the raw name and a suffix will be added in the case of a event overload. // // e.g. // There are two events have same name: // * foo(int,int) // * foo(uint,uint) // The event name of the first one wll be resolved as foo while the second one // will be resolved as foo0. Name string // RawName is the raw event name parsed from ABI. RawName string Anonymous bool Inputs Arguments }
Event is an event potentially triggered by the EVM's LOG mechanism. The Event holds type information (inputs) about the yielded output. Anonymous events don't get the signature canonical representation as the first LOG topic.
func (Event) ID ¶
ID returns the canonical representation of the event's signature used by the abi definition to identify event names and types.
type Method ¶
type Method struct { // Name is the method name used for internal representation. It's derived from // the raw name and a suffix will be added in the case of a function overload. // // e.g. // There are two functions have same name: // * foo(int,int) // * foo(uint,uint) // The method name of the first one will be resolved as foo while the second one // will be resolved as foo0. Name string // RawName is the raw method name parsed from ABI. RawName string Const bool Inputs Arguments Outputs Arguments }
Method represents a callable given a `Name` and whether the method is a constant. If the method is `Const` no transaction needs to be created for this particular Method call. It can easily be simulated using a local VM. For example a `Balance()` method only needs to retrieve something from the storage and therefore requires no Tx to be send to the network. A method such as `Transact` does require a Tx and thus will be flagged `false`. Input specifies the required input parameters for this gives method.
func (Method) ID ¶
ID returns the canonical representation of the method's signature used by the abi definition to identify method names and types.
type Type ¶
type Type struct { Elem *Type Kind reflect.Kind Type reflect.Type Size int T byte // Our own type checking // Tuple relative fields TupleRawName string // Raw struct name defined in source code, may be empty. TupleElems []*Type // Type information of all tuple fields TupleRawNames []string // Raw field name of all tuple fields // contains filtered or unexported fields }
Type is the reflection of the supported argument type