go-ethereum/core
Daniel Liu 05a9e91898
cmd/abigen, accounts/abi/bind: implement abigen version 2 #31379 (#1416)
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.

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.

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.

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: jwasinger <j-wasinger@hotmail.com>
Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
Co-authored-by: Felix Lange <fjl@twurst.com>
2025-09-03 15:47:12 +08:00
..
asm all: use strings.EqualFold for string comparison #24890 (#1134) 2025-06-23 16:00:32 +08:00
bloombits fix: replace all time.after with the time.newtimer (#400) 2024-01-27 21:01:24 +11:00
rawdb all: snapshot dump + unify with trie dump #22795 (#1205) 2025-08-22 16:39:28 +08:00
state cmd/abigen, accounts/abi/bind: implement abigen version 2 #31379 (#1416) 2025-09-03 15:47:12 +08:00
txpool all: use big.Sign to compare with zero #29490 (#1387) 2025-08-28 18:56:39 +08:00
types all: introduce trie owner notion #24750 (#1090) 2025-08-22 16:37:39 +08:00
vm all: mv loggers to eth/tracers #23892 (#1269) 2025-09-03 15:35:28 +08:00
.gitignore Renamed chain => core 2014-12-04 10:28:02 +01:00
bench_test.go core: move genesis alloc types to core/types (#29003) 2025-01-24 16:54:12 +08:00
block_validator.go core: show db error-info in case of mismatched hash root #26870 (#1220) 2025-08-04 11:51:47 +08:00
block_validator_test.go all: remove ethash pow 27178 (#1378) 2025-08-26 11:54:01 +08:00
blockchain.go core, eth, trie: use TryGetAccount to read what TryUpdateAccount has written #25458 (#1106) 2025-09-03 15:34:11 +08:00
blockchain_insert.go all: simplify timestamps to uint64 #19372 (#1318) 2025-08-08 10:33:37 +08:00
blockchain_test.go eth: skip VerifyHeader in traceBlock to fix #1185 (#1265) 2025-07-26 18:18:50 +08:00
blocks.go new EVM Upgrade 2021-09-21 16:53:46 +05:30
chain_indexer.go core: use atomic type #27011 2025-04-28 16:57:56 +08:00
chain_indexer_test.go light: CHT and bloom trie indexers working in light mode (#16534) 2025-03-06 15:30:48 +08:00
chain_makers.go all: simplify timestamps to uint64 #19372 (#1318) 2025-08-08 10:33:37 +08:00
chain_makers_test.go core: move genesis alloc types to core/types (#29003) 2025-01-24 16:54:12 +08:00
dao_test.go core: improve commit genesis 2025-03-20 14:15:09 +08:00
error.go all: improve EstimateGas API (#20830) 2024-12-21 14:35:44 +08:00
events.go eth/filters: remove use of event.TypeMux for pending logs (#20312) 2024-08-03 10:03:22 +08:00
evm.go core/vm: make time field use uint64 #26474 (#1319) 2025-08-20 15:22:06 +08:00
gaspool.go miner: avoid unnecessary work (#15883) 2018-01-15 12:57:06 +02:00
gen_genesis.go core: move genesis alloc types to core/types (#29003) 2025-01-24 16:54:12 +08:00
genesis.go core: fix deprecation comment for GenesisAccount #29218 (#1323) 2025-08-08 10:35:58 +08:00
genesis_alloc_devnet.go core: tidy up genesis alloc 2025-02-19 14:15:09 +08:00
genesis_alloc_mainnet.go core: tidy up genesis alloc 2025-02-19 14:15:09 +08:00
genesis_alloc_testnet.go core: tidy up genesis alloc 2025-02-19 14:15:09 +08:00
genesis_test.go core, light, params: clean genesis hash 2025-03-05 14:30:39 +08:00
headerchain.go all: simplify timestamps to uint64 #19372 (#1318) 2025-08-08 10:33:37 +08:00
mkalloc.go accounts, cmd/geth, core: close opened files (#29598) 2025-01-24 16:54:12 +08:00
sender_cacher.go all: refactor txpool into it's own package in prep for 4844 (#26038) 2024-11-01 11:36:53 +08:00
state_prefetcher.go core: use atomic type #27011 2025-04-28 16:57:56 +08:00
state_processor.go core: fill blockNumber in logs #26345 (#1215) 2025-07-11 10:53:48 +08:00
state_processor_test.go all: simplify timestamps to uint64 #19372 (#1318) 2025-08-08 10:33:37 +08:00
state_transition.go core/vm: set basefee to 0 internally on eth_call #28470 (#1238) 2025-07-17 17:54:23 +08:00
token_validator.go accounts/abi: ABI explicit difference between Unpack and UnpackIntoInterface (#21091) 2025-01-24 16:18:30 +08:00
types.go core: use atomic type #27011 2025-04-28 16:57:56 +08:00