move abigen2 specific bind code into BindV2. fix broken v1 tests

This commit is contained in:
Jared Wasinger 2024-12-16 13:28:19 +07:00 committed by Felix Lange
parent 780dd76746
commit 89fcfac1cf
2 changed files with 38 additions and 37 deletions

View File

@ -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)

View File

@ -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,39 +433,11 @@ 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,
}
return data, nil