Skip to content

Commit

Permalink
add support for decoding binary extension arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Duchesneau committed Feb 10, 2021
1 parent e3c3192 commit c029537
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions abidecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ func (a *ABI) read(binaryDecoder *Decoder, fieldType string) (interface{}, error
}

func analyzeFieldType(fieldType string) (typeName string, isOptional bool, isArray bool, isBinaryExtension bool) {
if strings.HasSuffix(fieldType, "[]$") {
return fieldType[0 : len(fieldType)-3], false, true, true
}

if strings.HasSuffix(fieldType, "?") {
return fieldType[0 : len(fieldType)-1], true, false, false
}
Expand Down
24 changes: 24 additions & 0 deletions abidecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,30 @@ func TestABI_decode_BinaryExtension(t *testing.T) {
assert.JSONEq(t, `{"id":0,"name":"name"}`, string(json))
}

func TestABI_decode_BinaryExtensionArray(t *testing.T) {
abi := &ABI{
Structs: []StructDef{
{
Name: "root",
Fields: []FieldDef{
{Name: "id", Type: "uint8"},
{Name: "name", Type: "name[]$"},
},
},
},
}

json, err := abi.Decode(NewDecoder(HexString("00")), "root")
require.NoError(t, err)

assert.JSONEq(t, `{"id":0}`, string(json))

json, err = abi.Decode(NewDecoder(HexString("00010000000000a0a499")), "root")
require.NoError(t, err)

assert.JSONEq(t, `{"id":0,"name":["name"]}`, string(json))
}

func TestABI_decodeFields(t *testing.T) {
types := []ABIType{
{NewTypeName: "action.type.1", Type: "name"},
Expand Down

0 comments on commit c029537

Please sign in to comment.