Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Solidity style full descriptions of functions/events/structs #71

Merged
merged 9 commits into from
Aug 12, 2024
69 changes: 68 additions & 1 deletion pkg/abi/abi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -670,6 +670,63 @@ func (e *Entry) SignatureCtx(ctx context.Context) (string, error) {
return buff.String(), nil
}

func (e *Entry) SolidityDef() (string, []string, error) {
return e.SolidityDefCtx(context.Background())
}

// DescriptorStringCtx returns a Solidity-like descriptor of the entry, including its type
func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
// Everything apart from event and error is a type of function
peterbroadhurst marked this conversation as resolved.
Show resolved Hide resolved
isFunction := e.Type != Error && e.Type != Event

allChildStructs := []string{}
buff := new(strings.Builder)
buff.WriteString(string(e.Type))
buff.WriteRune(' ')
buff.WriteString(e.Name)
buff.WriteRune('(')
for i, p := range e.Inputs {
if i > 0 {
buff.WriteString(", ")
}
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
if err != nil {
return "", nil, err
}
allChildStructs = append(allChildStructs, childStructs...)
buff.WriteString(s)
}
buff.WriteRune(')')

if isFunction {
buff.WriteString(" external")
if e.StateMutability != "" &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why include "external" in the descriptor if it's the same for all functions? Is it just to make it read more like a proper Solidity function definition?

Copy link
Contributor Author

@peterbroadhurst peterbroadhurst Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly that.
It allows you to paste it into a code editor in an interface or actual .sol file and it work as-is

// The state mutability nonpayable is reflected in Solidity by not specifying a state mutability modifier at all.
e.StateMutability != NonPayable {
buff.WriteRune(' ')
buff.WriteString(string(e.StateMutability))
}
if len(e.Outputs) > 0 {
buff.WriteString(" returns (")
for i, p := range e.Outputs {
if i > 0 {
buff.WriteString(", ")
}
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
if err != nil {
return "", nil, err
}
allChildStructs = append(allChildStructs, childStructs...)
buff.WriteString(s)
}
buff.WriteRune(')')
}
buff.WriteString(" { }")
}

return buff.String(), allChildStructs, nil
}

// Validate processes all the components of the type of this ABI parameter.
// - The elementary type
// - The fixed/variable length array dimensions
Expand Down Expand Up @@ -701,6 +758,16 @@ func (p *Parameter) SignatureStringCtx(ctx context.Context) (string, error) {
return tc.String(), nil
}

func (p *Parameter) SolidityDefCtx(ctx context.Context, inFunction bool) (string, []string, error) {
// Ensure the type component tree has been parsed
tc, err := p.TypeComponentTreeCtx(ctx)
if err != nil {
return "", nil, err
}
solDef, childStructs := tc.SolidityParamDef(inFunction)
return solDef, childStructs, nil
}

// String returns the signature string. If a Validate needs to be initiated, and that
// parse fails, then the error is logged, but is not returned
func (p *Parameter) String() string {
Expand Down
Loading
Loading