accounts/abi/bind: simplify bindv2: create constructors for contractBinder, tmplContractV2. move some sanitization of values loaded from ABI definition into tmplContractV2 constructor

This commit is contained in:
Jared Wasinger 2025-01-06 21:12:42 +07:00 committed by Felix Lange
parent b896ed2966
commit f70344216e
2 changed files with 37 additions and 28 deletions

View file

@ -76,6 +76,18 @@ type contractBinder struct {
errorIdentifiers map[string]bool
}
func newContractBinder(binder *binder) *contractBinder {
return &contractBinder{
binder,
make(map[string]*tmplMethod),
make(map[string]*tmplEvent),
make(map[string]*tmplError),
make(map[string]bool),
make(map[string]bool),
make(map[string]bool),
}
}
// bindMethod registers a method to be emitted in the bindings.
// The name, inputs and outputs are normalized. If any inputs are
// struct-type their structs are registered to be emitted in the bindings.
@ -218,16 +230,7 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
}
}
cb := contractBinder{
binder: &b,
calls: make(map[string]*tmplMethod),
events: make(map[string]*tmplEvent),
errors: make(map[string]*tmplError),
callIdentifiers: make(map[string]bool),
errorIdentifiers: make(map[string]bool),
eventIdentifiers: make(map[string]bool),
}
cb := newContractBinder(&b)
for _, original := range evmABI.Methods {
if err := cb.bindMethod(original); err != nil {
return "", err
@ -244,24 +247,8 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
return "", err
}
}
// Strip any whitespace from the JSON ABI
strippedABI := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, abis[i])
// replace this with a method call to cb (name it BoundContract()?)
b.contracts[types[i]] = &tmplContractV2{
Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor,
Calls: cb.calls,
Events: cb.events,
Errors: cb.errors,
Libraries: make(map[string]string),
}
b.contracts[types[i]] = newTmplContractV2(types[i], abis[i], bytecodes[i], evmABI.Constructor, cb)
}
invertedLibs := make(map[string]string)

View file

@ -18,6 +18,8 @@ package bind
import (
_ "embed"
"strings"
"unicode"
"github.com/ethereum/go-ethereum/accounts/abi"
)
@ -57,6 +59,26 @@ type tmplContractV2 struct {
Errors map[string]*tmplError // all errors defined
}
func newTmplContractV2(typ string, abiStr string, bytecode string, constructor abi.Method, cb *contractBinder) *tmplContractV2 {
// Strip any whitespace from the JSON ABI
strippedABI := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, abiStr)
return &tmplContractV2{
abi.ToCamelCase(typ),
strings.ReplaceAll(strippedABI, "\"", "\\\""),
strings.TrimPrefix(strings.TrimSpace(bytecode), "0x"),
constructor,
cb.calls,
cb.events,
make(map[string]string),
cb.errors,
}
}
type tmplDataV2 struct {
Package string // Name of the package to place the generated file in
Contracts map[string]*tmplContractV2 // List of contracts to generate into this file