bind

package
v1.4.9 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2016 License: GPL-3.0 Imports: 18 Imported by: 2

Documentation

Overview

Package bind generates Ethereum contract Go bindings.

Detailed usage document and tutorial available on the go-ethereum Wiki page: https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCode = errors.New("no contract code at given address")

ErrNoCode is returned by call and transact operations for which the requested recipient contract to operate on does not exist in the state db or does not have any code associated with it (i.e. suicided).

Functions

func Bind

func Bind(types []string, abis []string, bytecodes []string, pkg string) (string, error)

Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant to be used as is in client code, but rather as an intermediate struct which enforces compile time type safety and naming convention opposed to having to manually maintain hard coded strings that break on runtime.

Types

type BoundContract

type BoundContract struct {
	// contains filtered or unexported fields
}

BoundContract is the base wrapper object that reflects a contract on the Ethereum network. It contains a collection of methods that are used by the higher level contract bindings to operate.

func DeployContract

func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error)

DeployContract deploys a contract onto the Ethereum blockchain and binds the deployment address with a Go wrapper.

func NewBoundContract

func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor) *BoundContract

NewBoundContract creates a low level contract interface through which calls and transactions may be made through.

func (*BoundContract) Call

func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, params ...interface{}) error

Call invokes the (constant) contract method with params as input values and sets the output to result. The result type might be a single field for simple returns, a slice of interfaces for anonymous returns and a struct for named returns.

func (*BoundContract) Transact

func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error)

Transact invokes the (paid) contract method with params as input values.

func (*BoundContract) Transfer

func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error)

Transfer initiates a plain transaction to move funds to the contract, calling its default method if one is available.

type CallOpts

type CallOpts struct {
	Pending bool // Whether to operate on the pending state or the last known one
}

CallOpts is the collection of options to fine tune a contract call request.

type ContractBackend

type ContractBackend interface {
	// HasCode checks if the contract at the given address has any code associated
	// with it or not. This is needed to differentiate between contract internal
	// errors and the local chain being out of sync.
	HasCode(contract common.Address, pending bool) (bool, error)

	// ContractCall executes an Ethereum contract call with the specified data as
	// the input. The pending flag requests execution against the pending block, not
	// the stable head of the chain.
	ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error)

	// PendingAccountNonce retrieves the current pending nonce associated with an
	// account.
	PendingAccountNonce(account common.Address) (uint64, error)

	// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
	// execution of a transaction.
	SuggestGasPrice() (*big.Int, error)

	// EstimateGasLimit tries to estimate the gas needed to execute a specific
	// transaction based on the current pending state of the backend blockchain.
	// There is no guarantee that this is the true gas limit requirement as other
	// transactions may be added or removed by miners, but it should provide a basis
	// for setting a reasonable default.
	EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)

	// SendTransaction injects the transaction into the pending pool for execution.
	SendTransaction(tx *types.Transaction) error
}

ContractBackend defines the methods needed to allow operating with contract on a read-write basis.

This interface is essentially the union of ContractCaller and ContractTransactor but due to a bug in the Go compiler (https://github.com/golang/go/issues/6977), we cannot simply list it as the two interfaces. The other solution is to add a third interface containing the common methods, but that convolutes the user API as it introduces yet another parameter to require for initialization.

type ContractCaller

type ContractCaller interface {
	// HasCode checks if the contract at the given address has any code associated
	// with it or not. This is needed to differentiate between contract internal
	// errors and the local chain being out of sync.
	HasCode(contract common.Address, pending bool) (bool, error)

	// ContractCall executes an Ethereum contract call with the specified data as
	// the input. The pending flag requests execution against the pending block, not
	// the stable head of the chain.
	ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error)
}

ContractCaller defines the methods needed to allow operating with contract on a read only basis.

type ContractTransactor

type ContractTransactor interface {
	// PendingAccountNonce retrieves the current pending nonce associated with an
	// account.
	PendingAccountNonce(account common.Address) (uint64, error)

	// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
	// execution of a transaction.
	SuggestGasPrice() (*big.Int, error)

	// HasCode checks if the contract at the given address has any code associated
	// with it or not. This is needed to differentiate between contract internal
	// errors and the local chain being out of sync.
	HasCode(contract common.Address, pending bool) (bool, error)

	// EstimateGasLimit tries to estimate the gas needed to execute a specific
	// transaction based on the current pending state of the backend blockchain.
	// There is no guarantee that this is the true gas limit requirement as other
	// transactions may be added or removed by miners, but it should provide a basis
	// for setting a reasonable default.
	EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)

	// SendTransaction injects the transaction into the pending pool for execution.
	SendTransaction(tx *types.Transaction) error
}

ContractTransactor defines the methods needed to allow operating with contract on a write only basis. Beside the transacting method, the remainder are helpers used when the user does not provide some needed values, but rather leaves it up to the transactor to decide.

type SignerFn

type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)

SignerFn is a signer function callback when a contract requires a method to sign the transaction before submission.

type TransactOpts

type TransactOpts struct {
	From   common.Address // Ethereum account to send the transaction from
	Nonce  *big.Int       // Nonce to use for the transaction execution (nil = use pending state)
	Signer SignerFn       // Method to use for signing the transaction (mandatory)

	Value    *big.Int // Funds to transfer along along the transaction (nil = 0 = no funds)
	GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
	GasLimit *big.Int // Gas limit to set for the transaction execution (nil = estimate + 10%)
}

TransactOpts is the collection of authorization data required to create a valid Ethereum transaction.

func NewKeyedTransactor

func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts

NewKeyedTransactor is a utility method to easily create a transaction signer from a single private key.

func NewTransactor

func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error)

NewTransactor is a utility method to easily create a transaction signer from an encrypted json key stream and the associated passphrase.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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