abi

package
v6.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

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

Type enumerator

Variables

This section is empty.

Functions

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 U256

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

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

func Unpack

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

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

Types

type ABI

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

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 {
	log.Fatalln(err)
}
out, err := abi.Pack("isBar", common.HexToAddress("01"))
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%x\n", out)
Output:

1f2c40920000000000000000000000000000000000000000000000000000000000000001

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

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 Arguments

type Arguments []Argument

Arguments Argument slice type

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) 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      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

func (e Event) ID() common.Hash

ID returns the canonical representation of the event's signature used by the abi definition to identify event names and types.

func (Event) String

func (e Event) String() string

type Method

type Method struct {
	Name    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 therefor requires no Tx to be send to the network. A method such as `Transact` does require a Tx and thus will be flagged `true`. Input specifies the required input parameters for this gives method.

func (Method) ID

func (method Method) ID() []byte

ID method name hash

func (Method) Sig

func (method Method) Sig() string

Sig returns the methods string signature according to the ABI spec.

Example

function foo(uint32 a, int b)    =    "foo(uint32,int256)"

Please note that "int" is substitute for its canonical representation "int256"

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 返回值参数结构定义

type Type

type Type struct {
	Elem *Type

	Kind reflect.Kind
	Type reflect.Type
	Size int
	T    byte // Our own type checking
	// contains filtered or unexported fields
}

Type is the reflection of the supported argument type

func NewType

func NewType(t string) (typ Type, err error)

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

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