From 89fcfac1cf6158f8dd3f8515937c9d13546201ee Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 16 Dec 2024 13:28:19 +0700 Subject: [PATCH] move abigen2 specific bind code into BindV2. fix broken v1 tests --- accounts/abi/bind/base.go | 10 +++--- accounts/abi/bind/bind.go | 65 ++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index bb67546148..bd12cd0f87 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -470,11 +470,11 @@ func (c *BoundContract) FilterLogsById(opts *FilterOpts, eventID common.Hash, qu // FilterLogs filters contract logs for past blocks, returning the necessary // channels to construct a strongly typed bound iterator on top of them. -func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) { +func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) { return c.filterLogs(opts, c.abi.Events[name].ID, query...) } -func (c *BoundContract) filterLogs(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) { +func (c *BoundContract) filterLogs(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) { // Don't crash on a lazy user if opts == nil { opts = new(FilterOpts) @@ -519,17 +519,17 @@ func (c *BoundContract) filterLogs(opts *FilterOpts, eventID common.Hash, query // WatchLogs filters subscribes to contract logs for future blocks, returning a // subscription object that can be used to tear down the watcher. -func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) { +func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) { return c.watchLogs(opts, c.abi.Events[name].ID, query...) } // WatchLogsForId filters subscribes to contract logs for future blocks, returning a // subscription object that can be used to tear down the watcher. -func (c *BoundContract) WatchLogsForId(opts *WatchOpts, id common.Hash, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) { +func (c *BoundContract) WatchLogsForId(opts *WatchOpts, id common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) { return c.watchLogs(opts, id, query...) } -func (c *BoundContract) watchLogs(opts *WatchOpts, eventID common.Hash, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) { +func (c *BoundContract) watchLogs(opts *WatchOpts, eventID common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) { // Don't crash on a lazy user if opts == nil { opts = new(WatchOpts) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index f5ddda240e..a813296045 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -144,6 +144,35 @@ func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[strin call.Structured = true } } + + // map of contract name -> pattern + invertedLibs := make(map[string]string) + // assume that this is invertible/onto because I assume library names are unique now + // TODO: check that they've been sanitized at this point. + for pattern, name := range libs { + invertedLibs[name] = pattern + } + data.InvertedLibs = invertedLibs + + contractsBins := make(map[string]string) + for typ, contract := range data.Contracts { + pattern := invertedLibs[typ] + contractsBins[pattern] = contract.InputBin + } + builder := newDepTreeBuilder(nil, contractsBins) + roots, deps := builder.BuildDepTrees() + allNodes := append(roots, deps...) + for _, dep := range allNodes { + contractType := libs[dep.pattern] + for subDepPattern, _ := range dep.Flatten() { + if subDepPattern == dep.pattern { + continue + } + subDepType := libs[subDepPattern] + data.Contracts[contractType].AllLibraries[subDepType] = subDepPattern + } + } + buffer := new(bytes.Buffer) funcs := map[string]interface{}{ "bindtype": bindType, @@ -404,40 +433,12 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string] contracts[types[i]].Library = ok } - // map of contract name -> pattern - invertedLibs := make(map[string]string) - // assume that this is invertible/onto because I assume library names are unique now - // TODO: check that they've been sanitized at this point. - for pattern, name := range libs { - invertedLibs[name] = pattern - } - - contractsBins := make(map[string]string) - for typ, contract := range contracts { - pattern := invertedLibs[typ] - contractsBins[pattern] = contract.InputBin - } - builder := newDepTreeBuilder(nil, contractsBins) - roots, deps := builder.BuildDepTrees() - allNodes := append(roots, deps...) - for _, dep := range allNodes { - contractType := libs[dep.pattern] - for subDepPattern, _ := range dep.Flatten() { - if subDepPattern == dep.pattern { - continue - } - subDepType := libs[subDepPattern] - contracts[contractType].AllLibraries[subDepType] = subDepPattern - } - } - // Generate the contract template data content and render it data := &tmplData{ - Package: pkg, - Contracts: contracts, - Libraries: libs, - InvertedLibs: invertedLibs, - Structs: structs, + Package: pkg, + Contracts: contracts, + Libraries: libs, + Structs: structs, } return data, nil }