From eae78c87459f533b112469934babb8633033afcd Mon Sep 17 00:00:00 2001 From: Nicko Guyer Date: Thu, 9 Jun 2022 16:01:33 -0400 Subject: [PATCH 1/2] Expose ABI Parameter on TypeComponent Signed-off-by: Nicko Guyer --- pkg/abi/typecomponents.go | 16 +++++++++++++--- pkg/abi/typecomponents_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/pkg/abi/typecomponents.go b/pkg/abi/typecomponents.go index ef349865..463fb15d 100644 --- a/pkg/abi/typecomponents.go +++ b/pkg/abi/typecomponents.go @@ -53,6 +53,7 @@ type TypeComponent interface { ParseExternalCtx(ctx context.Context, v interface{}) (*ComponentValue, error) DecodeABIData(d []byte, offset int) (*ComponentValue, error) DecodeABIDataCtx(ctx context.Context, d []byte, offest int) (*ComponentValue, error) + Parameter() *Parameter } type typeComponent struct { @@ -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 @@ -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) } @@ -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 { @@ -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 { @@ -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 @@ -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] != '[' { @@ -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 @@ -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 diff --git a/pkg/abi/typecomponents_test.go b/pkg/abi/typecomponents_test.go index 53567308..cab15a0f 100644 --- a/pkg/abi/typecomponents_test.go +++ b/pkg/abi/typecomponents_test.go @@ -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) +} From 8028a13976872e3f78f57e9580f03b459c67b063 Mon Sep 17 00:00:00 2001 From: Nicko Guyer Date: Mon, 13 Jun 2022 11:45:12 -0400 Subject: [PATCH 2/2] Add comments to TypeComponent interface Signed-off-by: Nicko Guyer --- pkg/abi/typecomponents.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/abi/typecomponents.go b/pkg/abi/typecomponents.go index 463fb15d..94ca5854 100644 --- a/pkg/abi/typecomponents.go +++ b/pkg/abi/typecomponents.go @@ -48,12 +48,12 @@ 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) DecodeABIDataCtx(ctx context.Context, d []byte, offest int) (*ComponentValue, error) - Parameter() *Parameter } type typeComponent struct {