Skip to content

Commit

Permalink
Merge pull request #15 from kaleido-io/constructor
Browse files Browse the repository at this point in the history
Add function to get ABI constructor
  • Loading branch information
peterbroadhurst committed Aug 2, 2022
2 parents 0aa53d8 + 055db58 commit 94b6cf0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ func (a ABI) Functions() map[string]*Entry {
return m
}

func (a ABI) Constructor() *Entry {
for _, e := range a {
if e.Type == Constructor {
return e
}
}
return nil
}

func (a ABI) Events() map[string]*Entry {
m := make(map[string]*Entry)
for _, e := range a {
Expand Down
63 changes: 63 additions & 0 deletions pkg/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,58 @@ const sampleABI2 = `[
}
]`

const sampleABI3 = `[
{
"type": "constructor",
"inputs": [
{
"name": "a",
"type": "tuple",
"components": [
{
"name": "b",
"type": "uint"
},
{
"name": "c",
"type": "string[2]"
},
{
"name": "d",
"type": "bytes"
}
]
}
],
"outputs": []
},
{
"name": "foo",
"type": "function",
"inputs": [
{
"name": "a",
"type": "tuple",
"components": [
{
"name": "b",
"type": "uint"
},
{
"name": "c",
"type": "string[2]"
},
{
"name": "d",
"type": "bytes"
}
]
}
],
"outputs": []
}
]`

func testABI(t *testing.T, abiJSON string) (abi ABI) {
err := json.Unmarshal([]byte(abiJSON), &abi)
assert.NoError(t, err)
Expand Down Expand Up @@ -682,3 +734,14 @@ func TestDecodeEventBadData(t *testing.T) {
_, err := e.DecodeEventData([]ethtypes.HexBytes0xPrefix{}, ethtypes.MustNewHexBytes0xPrefix("0x"))
assert.Regexp(t, "FF22047", err)
}

func TestGetConstructor(t *testing.T) {
a := testABI(t, sampleABI1)
c := a.Constructor()
assert.Nil(t, c)

a = testABI(t, sampleABI3)
c = a.Constructor()
assert.Equal(t, Constructor, c.Type)
assert.Equal(t, 1, len(c.Inputs))
}

0 comments on commit 94b6cf0

Please sign in to comment.