bind

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0, GPL-3.0 Imports: 27 Imported by: 43

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 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).
	ErrNoCode = errors.New("no contract code at given address")

	// This error is raised when attempting to perform a pending state action
	// on a backend that doesn't implement PendingContractCaller.
	ErrNoPendingState = errors.New("backend does not support pending state")

	// This error is returned by WaitDeployed if contract creation leaves an
	// empty contract behind.
	ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
)

Functions

func AsyncDeployContract added in v0.10.0

func AsyncDeployContract(opts *TransactOpts, handler func(*types.Receipt, error), abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (*types.Transaction, error)

func Bind

func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, smcrypto bool) (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.

func WaitDeployed

func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error)

WaitDeployed waits for a contract deployment transaction and returns the on-chain contract address when it is mined. It stops waiting when ctx is canceled.

func WaitMined

func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error)

WaitMined waits for tx to be mined on the blockchain. It stops waiting when the context is canceled.

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 DeployContractGetReceipt added in v0.11.0

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

func NewBoundContract

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

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

func NewUnsignedContract added in v1.1.0

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

NewUnsignedContract

func NewUnsignedTx added in v1.1.0

func NewUnsignedTx(opts *TransactOpts, addr common.Address, abi abi.ABI, backend ContractBackend, method string, params ...interface{}) (*types.Transaction, *BoundContract, error)

NewUnsignedTx generate raw transaction

func (*BoundContract) AsyncTransact added in v0.10.0

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

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) GetTransactor added in v1.1.0

func (c *BoundContract) GetTransactor() ContractTransactor

func (*BoundContract) Transact

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

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

func (*BoundContract) TransactWithResult added in v1.1.0

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

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

func (*BoundContract) Transfer

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

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

func (*BoundContract) UnpackLog

func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error

UnpackLog unpacks a retrieved log into the provided output structure.

func (*BoundContract) UnpackLogIntoMap

func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error

UnpackLogIntoMap unpacks a retrieved log into the provided map.

func (*BoundContract) WatchLogs

func (c *BoundContract) WatchLogs(fromBlock *uint64, handler func(int, []types.Log), name string, query ...interface{}) (string, error)

WatchLogs filters subscribes to contract logs for future blocks, returning a subscription object that can be used to tear down the watcher.

type CallOpts

type CallOpts struct {
	Pending     bool            // Whether to operate on the pending state or the last known one
	From        common.Address  // Optional the sender address, otherwise the first account is used
	BlockNumber *big.Int        // Optional the block number on which the call should be performed
	Context     context.Context // Network context to support cancellation and timeouts (nil = no timeout)
}

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

type ContractBackend

type ContractBackend interface {
	ContractCaller
	ContractTransactor
	ContractFilterer
}

ContractBackend defines the methods needed to work with contracts on a read-write basis.

type ContractCaller

type ContractCaller interface {
	// CodeAt returns the code of the given account. This is needed to differentiate
	// between contract internal errors and the local chain being out of sync.
	CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
	// ContractCall executes a Solidity contract call with the specified data as the
	// input.
	CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
}

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

type ContractFilterer

type ContractFilterer interface {
	// SubscribeEventLogs creates a background log filtering operation, returning
	// a subscription immediately, which can be used to stream the found events.
	SubscribeEventLogs(eventLogParams types.EventLogParams, handler func(int, []types.Log)) (string, error)
	UnSubscribeEventLogs(filterID string) error
}

ContractFilterer defines the methods needed to access log events using one-off queries or continuous event subscriptions.

type ContractTransactor

type ContractTransactor interface {
	// PendingCodeAt returns the code of the given account in the pending state.
	PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
	// SendTransaction injects the transaction into the pending pool for execution.
	SendTransaction(ctx context.Context, tx *types.Transaction) (*types.Receipt, error)
	AsyncSendTransaction(ctx context.Context, tx *types.Transaction, handler func(*types.Receipt, error)) error
	// GetBlockLimit returns the blocklimit for current blocknumber
	GetBlockLimit(ctx context.Context) (*big.Int, error)
	// GetGroupID returns the groupID of the client
	GetGroupID() *big.Int
	// GetChainID returns the chainID of the blockchain
	GetChainID(ctx context.Context) (*big.Int, error)
	// GetContractAddress returns the contract address once it was deployed
	GetContractAddress(ctx context.Context, txHash common.Hash) (common.Address, error)
	// SMCrypto returns true if use sm crypto
	SMCrypto() bool
}

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 DeployBackend

type DeployBackend interface {
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
}

DeployBackend wraps the operations needed by WaitMined and WaitDeployed.

type FilterOpts

type FilterOpts struct {
	Start uint64  // Start of the queried range
	End   *uint64 // End of the range (nil = latest)

	Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
}

FilterOpts is the collection of options to fine tune filtering for events within a bound contract.

type Lang

type Lang int

Lang is a target programming language selector to generate bindings for.

const (
	LangGo Lang = iota
	LangJava
	LangObjC
	LangObjCHeader
)

type PendingContractCaller

type PendingContractCaller interface {
	// PendingCodeAt returns the code of the given account in the pending state.
	PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
	// PendingCallContract executes a Solidity contract call against the pending state.
	PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
}

PendingContractCaller defines methods to perform contract calls on the pending state. Call will try to discover this interface when access to the pending state is requested. If the backend does not support the pending state, Call returns ErrNoPendingState.

type SignerFn

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 (0 = estimate)
	Context  context.Context // Network context to support cancellation and timeouts (nil = no timeout)
}

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

func NewKeyStoreTransactor

func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error)

NewKeyStoreTransactor is a utility method to easily create a transaction signer from an decrypted key from a keystore

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 NewSMCryptoTransactor

func NewSMCryptoTransactor(sm2Key []byte) *TransactOpts

NewSMCryptoTransactor is a utility method to easily create a transaction signer from a single sm2p256v1 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.

type WatchOpts

type WatchOpts struct {
	Start   *uint64         // Start of the queried range (nil = latest)
	Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
}

WatchOpts is the collection of options to fine tune subscribing for events within a bound contract.

Jump to

Keyboard shortcuts

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