mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-03-17 00:20:39 +00:00
This PR implements a new version of the abigen utility (v2) which exists
along with the pre-existing v1 version.
Abigen is a utility command provided by go-ethereum that, given a
solidity contract ABI definition, will generate Go code to transact/call
the contract methods, converting the method parameters/results and
structures defined in the contract into corresponding Go types. This is
useful for preventing the need to write custom boilerplate code for
contract interactions.
Methods in the generated bindings perform encoding between Go types and
Solidity ABI-encoded packed bytecode, as well as some action (e.g.
`eth_call` or creating and submitting a transaction). This limits the
flexibility of how the generated bindings can be used, and prevents
easily adding new functionality, as it will make the generated bindings
larger for each feature added.
Abigen v2 was conceived of by the observation that the only
functionality that generated Go bindings ought to perform is conversion
between Go types and ABI-encoded packed data. Go-ethereum already
provides various APIs which in conjunction with conversion methods
generated in v2 bindings can cover all functionality currently provided
by v1, and facilitate all other previously-desired use-cases.
## Generating Bindings
To generate contract bindings using abigen v2, invoke the `abigen`
command with the `--v2` flag. The functionality of all other flags is
preserved between the v2 and v1 versions.
## What is Generated in the Bindings
The execution of `abigen --v2` generates Go code containing methods
which convert between Go types and corresponding ABI-encoded data
expected by the contract. For each input-accepting contract method and
the constructor, a "packing" method is generated in the binding which
converts from Go types to the corresponding packed solidity expected by
the contract. If a method returns output, an "unpacking" method is
generated to convert this output from ABI-encoded data to the
corresponding Go types.
For contracts which emit events, an unpacking method is defined for each
event to unpack the corresponding raw log to the Go type that it
represents.
Likewise, where custom errors are defined by contracts, an unpack method
is generated to unpack raw error data into a Go type.
## Using the Generated Bindings
For a smooth user-experience, abigen v2 comes with a number of utility
functions to be used in conjunction with the generated bindings for
performing common contract interaction use-cases. These include:
* filtering for historical logs of a given topic
* watching the chain for emission of logs with a given topic
* contract deployment methods
* Call/Transact methods
https://geth.ethereum.org will be updated to include a new tutorial page
for abigen v2 with full code examples. The page currently exists in a
PR: https://github.com/ethereum/go-ethereum/pull/31390 .
There are also extensive examples of interactions with contract bindings
in [test
cases](cc855c7ede/accounts/abi/bind/v2/lib_test.go)
provided with this PR.
---------
Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
Co-authored-by: Felix Lange <fjl@twurst.com>
133 lines
5.9 KiB
Go
133 lines
5.9 KiB
Go
// Copyright 2015 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package bind
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math/big"
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
)
|
|
|
|
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. self-destructed).
|
|
ErrNoCode = errors.New("no contract code at given address")
|
|
|
|
// ErrNoPendingState 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")
|
|
|
|
// ErrNoBlockHashState is raised when attempting to perform a block hash action
|
|
// on a backend that doesn't implement BlockHashContractCaller.
|
|
ErrNoBlockHashState = errors.New("backend does not support block hash state")
|
|
|
|
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
|
// an empty contract behind.
|
|
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
|
|
|
|
// ErrNoAddressInReceipt is returned by WaitDeployed when the receipt for the
|
|
// transaction hash does not contain a contract address. This error may indicate
|
|
// that the transaction hash was not a CREATE transaction.
|
|
ErrNoAddressInReceipt = errors.New("no contract address in receipt")
|
|
)
|
|
|
|
// ContractCaller defines the methods needed to allow operating with a contract on a read
|
|
// only basis.
|
|
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)
|
|
|
|
// CallContract executes an Ethereum contract call with the specified data as the
|
|
// input.
|
|
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]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 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 an Ethereum contract call against the pending state.
|
|
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
|
|
}
|
|
|
|
// BlockHashContractCaller defines methods to perform contract calls on a specific block hash.
|
|
// Call will try to discover this interface when access to a block by hash is requested.
|
|
// If the backend does not support the block hash state, Call returns ErrNoBlockHashState.
|
|
type BlockHashContractCaller interface {
|
|
// CodeAtHash returns the code of the given account in the state at the specified block hash.
|
|
CodeAtHash(ctx context.Context, contract common.Address, blockHash common.Hash) ([]byte, error)
|
|
|
|
// CallContractAtHash executes an Ethereum contract call against the state at the specified block hash.
|
|
CallContractAtHash(ctx context.Context, call ethereum.CallMsg, blockHash common.Hash) ([]byte, error)
|
|
}
|
|
|
|
// ContractTransactor defines the methods needed to allow operating with a contract
|
|
// on a write only basis. Besides 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 ContractTransactor interface {
|
|
ethereum.GasEstimator
|
|
ethereum.GasPricer
|
|
ethereum.GasPricer1559
|
|
ethereum.TransactionSender
|
|
|
|
// HeaderByNumber returns a block header from the current canonical chain. If
|
|
// number is nil, the latest known header is returned.
|
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
|
|
|
// PendingCodeAt returns the code of the given account in the pending state.
|
|
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
|
|
|
|
// PendingNonceAt retrieves the current pending nonce associated with an account.
|
|
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
|
}
|
|
|
|
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
|
|
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)
|
|
}
|
|
|
|
// ContractFilterer defines the methods needed to access log events using one-off
|
|
// queries or continuous event subscriptions.
|
|
type ContractFilterer interface {
|
|
ethereum.LogFilterer
|
|
}
|
|
|
|
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
|
|
type ContractBackend interface {
|
|
ContractCaller
|
|
ContractTransactor
|
|
ContractFilterer
|
|
}
|
|
|
|
// Backend combines all backend methods used in this package. This type is provided for
|
|
// convenience. It is meant to be used when you need to hold a reference to a backend that
|
|
// is used for both deployment and contract interaction.
|
|
type Backend interface {
|
|
DeployBackend
|
|
ContractBackend
|
|
}
|