From 5c5e66bf1ddd20507c177e8d3bae308a6a01b4f1 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 6 Jan 2025 21:12:42 +0700 Subject: [PATCH] accounts/abi/bind: simplify bindv2: create constructors for contractBinder, tmplContractV2. move some sanitization of values loaded from ABI definition into tmplContractV2 constructor --- accounts/abi/bind/bindv2.go | 43 ++++++++++++----------------------- accounts/abi/bind/template.go | 22 ++++++++++++++++++ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/accounts/abi/bind/bindv2.go b/accounts/abi/bind/bindv2.go index 4f42d1b668fa..55e91b18e7e2 100644 --- a/accounts/abi/bind/bindv2.go +++ b/accounts/abi/bind/bindv2.go @@ -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) diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index f9a5e9fc466a..1eb95a6dccc8 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -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