Skip to content

Commit

Permalink
solface now generates more meaningful names for some compound types
Browse files Browse the repository at this point in the history
For compound types which are structs, the names are now much more human
readable assuming that the input ABI defines their `internalType`
fields.

Also updated the GitHub action to use the new `-version` flag to print
the version.
  • Loading branch information
zomglings committed Nov 22, 2023
1 parent 3d1d347 commit 862f300
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
go test ./... -v
- name: Print solface version
run: |
go run ./... -h 2>&1 | tail -n1
go run ./... -version
7 changes: 4 additions & 3 deletions abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type TypeDeclaration struct {

// Represents a value in an ABI.
type Value struct {
Name string
Type string
Components []Value
Name string
Type string
InternalType string `json:"internalType,omitempty"`
Components []Value
}

// Represents a parameter for an event in an ABI.
Expand Down
22 changes: 19 additions & 3 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,25 @@ func GenerateName(nameCounter *int) string {
return result
}

// Parses the name of an internal type and either returns that name (for structs) or "Compound" (for
// any other type).
// For nested structs (e.g. structs defined in other contracts or interfaces), this only returns the
// final component of the name.
func ParseInternalType(internalType string) string {
if !strings.HasPrefix(internalType, "struct") {
return "Compound"
}

structQualifiedName := strings.TrimPrefix(internalType, "struct ")
structNameComponents := strings.Split(structQualifiedName, ".")
structName := structNameComponents[len(structNameComponents)-1]
return structName
}

// Generates a fresh name for an anonymous compound type.
func GenerateType(typeCounter *int) string {
result := fmt.Sprintf("Compound%d", *typeCounter)
func GenerateType(typeCounter *int, internalType string) string {
typeName := ParseInternalType(internalType)
result := fmt.Sprintf("%s%d", typeName, *typeCounter)
(*typeCounter) += 1
return result
}
Expand Down Expand Up @@ -168,7 +184,7 @@ func CompoundSingleValue(val Value, typeCounter, nameCounter *int) (Value, []Com
}

var compound CompoundType
compound.TypeName = GenerateType(typeCounter)
compound.TypeName = GenerateType(typeCounter, val.InternalType)
compound.Members = make([]NamedValue, len(updatedComponents))
for i, component := range updatedComponents {
memberName := component.Name
Expand Down

0 comments on commit 862f300

Please sign in to comment.