Skip to content

Commit

Permalink
accounts/abi/bind: simplify bindv2: create constructors for contractB…
Browse files Browse the repository at this point in the history
…inder, tmplContractV2. move some sanitization of values loaded from ABI definition into tmplContractV2 constructor
  • Loading branch information
jwasinger committed Jan 6, 2025
1 parent 2605ed3 commit 5c5e66b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
43 changes: 15 additions & 28 deletions accounts/abi/bind/bindv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions accounts/abi/bind/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package bind

import (
_ "embed"
"strings"
"unicode"

"github.com/ethereum/go-ethereum/accounts/abi"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c5e66b

Please sign in to comment.