abi

package
v1.68.4 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: BSD-3-Clause Imports: 17 Imported by: 18

Documentation

Index

Examples

Constants

View Source
const (
	IntTy byte = iota
	UintTy
	BoolTy
	StringTy
	SliceTy
	ArrayTy
	TupleTy
	AddressTy
	FixedBytesTy
	BytesTy
	HashTy
	FixedPointTy
	FunctionTy
)

Type enumerator

Variables

View Source
var (
	// MaxUint256 is the maximum value that can be represented by a uint256
	MaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1)
	// MaxInt256 is the maximum value that can be represented by a int256
	MaxInt256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 255), common.Big1)
)

Functions

func MakeTopics added in v1.65.1

func MakeTopics(query ...[]interface{}) ([][]common.Hash, error)

MakeTopics converts a filter query argument list into a filter topic set.

func Pack

func Pack(param, abiData string, readOnly bool) (methodName string, packData []byte, err error)

Pack 使用ABI方式调用时,将调用方式转换为EVM底层处理的十六进制编码 abiData 完整的ABI定义 param 调用方法及参数 readOnly 是否只读,如果调用的方法不为只读,则报错 调用方式: foo(param1,param2)

func PackContructorPara added in v1.65.3

func PackContructorPara(param, abiStr string) (packData []byte, err error)

func ParseTopics added in v1.65.1

func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error

ParseTopics converts the indexed topic fields into actual log field values.

func ParseTopicsIntoMap added in v1.65.1

func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error

ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs

func ProcFuncCall added in v1.67.0

func ProcFuncCall(param string) (funcName string, res []string, err error)

解析方法调用字符串,返回方法名以及方法参数 例如:foo(param1,param2) -> [foo,param1,param2]

func ReadFixedBytes added in v1.65.1

func ReadFixedBytes(t Type, word []byte) (interface{}, error)

ReadFixedBytes uses reflection to create a fixed array to be read from

func ReadInteger added in v1.65.1

func ReadInteger(typ Type, b []byte) interface{}

ReadInteger reads the integer based on its kind and returns the appropriate value

func Str2GoValue added in v1.67.0

func Str2GoValue(typ Type, val string) (res interface{}, err error)

从字符串格式的输入参数取值(单个),获取Go类型的

func ToCamelCase added in v1.65.1

func ToCamelCase(input string) string

ToCamelCase converts an under-score string to a camel-case string

func U256

func U256(n *big.Int) []byte

U256 converts a big Int into a 256bit EVM number.

func UnpackRevert added in v1.65.1

func UnpackRevert(data []byte) (string, error)

UnpackRevert 解包转换为string

Types

type ABI

type ABI struct {
	Constructor Method
	Methods     map[string]Method
	Events      map[string]Event

	// Additional "special" functions introduced in solidity v0.6.0.
	// It's separated from the original default fallback. Each contract
	// can only define one fallback and receive function.
	Fallback Method // Note it's also used to represent legacy fallback before v0.6.0
	Receive  Method
}

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

func JSON(reader io.Reader) (ABI, error)

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 added in v1.65.1

func (abi *ABI) EventByID(topic common.Hash) (*Event, error)

EventByID looks an event up by its topic hash in the ABI and returns nil if none found.

func (*ABI) HasFallback added in v1.65.1

func (abi *ABI) HasFallback() bool

HasFallback returns an indicator whether a fallback function is included.

func (*ABI) HasReceive added in v1.65.1

func (abi *ABI) HasReceive() bool

HasReceive returns an indicator whether a receive function is included.

func (*ABI) MethodByID

func (abi *ABI) MethodByID(sigdata []byte) (*Method, error)

MethodByID looks up a method by the 4-byte id returns nil if none found

func (ABI) Pack

func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error)

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

func (abi *ABI) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface

func (ABI) Unpack

func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error)

Unpack output in v according to the abi specification

func (ABI) UnpackIntoMap added in v1.65.1

func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error)

UnpackIntoMap unpacks a log into the provided map[string]interface{}

type Argument

type Argument struct {
	Name    string
	Type    Type
	Indexed bool // indexed is only used by events
}

Argument holds the name of the argument and the corresponding type. Types are used when packing and testing arguments.

func (*Argument) UnmarshalJSON

func (argument *Argument) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface

type ArgumentMarshaling added in v1.65.1

type ArgumentMarshaling struct {
	Name         string
	Type         string
	InternalType string
	Components   []ArgumentMarshaling
	Indexed      bool
}

ArgumentMarshaling 参数数列化结构体

type Arguments

type Arguments []Argument

Arguments 参数

