remove capitalize helper method. simplify call/event/error symbol name registration

This commit is contained in:
Jared Wasinger 2024-12-23 16:02:05 +07:00 committed by Felix Lange
parent ddc897e52c
commit f4acb0ce04
2 changed files with 19 additions and 41 deletions

View file

@ -125,7 +125,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
for _, original := range evmABI.Methods { for _, original := range evmABI.Methods {
// Normalize the method for capital cases and non-anonymous inputs/outputs // Normalize the method for capital cases and non-anonymous inputs/outputs
normalized := original normalized := original
normalizedName := methodNormalizer(alias(aliases, original.Name)) normalizedName := abi.ToCamelCase(alias(aliases, original.Name))
// Ensure there is no duplicated identifier // Ensure there is no duplicated identifier
var identifiers = callIdentifiers var identifiers = callIdentifiers
if !original.IsConstant() { if !original.IsConstant() {
@ -159,7 +159,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
copy(normalized.Outputs, original.Outputs) copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs { for j, output := range normalized.Outputs {
if output.Name != "" { if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name) normalized.Outputs[j].Name = abi.ToCamelCase(output.Name)
} }
if hasStruct(output.Type) { if hasStruct(output.Type) {
bindStructType(output.Type, structs) bindStructType(output.Type, structs)
@ -181,7 +181,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
normalized := original normalized := original
// Ensure there is no duplicated identifier // 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. // Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) { if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
normalizedName = fmt.Sprintf("E%s", normalizedName) normalizedName = fmt.Sprintf("E%s", normalizedName)
@ -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, // Event is a bit special, we need to define event struct in binding,
// ensure there is no camel-case-style name conflict. // ensure there is no camel-case-style name conflict.
for index := 0; ; index++ { for index := 0; ; index++ {
if !used[capitalise(normalized.Inputs[j].Name)] { if !used[abi.ToCamelCase(normalized.Inputs[j].Name)] {
used[capitalise(normalized.Inputs[j].Name)] = true used[abi.ToCamelCase(normalized.Inputs[j].Name)] = true
break break
} }
normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index) normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index)
@ -228,7 +228,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
} }
contracts[types[i]] = &tmplContract{ contracts[types[i]] = &tmplContract{
Type: capitalise(types[i]), Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""), InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"), InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor, Constructor: evmABI.Constructor,
@ -278,7 +278,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
funcs := map[string]interface{}{ funcs := map[string]interface{}{
"bindtype": bindType, "bindtype": bindType,
"bindtopictype": bindTopicType, "bindtopictype": bindTopicType,
"capitalise": capitalise, "capitalise": abi.ToCamelCase,
"decapitalise": decapitalise, "decapitalise": decapitalise,
} }
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource)) tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource))
@ -371,7 +371,7 @@ func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
fields []*tmplField fields []*tmplField
) )
for i, elem := range kind.TupleElems { 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] }) name = abi.ResolveNameConflict(name, func(s string) bool { return names[s] })
names[name] = true names[name] = true
fields = append(fields, &tmplField{Type: bindStructType(*elem, structs), Name: name, SolKind: *elem}) fields = append(fields, &tmplField{Type: bindStructType(*elem, structs), Name: name, SolKind: *elem})
@ -380,7 +380,7 @@ func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
if name == "" { if name == "" {
name = fmt.Sprintf("Struct%d", len(structs)) name = fmt.Sprintf("Struct%d", len(structs))
} }
name = capitalise(name) name = abi.ToCamelCase(name)
structs[id] = &tmplStruct{ structs[id] = &tmplStruct{
Name: name, Name: name,
@ -405,13 +405,6 @@ func alias(aliases map[string]string, n string) string {
return n 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. // decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string { func decapitalise(input string) string {
if len(input) == 0 { if len(input) == 0 {
@ -436,7 +429,7 @@ func structured(args abi.Arguments) bool {
} }
// If the field name is empty when normalized or collides (var, Var, _var, _Var), // If the field name is empty when normalized or collides (var, Var, _var, _Var),
// we can't organize into a struct // we can't organize into a struct
field := capitalise(out.Name) field := abi.ToCamelCase(out.Name)
if field == "" || exists[field] { if field == "" || exists[field] {
return false return false
} }

View file

@ -26,7 +26,7 @@ type binder struct {
// registerIdentifier applies alias renaming, name normalization (conversion to camel case), and registers the normalized // 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. // 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) { 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. // Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalized) > 0 && unicode.IsDigit(rune(normalized[0])) { if len(normalized) > 0 && unicode.IsDigit(rune(normalized[0])) {
normalized = fmt.Sprintf("E%s", normalized) normalized = fmt.Sprintf("E%s", normalized)
@ -43,21 +43,6 @@ func (b *contractBinder) registerIdentifier(identifiers map[string]bool, origina
return normalized, nil 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 // BindStructType register the type to be emitted as a struct in the
// bindings. // bindings.
func (b *binder) BindStructType(typ abi.Type) { func (b *binder) BindStructType(typ abi.Type) {
@ -83,7 +68,7 @@ type contractBinder struct {
// into a struct. // into a struct.
func (cb *contractBinder) bindMethod(original abi.Method) error { func (cb *contractBinder) bindMethod(original abi.Method) error {
normalized := original normalized := original
normalizedName, err := cb.RegisterCallIdentifier(original.Name) normalizedName, err := cb.registerIdentifier(cb.callIdentifiers, original.Name)
if err != nil { if err != nil {
return err return err
} }
@ -103,7 +88,7 @@ func (cb *contractBinder) bindMethod(original abi.Method) error {
copy(normalized.Outputs, original.Outputs) copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs { for j, output := range normalized.Outputs {
if output.Name != "" { if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name) normalized.Outputs[j].Name = abi.ToCamelCase(output.Name)
} }
if hasStruct(output.Type) { if hasStruct(output.Type) {
cb.binder.BindStructType(output.Type) cb.binder.BindStructType(output.Type)
@ -124,7 +109,7 @@ func (cb *contractBinder) bindMethod(original abi.Method) error {
if o.Name != "" { if o.Name != "" {
continue 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 normalized.Outputs[i] = o
keys[strings.ToLower(o.Name)] = struct{}{} keys[strings.ToLower(o.Name)] = struct{}{}
} }
@ -143,7 +128,7 @@ func normalizeArgs(args abi.Arguments) abi.Arguments {
if input.Name == "" || isKeyWord(input.Name) { if input.Name == "" || isKeyWord(input.Name) {
args[i].Name = fmt.Sprintf("arg%d", i) 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++ { for index := 0; ; index++ {
if !used[args[i].Name] { if !used[args[i].Name] {
used[args[i].Name] = true used[args[i].Name] = true
@ -173,7 +158,7 @@ func (cb *contractBinder) bindEvent(original abi.Event) error {
if original.Anonymous { if original.Anonymous {
return nil return nil
} }
normalizedName, err := cb.RegisterEventIdentifier(original.Name) normalizedName, err := cb.registerIdentifier(cb.eventIdentifiers, original.Name)
if err != nil { if err != nil {
return err return err
} }
@ -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. // bindError normalizes an error and registers it to be emitted in the bindings.
func (cb *contractBinder) bindError(original abi.Error) error { 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 { if err != nil {
return err return err
} }
@ -268,7 +253,7 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
}, abis[i]) }, abis[i])
// replace this with a method call to cb (name it BoundContract()?) // replace this with a method call to cb (name it BoundContract()?)
b.contracts[types[i]] = &tmplContractV2{ b.contracts[types[i]] = &tmplContractV2{
Type: capitalise(types[i]), Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""), InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"), InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor, Constructor: evmABI.Constructor,
@ -300,7 +285,7 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
funcs := map[string]interface{}{ funcs := map[string]interface{}{
"bindtype": bindType, "bindtype": bindType,
"bindtopictype": bindTopicType, "bindtopictype": bindTopicType,
"capitalise": capitalise, "capitalise": abi.ToCamelCase,
"decapitalise": decapitalise, "decapitalise": decapitalise,
"add": func(val1, val2 int) int { "add": func(val1, val2 int) int {
return val1 + val2 return val1 + val2