Skip to content

Commit

Permalink
Merge pull request #8 from kaleido-io/component-type
Browse files Browse the repository at this point in the history
Expose ABI Parameter on TypeComponent
  • Loading branch information
nguyer authored Jun 13, 2022
2 parents a3ff54c + 8028a13 commit 766861b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/abi/typecomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type TypeComponent interface {
ElementaryType() ElementaryTypeInfo // only non-nil for elementary components
ArrayChild() TypeComponent // only non-nil for array components
TupleChildren() []TypeComponent // only non-nil for tuple components
KeyName() string // the name of the ABI property/component
KeyName() string // the name of the ABI property/component, only set for top-level parameters and tuple entries
Parameter() *Parameter // the ABI property/component, only set for top-level parameters and tuple entries
ParseExternal(v interface{}) (*ComponentValue, error)
ParseExternalCtx(ctx context.Context, v interface{}) (*ComponentValue, error)
DecodeABIData(d []byte, offset int) (*ComponentValue, error)
Expand All @@ -65,6 +66,7 @@ type typeComponent struct {
arrayChild *typeComponent // For array parameter
keyName string // For top level ABI entries, and tuple children
tupleChildren []*typeComponent // For tuple parameters
parameter *Parameter // The original ABI parameter for this typeComponent
}

// elementaryTypeInfo defines the string parsing rules, as well as a pointer to the functions for
Expand Down Expand Up @@ -322,6 +324,10 @@ func (tc *typeComponent) DecodeABIDataCtx(ctx context.Context, b []byte, offset
return cv, err
}

func (tc *typeComponent) Parameter() *Parameter {
return tc.parameter
}

func (tc *typeComponent) parseExternal(ctx context.Context, desc string, input interface{}) (*ComponentValue, error) {
return walkInput(ctx, desc, input, tc)
}
Expand All @@ -348,6 +354,7 @@ func (p *Parameter) parseABIParameterComponents(ctx context.Context) (tc *typeCo
cType: TupleComponent,
tupleChildren: make([]*typeComponent, len(p.Components)),
keyName: p.Name,
parameter: p,
}
// Process all the components of the tuple
for i, c := range p.Components {
Expand All @@ -369,6 +376,7 @@ func (p *Parameter) parseABIParameterComponents(ctx context.Context) (tc *typeCo
elementarySuffix: suffix,
keyName: p.Name,
m: et.defaultM,
parameter: p,
}
// Process any suffix according to the rules of the elementary type
switch et.suffixType {
Expand Down Expand Up @@ -401,7 +409,7 @@ func (p *Parameter) parseABIParameterComponents(ctx context.Context) (tc *typeCo

if arrays != "" {
// The component needs to be wrapped in some number of array dimensions
return parseArrays(ctx, abiTypeString, tc, arrays, p.Name)
return p.parseArrays(ctx, abiTypeString, tc, arrays, p.Name)
}

return tc, nil
Expand Down Expand Up @@ -477,7 +485,7 @@ func parseArrayM(ctx context.Context, abiTypeString string, ac *typeComponent, m
}

// parseArrays recursively builds arrays for the "[8][]" part of "uint256[8][]" for variable or fixed array types
func parseArrays(ctx context.Context, abiTypeString string, child *typeComponent, suffix, keyName string) (*typeComponent, error) {
func (p *Parameter) parseArrays(ctx context.Context, abiTypeString string, child *typeComponent, suffix, keyName string) (*typeComponent, error) {

pos := 0
if pos >= len(suffix) || suffix[pos] != '[' {
Expand All @@ -497,12 +505,14 @@ func parseArrays(ctx context.Context, abiTypeString string, child *typeComponent
cType: DynamicArrayComponent,
arrayChild: child,
keyName: keyName,
parameter: p,
}
} else {
ac = &typeComponent{
cType: FixedArrayComponent,
arrayChild: child,
keyName: keyName,
parameter: p,
}
if err := parseArrayM(ctx, abiTypeString, ac, mStr.String()); err != nil {
return nil, err
Expand All @@ -511,7 +521,7 @@ func parseArrays(ctx context.Context, abiTypeString string, child *typeComponent

// We might have more dimensions to the array - if so recurse
if pos < len(suffix) {
return parseArrays(ctx, abiTypeString, ac, suffix[pos:], keyName)
return p.parseArrays(ctx, abiTypeString, ac, suffix[pos:], keyName)
}

// We're the last array in the chain
Expand Down
31 changes: 31 additions & 0 deletions pkg/abi/typecomponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,34 @@ func TestTypeComponentParseExternalOk(t *testing.T) {
assert.Equal(t, "test", cv.Value)

}

func TestTypeInternalTypeIndexed(t *testing.T) {

abiString := `[
{
"name": "f",
"type": "function",
"inputs": [
{
"name": "a",
"type": "uint256[]",
"internalType": "uint256[]",
"indexed": true
}
],
"outputs": []
}
]`
var abi ABI
err := json.Unmarshal([]byte(abiString), &abi)
assert.NoError(t, err)
err = abi.Validate()
assert.NoError(t, err)

tc, err := abi.Functions()["f"].Inputs[0].TypeComponentTree()
assert.NoError(t, err)
assert.Equal(t, "uint256[]", tc.Parameter().Type)
assert.Equal(t, "uint256[]", tc.Parameter().InternalType)
assert.Equal(t, true, tc.Parameter().Indexed)
}

0 comments on commit 766861b

Please sign in to comment.