Skip to content

Commit

Permalink
Add simple concattenated string support
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst committed Aug 12, 2024
1 parent 97939c6 commit 6a0f8ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,30 @@ func (e *Entry) String() string {
return s
}

// SolString returns a Solidity - like string, with an empty function definition,
// and any struct definitions separated afterwards with a ; (semicolon)
func (e *Entry) SolString() string {
solStr, err := e.SolidityStringCtx(context.Background())
if err != nil {
log.L(context.Background()).Warnf("ABI parsing failed: %s", err)
}
return solStr
}

func (e *Entry) SolidityStringCtx(ctx context.Context) (string, error) {
solDef, childStructs, err := e.SolidityDefCtx(ctx)
if err != nil {
return "", err
}
buff := new(strings.Builder)
buff.WriteString(solDef)
for _, e := range childStructs {
buff.WriteString("; ")
buff.WriteString(e)
}
return buff.String(), nil
}

func (e *Entry) Signature() (string, error) {
return e.SignatureCtx(context.Background())
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ func TestSignatureHashInvalid(t *testing.T) {
_, _, err = e.SolidityDef()
assert.Regexp(t, "FF22025", err)

assert.Empty(t, e.SolString())

assert.Equal(t, make(ethtypes.HexBytes0xPrefix, 32), e.SignatureHashBytes())

e = &Entry{
Expand Down Expand Up @@ -1008,6 +1010,9 @@ func TestComplexStructSolidityDef(t *testing.T) {
"struct Invoice { Customer customer; Widget[] widgets; }",
}, childStructs)

assert.Equal(t, "function invoice(Invoice memory _invoice) external payable { }; struct Customer { address owner; bytes32 locator; }; struct Widget { string description; uint256 price; string[] attributes; }; struct Invoice { Customer customer; Widget[] widgets; }",
abi.Functions()["invoice"].SolString())

solDef, childStructs, err = abi.Events()["Invoiced"].SolidityDef()
assert.NoError(t, err)
assert.Equal(t, "event Invoiced(Customer customer, Widget[] widgets)", solDef)
Expand Down

0 comments on commit 6a0f8ba

Please sign in to comment.