still in a wip state

pull/26782/head
Jared Wasinger 4 days ago
parent a2479e1aed
commit 46e6dd4fcd
  1. 16
      accounts/abi/bind/base.go
  2. 44
      accounts/abi/bind/bind.go
  3. 8
      accounts/abi/bind/lib.go
  4. 3
      accounts/abi/bind/template.go
  5. 23
      accounts/abi/bind/template2.go
  6. 44
      accounts/abi/bind/testdata/v2_testcase_library/contract.sol
  7. 254
      accounts/abi/bind/v2/lib.go
  8. 36
      accounts/abi/bind/v2/v2_test.go

@ -151,6 +151,18 @@ func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend Co
return c.address, tx, c, nil return c.address, tx, c, nil
} }
func DeployContractRaw(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, packedParams []byte) (common.Address, *types.Transaction, *BoundContract, error) {
// Otherwise try to deploy the contract
c := NewBoundContract(common.Address{}, abi, backend, backend, backend)
tx, err := c.transact(opts, nil, append(bytecode, packedParams...))
if err != nil {
return common.Address{}, nil, nil, err
}
c.address = crypto.CreateAddress(opts.From, tx.Nonce())
return c.address, tx, c, nil
}
// Call invokes the (constant) contract method with params as input values and // 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 // 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, a slice of interfaces for anonymous returns and a struct for named
@ -179,6 +191,10 @@ func (c *BoundContract) Call(opts *CallOpts, results *[]interface{}, method stri
return c.abi.UnpackIntoInterface(res[0], method, output) return c.abi.UnpackIntoInterface(res[0], method, output)
} }
func (c *BoundContract) CallRaw(opts *CallOpts, input []byte) ([]byte, error) {
return c.call(opts, input)
}
func (c *BoundContract) call(opts *CallOpts, input []byte) ([]byte, error) { func (c *BoundContract) call(opts *CallOpts, input []byte) ([]byte, error) {
// Don't crash on a lazy user // Don't crash on a lazy user
if opts == nil { if opts == nil {

@ -322,7 +322,9 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
Receive: receive, Receive: receive,
Events: events, Events: events,
Libraries: make(map[string]string), Libraries: make(map[string]string),
AllLibraries: make(map[string]string),
} }
// Function 4-byte signatures are stored in the same sequence // Function 4-byte signatures are stored in the same sequence
// as types, if available. // as types, if available.
if len(fsigs) > i { if len(fsigs) > i {
@ -340,7 +342,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
if _, ok := isLib[name]; !ok { if _, ok := isLib[name]; !ok {
isLib[name] = struct{}{} isLib[name] = struct{}{}
} }
}
} }
} }
// Check if that type has already been identified as a library // Check if that type has already been identified as a library
@ -348,6 +350,46 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
_, ok := isLib[types[i]] _, ok := isLib[types[i]]
contracts[types[i]].Library = ok contracts[types[i]].Library = ok
} }
// recursively traverse the library dependency graph
// of the contract, flattening it into a list.
//
// For abigenv2, we do not generate contract deploy
// methods (which in v1 recursively deploy their
// library dependencies). So, the entire set of
// library dependencies is required, and we will
// the order to deploy and link them at runtime.
var findDeps func(contract *tmplContract) map[string]struct{}
findDeps = func(contract *tmplContract) map[string]struct{} {
// 1) match all libraries that this contract depends on
re, err := regexp.Compile("__\\$([a-f0-9]+)\\$__")
if err != nil {
panic(err)
}
libBin := contracts[contract.Type].InputBin
matches := re.FindAllStringSubmatch(libBin, -1)
var result map[string]struct{}
// 2) recurse, gathering nested library dependencies
for _, match := range matches {
pattern := match[1]
result[pattern] = struct{}{}
depContract := contracts[pattern]
for subPattern, _ := range findDeps(depContract) {
result[subPattern] = struct{}{}
}
}
return result
}
// take the set of library patterns, convert it to a map of pattern -> type
deps := findDeps(contracts[types[i]])
contracts[types[i]].AllLibraries = make(map[string]string)
for contractPattern, _ := range deps {
contractType := libs[contractPattern]
contracts[types[i]].AllLibraries[contractType] = contractPattern
}
}
// Generate the contract template data content and render it // Generate the contract template data content and render it
data := &tmplData{ data := &tmplData{
Package: pkg, Package: pkg,

@ -17,7 +17,6 @@
package bind package bind
import ( import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -30,10 +29,5 @@ type ContractInstance interface {
type ContractInstanceV2 interface { type ContractInstanceV2 interface {
Address() common.Address Address() common.Address
} Backend() ContractBackend
func CallRaw(instance ContractInstance, opts *CallOpts, input []byte) ([]byte, error) {
backend := instance.Backend()
c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend)
return c.call(opts, input)
} }

@ -42,7 +42,8 @@ type tmplContract struct {
Fallback *tmplMethod // Additional special fallback function Fallback *tmplMethod // Additional special fallback function
Receive *tmplMethod // Additional special receive function Receive *tmplMethod // Additional special receive function
Events map[string]*tmplEvent // Contract events accessors Events map[string]*tmplEvent // Contract events accessors
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs Libraries map[string]string // Same as tmplData, but filtered to only keep direct deps that the contract needs
AllLibraries map[string]string // same as Libraries, but all direct/indirect library dependencies
Library bool // Indicator whether the contract is a library Library bool // Indicator whether the contract is a library
} }

@ -38,6 +38,12 @@ var (
{{end}} {{end}}
{{range $contract := .Contracts}} {{range $contract := .Contracts}}
var {{$contract.Type}}LibraryDeps = map[string]*bind.MetaData{
{{range $pattern, $name := .Libraries}}
"{{$pattern}}": &{{$name}}MetaData,
{{end}}
}
// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract. // {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.
var {{.Type}}MetaData = &bind.MetaData{ var {{.Type}}MetaData = &bind.MetaData{
ABI: "{{.InputABI}}", ABI: "{{.InputABI}}",
@ -52,20 +58,14 @@ var (
{{end}} {{end}}
} }
// {{.Type}}Instance represents a deployed instance of the {{.Type}} contract.
type {{.Type}}Instance struct {
{{.Type}}
address common.Address // consider removing this, not clear what it's used for now (and why did we need custom deploy method on previous abi?)
}
func New{{.Type}}Instance(c *{{.Type}}, address common.Address) *{{.Type}}Instance {
return &{{.Type}}Instance{ {{$contract.Type}}: *c, address: address}
}
func (i *{{$contract.Type}}Instance) Address() common.Address { func (i *{{$contract.Type}}Instance) Address() common.Address {
return i.address return i.address
} }
func (i *{{$contract.Type}}Instance) Backend() bind.ContractBackend {
return i.backend
}
// {{.Type}} is an auto generated Go binding around an Ethereum contract. // {{.Type}} is an auto generated Go binding around an Ethereum contract.
type {{.Type}} struct { type {{.Type}} struct {
abi abi.ABI abi abi.ABI
@ -86,7 +86,8 @@ var (
return _{{$contract.Type}}.deployCode return _{{$contract.Type}}.deployCode
} }
func (_{{$contract.Type}} *{{$contract.Type}}) PackConstructor({{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) ([]byte, error) { // TODO: test constructor with inputs
func (_{{$contract.Type}} *{{$contract.Type}}) PackConstructor({{range .Constructor.Inputs}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ([]byte, error) {
return _{{$contract.Type}}.abi.Pack("" {{range .Constructor.Inputs}}, {{.Name}}{{end}}) return _{{$contract.Type}}.abi.Pack("" {{range .Constructor.Inputs}}, {{.Name}}{{end}})
} }

@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
library RecursiveDep {
function AddOne(uint256 val) public pure returns (uint256 ret) {
return val + 1;
}
}
// Array function to delete element at index and re-organize the array
// so that there are no gaps between the elements.
library Array {
using RecursiveDep for uint256;
function remove(uint256[] storage arr, uint256 index) public {
// Move the last element into the place to delete
require(arr.length > 0, "Can't remove from empty array");
arr[index] = arr[arr.length - 1];
arr[index] = arr[index].AddOne();
arr.pop();
}
}
contract TestArray {
using Array for uint256[];
uint256[] public arr;
function testArrayRemove(uint256 value) public {
for (uint256 i = 0; i < 3; i++) {
arr.push(i);
}
arr.remove(1);
assert(arr.length == 2);
assert(arr[0] == 0);
assert(arr[1] == 2);
}
constructor(uint256 foobar) {
}
}

@ -1,8 +1,6 @@
package v2 package v2
import ( import (
"context"
"errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
@ -10,57 +8,74 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"math/big" "regexp"
"strings"
) )
func FilterLogs[T any](instance bind.ContractInstance, opts *bind.FilterOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) { type ContractInstance struct {
backend := instance.Backend() Address common.Address
c := bind.NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) Backend bind.ContractBackend
logs, sub, err := c.FilterLogs(opts, eventID.String(), topics...)
if err != nil {
return nil, err
}
return &EventIterator[T]{unpack: unpack, logs: logs, sub: sub}, nil
} }
// WatchOpts is the collection of options to fine tune subscribing for events func DeployContracts(auth *bind.TransactOpts, backend bind.ContractBackend, constructorInput []byte, contracts map[string]*bind.MetaData) {
// within a bound contract. // match if the contract has dynamic libraries that need to be linked
type WatchOpts struct { hasDepsMatcher, err := regexp.Compile("__\\$.*\\$__")
Start *uint64 // Start of the queried range (nil = latest) if err != nil {
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout) panic(err)
} }
func watchLogs(backend V2Backend, address common.Address, opts *WatchOpts, eventID common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) { // deps we are linking
// Don't crash on a lazy user wipDeps := make(map[string]string)
if opts == nil { for id, metadata := range contracts {
opts = new(WatchOpts) wipDeps[id] = metadata.Bin
} }
// Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{eventID}}, query...)
topics, err := abi.MakeTopics(query...) // nested iteration: find contracts without library dependencies first,
// deploy them, link them into any other contracts that depend on them.
// repeat this until there are no more contracts to link/deploy
for {
for id, contractBin := range wipDeps {
if !hasDepsMatcher.MatchString(contractBin) {
// this library/contract doesn't depend on any others
// it can be deployed as-is.
abi, err := contracts[id].GetAbi()
if err != nil {
panic(err)
}
addr, _, _, err := bind.DeployContractRaw(auth, *abi, []byte(contractBin), backend, constructorInput)
if err != nil { if err != nil {
return nil, nil, err panic(err)
} }
// Start the background filtering delete(wipDeps, id)
logs := make(chan types.Log, 128)
config := ethereum.FilterQuery{ // embed the address of the deployed contract into any
Addresses: []common.Address{address}, // libraries/contracts that depend on it.
Topics: topics, for id, contractBin := range wipDeps {
contractBin = strings.ReplaceAll(contractBin, fmt.Sprintf("__$%s%__", id), fmt.Sprintf("__$%s$__", addr.String()))
wipDeps[id] = contractBin
}
}
} }
if opts.Start != nil { if len(wipDeps) == 0 {
config.FromBlock = new(big.Int).SetUint64(*opts.Start) break
} }
sub, err := backend.SubscribeFilterLogs(ensureContext(opts.Context), config, logs) }
}
func FilterLogs[T any](instance *ContractInstance, opts *bind.FilterOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.FilterLogs(opts, eventID.String(), topics...)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
return logs, sub, nil return &EventIterator[T]{unpack: unpack, logs: logs, sub: sub}, nil
} }
func WatchLogs[T any](address common.Address, backend V2Backend, opts *WatchOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) { func WatchLogs[T any](instance *ContractInstance, opts *bind.WatchOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
logs, sub, err := watchLogs(backend, address, opts, eventID, topics...) backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.WatchLogs(opts, eventID.String(), topics...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -166,167 +181,14 @@ func Transact(instance bind.ContractInstance, opts *bind.TransactOpts, input []b
return c.RawTransact(opts, input) return c.RawTransact(opts, input)
} }
// ensureContext is a helper method to ensure a context is not nil, even if the
// user specified it as such.
func ensureContext(ctx context.Context) context.Context {
if ctx == nil {
return context.Background()
}
return ctx
}
// SignerFn is a signer function callback when a contract requires a method to
// sign the transaction before submission.
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
// TransactOpts is the collection of authorization data required to create a
// valid Ethereum transaction.
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 the transaction (nil = 0 = no funds)
GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
GasFeeCap *big.Int // Gas fee cap to use for the 1559 transaction execution (nil = gas price oracle)
GasTipCap *big.Int // Gas priority fee cap to use for the 1559 transaction execution (nil = gas price oracle)
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
AccessList types.AccessList // Access list to set for the transaction execution (nil = no access list)
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
NoSend bool // Do all transact steps but do not send the transaction
}
func estimateGasLimit(backend V2Backend, address common.Hash, opts *TransactOpts, contract *common.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value *big.Int) (uint64, error) {
if contract != nil {
// Gas estimation cannot succeed without code for method invocations.
if code, err := backend.PendingCodeAt(ensureContext(opts.Context), address); err != nil {
return 0, err
} else if len(code) == 0 {
return 0, ErrNoCode
}
}
msg := ethereum.CallMsg{
From: opts.From,
To: contract,
GasPrice: gasPrice,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: value,
Data: input,
}
return backend.EstimateGas(ensureContext(opts.Context), msg)
}
func getNonce(backend V2Backend, opts *TransactOpts) (uint64, error) {
if opts.Nonce == nil {
return backend.PendingNonceAt(ensureContext(opts.Context), opts.From)
} else {
return opts.Nonce.Uint64(), nil
}
}
func createLegacyTx(backend V2Backend, address common.Hash, opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
if opts.GasFeeCap != nil || opts.GasTipCap != nil || opts.AccessList != nil {
return nil, errors.New("maxFeePerGas or maxPriorityFeePerGas or accessList specified but london is not active yet")
}
// Normalize value
value := opts.Value
if value == nil {
value = new(big.Int)
}
// Estimate GasPrice
gasPrice := opts.GasPrice
if gasPrice == nil {
price, err := backend.SuggestGasPrice(ensureContext(opts.Context))
if err != nil {
return nil, err
}
gasPrice = price
}
// Estimate GasLimit
gasLimit := opts.GasLimit
if opts.GasLimit == 0 {
var err error
gasLimit, err = estimateGasLimit(backend, address, opts, contract, input, gasPrice, nil, nil, value)
if err != nil {
return nil, err
}
}
// create the transaction
nonce, err := getNonce(backend, opts)
if err != nil {
return nil, err
}
baseTx := &types.LegacyTx{
To: contract,
Nonce: nonce,
GasPrice: gasPrice,
Gas: gasLimit,
Value: value,
Data: input,
}
return types.NewTx(baseTx), nil
}
const basefeeWiggleMultiplier = 2
func createDynamicTx(backend V2Backend, opts *TransactOpts, contract *common.Address, input []byte, head *types.Header) (*types.Transaction, error) {
// Normalize value
value := opts.Value
if value == nil {
value = new(big.Int)
}
// Estimate TipCap
gasTipCap := opts.GasTipCap
if gasTipCap == nil {
tip, err := backend.SuggestGasTipCap(ensureContext(opts.Context))
if err != nil {
return nil, err
}
gasTipCap = tip
}
// Estimate FeeCap
gasFeeCap := opts.GasFeeCap
if gasFeeCap == nil {
gasFeeCap = new(big.Int).Add(
gasTipCap,
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)),
)
}
if gasFeeCap.Cmp(gasTipCap) < 0 {
return nil, fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap)
}
// Estimate GasLimit
gasLimit := opts.GasLimit
if opts.GasLimit == 0 {
var err error
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value)
if err != nil {
return nil, err
}
}
// create the transaction
nonce, err := c.getNonce(opts)
if err != nil {
return nil, err
}
baseTx := &types.DynamicFeeTx{
To: contract,
Nonce: nonce,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Gas: gasLimit,
Value: value,
Data: input,
AccessList: opts.AccessList,
}
return types.NewTx(baseTx), nil
}
func Transfer(instance bind.ContractInstance, opts *bind.TransactOpts) (*types.Transaction, error) { func Transfer(instance bind.ContractInstance, opts *bind.TransactOpts) (*types.Transaction, error) {
backend := instance.Backend() backend := instance.Backend()
c := bind.NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) c := bind.NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend)
return c.Transfer(opts) return c.Transfer(opts)
} }
func CallRaw(instance bind.ContractInstance, opts *bind.CallOpts, input []byte) ([]byte, error) {
backend := instance.Backend()
c := bind.NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend)
return c.CallRaw(opts, input)
}