func (Arguments) LengthNonIndexed

func (arguments Arguments) LengthNonIndexed() int

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

func (arguments Arguments) NonIndexed() Arguments

NonIndexed returns the arguments with indexed arguments filtered out

func (Arguments) Pack

func (arguments Arguments) Pack(args ...interface{}) ([]byte, error)

Pack performs the operation Go format -> Hexdata

func (Arguments) PackValues

func (arguments Arguments) PackValues(args []interface{}) ([]byte, error)

PackValues performs the operation Go format -> Hexdata It is the semantic opposite of UnpackValues

func (Arguments) Unpack

func (arguments Arguments) Unpack(v interface{}, data []byte) error

Unpack performs the operation hexdata -> Go format

func (Arguments) UnpackIntoMap added in v1.65.1

func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error

UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value

func (Arguments) UnpackValues

func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error)

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

	// Sig contains the string signature according to the ABI spec.
	// e.g.	 event foo(uint32 a, int b) = "foo(uint32,int256)"
	// Please note that "int" is substitute for its canonical representation "int256"
	Sig string
	// ID returns the canonical representation of the event's signature used by the
	// abi definition to identify event names and types.
	ID common.Hash
	// contains filtered or unexported fields
}

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 NewEvent added in v1.65.1

func NewEvent(name, rawName string, anonymous bool, inputs Arguments) Event

NewEvent creates a new Event. It sanitizes the input arguments to remove unnamed arguments. It also precomputes the id, signature and string representation of the event.

func (Event) String

func (e Event) String() string

type FunctionType added in v1.65.1

type FunctionType int

FunctionType represents different types of functions a contract might have.

const (
	// Constructor represents the constructor of the contract.
	// The constructor function is called while deploying a contract.
	Constructor FunctionType = iota
	// Fallback represents the fallback function.
	// This function is executed if no other function matches the given function
	// signature and no receive function is specified.
	Fallback
	// Receive represents the receive function.
	// This function is executed on plain Ether transfers.
	Receive
	// Function represents a normal function.
	Function
)

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 string // RawName is the raw method name parsed from ABI

	// Type indicates whether the method is a
	// special fallback introduced in solidity v0.6.0
	Type FunctionType

	// StateMutability indicates the mutability state of method,
	// the default value is nonpayable. It can be empty if the abi
	// is generated by legacy compiler.
	StateMutability string

	// Legacy indicators generated by compiler before v0.6.0
	Constant bool
	Payable  bool

	Inputs  Arguments
	Outputs Arguments

	// Sig returns the methods string signature according to the ABI spec.
	// e.g.		function foo(uint32 a, int b) = "foo(uint32,int256)"
	// Please note that "int" is substitute for its canonical representation "int256"
	Sig string
	// ID returns the canonical representation of the method's signature used by the
	// abi definition to identify method names and types.
	ID []byte
	// contains filtered or unexported fields
}

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 NewMethod added in v1.65.1

func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method

NewMethod creates a new Method. A method should always be created using NewMethod. It also precomputes the sig representation and the string representation of the method.

func (Method) IsConstant added in v1.65.1

func (method Method) IsConstant() bool

IsConstant returns the indicator whether the method is read-only.

func (Method) IsPayable added in v1.65.1

func (method Method) IsPayable() bool

IsPayable returns the indicator whether the method can process plain ether transfers.

func (Method) String

func (method Method) String() string

type Param

type Param struct {
	// Name 参数名称
	Name string `json:"name"`
	// Type 参数类型
	Type string `json:"type"`
	// Value 参数取值
	Value interface{} `json:"value"`
}

Param 返回值参数结构定义

func Unpack

func Unpack(data []byte, methodName, abiData string) (output []*Param, err error)

Unpack 将调用返回结果按照ABI的格式序列化为json data 合约方法返回值 abiData 完整的ABI定义

func UnpackAllTypes added in v1.66.0

func UnpackAllTypes(data []byte, name, abiData string) (output []*Param, err error)

同时支持input,output和event三种数据的unpack

type Type

type Type struct {
	Elem *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
	TupleType     reflect.Type // Underlying struct of the tuple
	// contains filtered or unexported fields
}

Type is the reflection of the supported argument type

func NewType

func NewType(t string, internalType string, components []ArgumentMarshaling) (typ Type, err error)

NewType creates a new reflection type of abi type given in t.

func (Type) GetType added in v1.65.1

func (t Type) GetType() reflect.Type

GetType returns the reflection type of the ABI type.

func (Type) String

func (t Type) String() (out string)

String implements Stringer

Jump to

Keyboard shortcuts

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