Skip to content

Commit

Permalink
remove capitalize helper method. simplify call/event/error symbol nam…
Browse files Browse the repository at this point in the history
…e registration
  • Loading branch information
jwasinger committed Dec 23, 2024
1 parent 230b3f4 commit c802765
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
27 changes: 10 additions & 17 deletions accounts/abi/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
for _, original := range evmABI.Methods {
// Normalize the method for capital cases and non-anonymous inputs/outputs
normalized := original
normalizedName := methodNormalizer(alias(aliases, original.Name))
normalizedName := abi.ToCamelCase(alias(aliases, original.Name))
// Ensure there is no duplicated identifier
var identifiers = callIdentifiers
if !original.IsConstant() {
Expand Down Expand Up @@ -159,7 +159,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs {
if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name)
normalized.Outputs[j].Name = abi.ToCamelCase(output.Name)
}
if hasStruct(output.Type) {
bindStructType(output.Type, structs)
Expand All @@ -181,7 +181,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
normalized := original

// Ensure there is no duplicated identifier
normalizedName := methodNormalizer(alias(aliases, original.Name))
normalizedName := abi.ToCamelCase(alias(aliases, original.Name))
// Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
normalizedName = fmt.Sprintf("E%s", normalizedName)
Expand All @@ -206,8 +206,8 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
// Event is a bit special, we need to define event struct in binding,
// ensure there is no camel-case-style name conflict.
for index := 0; ; index++ {
if !used[capitalise(normalized.Inputs[j].Name)] {
used[capitalise(normalized.Inputs[j].Name)] = true
if !used[abi.ToCamelCase(normalized.Inputs[j].Name)] {
used[abi.ToCamelCase(normalized.Inputs[j].Name)] = true
break
}
normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index)
Expand All @@ -228,7 +228,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
}

contracts[types[i]] = &tmplContract{
Type: capitalise(types[i]),
Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor,
Expand Down Expand Up @@ -278,7 +278,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
funcs := map[string]interface{}{
"bindtype": bindType,
"bindtopictype": bindTopicType,
"capitalise": capitalise,
"capitalise": abi.ToCamelCase,
"decapitalise": decapitalise,
}
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource))
Expand Down Expand Up @@ -371,7 +371,7 @@ func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
fields []*tmplField
)
for i, elem := range kind.TupleElems {
name := capitalise(kind.TupleRawNames[i])
name := abi.ToCamelCase(kind.TupleRawNames[i])
name = abi.ResolveNameConflict(name, func(s string) bool { return names[s] })
names[name] = true
fields = append(fields, &tmplField{Type: bindStructType(*elem, structs), Name: name, SolKind: *elem})
Expand All @@ -380,7 +380,7 @@ func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
if name == "" {
name = fmt.Sprintf("Struct%d", len(structs))
}
name = capitalise(name)
name = abi.ToCamelCase(name)

structs[id] = &tmplStruct{
Name: name,
Expand All @@ -405,13 +405,6 @@ func alias(aliases map[string]string, n string) string {
return n
}

// methodNormalizer is a name transformer that modifies Solidity method names to
// conform to Go naming conventions.
var methodNormalizer = abi.ToCamelCase

// capitalise makes a camel-case string which starts with an upper case character.
var capitalise = abi.ToCamelCase

// decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string {
if len(input) == 0 {
Expand All @@ -436,7 +429,7 @@ func structured(args abi.Arguments) bool {
}
// If the field name is empty when normalized or collides (var, Var, _var, _Var),
// we can't organize into a struct
field := capitalise(out.Name)
field := abi.ToCamelCase(out.Name)
if field == "" || exists[field] {
return false
}
Expand Down
33 changes: 9 additions & 24 deletions accounts/abi/bind/bindv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type binder struct {
// registerIdentifier applies alias renaming, name normalization (conversion to camel case), and registers the normalized
// name in the specified identifier map. It returns an error if the normalized name already exists in the map.
func (b *contractBinder) registerIdentifier(identifiers map[string]bool, original string) (normalized string, err error) {
normalized = methodNormalizer(alias(b.binder.aliases, original))
normalized = abi.ToCamelCase(alias(b.binder.aliases, original))
// Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalized) > 0 && unicode.IsDigit(rune(normalized[0])) {
normalized = fmt.Sprintf("E%s", normalized)
Expand All @@ -43,21 +43,6 @@ func (b *contractBinder) registerIdentifier(identifiers map[string]bool, origina
return normalized, nil
}

// RegisterCallIdentifier applies registerIdentifier for contract methods.
func (b *contractBinder) RegisterCallIdentifier(id string) (string, error) {
return b.registerIdentifier(b.callIdentifiers, id)
}

// RegisterEventIdentifier applies registerIdentifier for contract events.
func (b *contractBinder) RegisterEventIdentifier(id string) (string, error) {
return b.registerIdentifier(b.eventIdentifiers, id)
}

// RegisterErrorIdentifier applies registerIdentifier for contract errors.
func (b *contractBinder) RegisterErrorIdentifier(id string) (string, error) {
return b.registerIdentifier(b.errorIdentifiers, id)
}

// BindStructType register the type to be emitted as a struct in the
// bindings.
func (b *binder) BindStructType(typ abi.Type) {
Expand All @@ -83,7 +68,7 @@ type contractBinder struct {
// into a struct.
func (cb *contractBinder) bindMethod(original abi.Method) error {
normalized := original
normalizedName, err := cb.RegisterCallIdentifier(original.Name)
normalizedName, err := cb.registerIdentifier(cb.callIdentifiers, original.Name)
if err != nil {
return err
}
Expand All @@ -103,7 +88,7 @@ func (cb *contractBinder) bindMethod(original abi.Method) error {
copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs {
if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name)
normalized.Outputs[j].Name = abi.ToCamelCase(output.Name)
}
if hasStruct(output.Type) {
cb.binder.BindStructType(output.Type)
Expand All @@ -124,7 +109,7 @@ func (cb *contractBinder) bindMethod(original abi.Method) error {
if o.Name != "" {
continue
}
o.Name = capitalise(abi.ResolveNameConflict("arg", func(name string) bool { _, ok := keys[name]; return ok }))
o.Name = abi.ToCamelCase(abi.ResolveNameConflict("arg", func(name string) bool { _, ok := keys[name]; return ok }))
normalized.Outputs[i] = o
keys[strings.ToLower(o.Name)] = struct{}{}
}
Expand All @@ -143,7 +128,7 @@ func normalizeArgs(args abi.Arguments) abi.Arguments {
if input.Name == "" || isKeyWord(input.Name) {
args[i].Name = fmt.Sprintf("arg%d", i)
}
args[i].Name = capitalise(args[i].Name)
args[i].Name = abi.ToCamelCase(args[i].Name)
for index := 0; ; index++ {
if !used[args[i].Name] {
used[args[i].Name] = true
Expand Down Expand Up @@ -173,7 +158,7 @@ func (cb *contractBinder) bindEvent(original abi.Event) error {
if original.Anonymous {
return nil
}
normalizedName, err := cb.RegisterEventIdentifier(original.Name)
normalizedName, err := cb.registerIdentifier(cb.eventIdentifiers, original.Name)
if err != nil {
return err
}
Expand All @@ -187,7 +172,7 @@ func (cb *contractBinder) bindEvent(original abi.Event) error {

// bindError normalizes an error and registers it to be emitted in the bindings.
func (cb *contractBinder) bindError(original abi.Error) error {
normalizedName, err := cb.RegisterErrorIdentifier(original.Name)
normalizedName, err := cb.registerIdentifier(cb.errorIdentifiers, original.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +253,7 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
}, abis[i])
// replace this with a method call to cb (name it BoundContract()?)
b.contracts[types[i]] = &tmplContractV2{
Type: capitalise(types[i]),
Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor,
Expand Down Expand Up @@ -300,7 +285,7 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
funcs := map[string]interface{}{
"bindtype": bindType,
"bindtopictype": bindTopicType,
"capitalise": capitalise,
"capitalise": abi.ToCamelCase,
"decapitalise": decapitalise,
"add": func(val1, val2 int) int {
return val1 + val2
Expand Down

0 comments on commit c802765

Please sign in to comment.