@ -75,28 +75,52 @@ func TestV2(t *testing.T) {
Backend: backend, Backend: backend,
Client: backend.Client(), Client: backend.Client(),
} }
address, _, boundContract, err := bind.DeployContract(&opts, contractABI, common.Hex2Bytes(v2_generated_testcase.V2GeneratedTestcaseMetaData.Bin), &bindBackend) address, tx, _, err := bind.DeployContract(&opts, contractABI, common.Hex2Bytes(v2_generated_testcase.V2GeneratedTestcaseMetaData.Bin), &bindBackend)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = bind.WaitDeployed(context.Background(), &bindBackend, tx)
if err != nil {
t.Fatalf("error deploying bound contract: %+v", err)
}
contract, err := v2_generated_testcase.NewV2GeneratedTestcase() contract, err := v2_generated_testcase.NewV2GeneratedTestcase()
if err != nil { if err != nil {
t.Fatal(err) // can't happen here with the example used. consider removing this block t.Fatal(err) // can't happen here with the example used. consider removing this block
} }
contractInstance := v2_generated_testcase.NewV2GeneratedTestcaseInstance(contract, address) //contractInstance := v2_generated_testcase.NewV2GeneratedTestcaseInstance(contract, address, bindBackend)
sinkCh := make(chan *v2_generated_testcase.V2GeneratedTestcase) contractInstance := ContractInstance{
Address: address,
Backend: bindBackend,
}
sinkCh := make(chan *v2_generated_testcase.V2GeneratedTestcaseStruct)
// q: what extra functionality is given by specifying this as a custom method, instead of catching emited methods // q: what extra functionality is given by specifying this as a custom method, instead of catching emited methods
// from the sync channel? // from the sync channel?
unpackStruct := func(log *types.Log) (v2_generated_testcase.V2GeneratedTestcaseStruct, error) { unpackStruct := func(log *types.Log) (*v2_generated_testcase.V2GeneratedTestcaseStruct, error) {
res, err := contract.UnpackStructEvent(log) res, err := contract.UnpackStructEvent(log)
return *res, err return res, err
}
watchOpts := bind.WatchOpts{
Start: nil,
Context: context.Background(),
} }
// TODO: test using various topics // TODO: test using various topics
// q: does nil topics mean to accept any? // q: does nil topics mean to accept any?
sub, err := WatchLogs[v2_generated_testcase.V2GeneratedTestcaseStruct](contractInstance, v2_generated_testcase.V2GeneratedTestcaseStructEventID(), unpackStruct, sinkCh) sub, err := WatchLogs[v2_generated_testcase.V2GeneratedTestcaseStruct](&contractInstance, &watchOpts, v2_generated_testcase.V2GeneratedTestcaseStructEventID(), unpackStruct, sinkCh, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer sub.Unsubscribe() defer sub.Unsubscribe()
// send a balance to our contract (contract must accept ether by default) // send a balance to our contract (contract must accept ether by default)
} }
func TestDeployment(t *testing.T) {
DeployContracts
}
/* test-cases that should be extracted from v1 tests
* EventChecker
*/

Loading…
Cancel
Save