abi

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2016 License: GPL-3.0 Imports: 10 Imported by: 0

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

Examples

Constants

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

Variables

This section is empty.

Functions

func S256 added in v0.9.39

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

func S2S256 added in v0.9.39

func S2S256(n int64) []byte

func U256 added in v0.9.39

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

U256 will ensure unsigned 256bit on big nums

func U2U256 added in v0.9.39

func U2U256(n uint64) []byte

S256 will ensure signed 256bit on big nums

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

func (ABI) Unpack

func (abi ABI) Unpack(v interface{}, name string, output []byte) 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 (a *Argument) UnmarshalJSON(data []byte) error

type Event

type Event struct {
	Name   string
	Inputs []Argument
}

Event is an event potentially triggered by the EVM's LOG mechanism. The Event holds type information (inputs) about the yielded output

func (Event) Id added in v1.4.0

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.

type Method

type Method struct {
	Name    string
	Const   bool
	Inputs  []Argument
	Outputs []Argument
}

Callable method 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 added in v0.9.39

func (m Method) Id() []byte

func (Method) Sig

func (m 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 (m Method) String() string

type Type

type Type struct {
	IsSlice, IsArray bool
	SliceSize        int

	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

Directories

Path Synopsis
Package bind generates Ethereum contract Go bindings.
Package bind generates Ethereum contract Go bindings.

Jump to

Keyboard shortcuts

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