Skip to content

Commit

Permalink
Fix: events from v0.13.3
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 12, 2024
1 parent 592465a commit 27d5587
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
31 changes: 31 additions & 0 deletions pkg/abi/data.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package abi

import "github.com/goccy/go-json"

// abi types
const (
FunctionType = "function"
Expand Down Expand Up @@ -60,6 +62,7 @@ type Abi struct {
type Type struct {
Type string `json:"type"`
Name string `json:"name"`
Kind string `json:"kind,omitempty"`
}

// FunctionItem -
Expand All @@ -79,6 +82,34 @@ type EventItem struct {
Inputs []Type `json:"inputs"`
}

type members struct {
Members []Type `json:"members"`
}

func (item *EventItem) UnmarshalJSON(data []byte) error {
type buf EventItem
if err := json.Unmarshal(data, (*buf)(item)); err != nil {
return err
}
if item.Data == nil && item.Keys == nil {
var m members
if err := json.Unmarshal(data, &m); err != nil {
return err
}
item.Data = make([]Type, 0)
item.Keys = make([]Type, 0)
for i := range m.Members {
switch m.Members[i].Kind {
case "data":
item.Data = append(item.Data, m.Members[i])
case "keys":
item.Keys = append(item.Keys, m.Members[i])
}
}
}
return nil
}

// StructItem -
type StructItem struct {
Type
Expand Down
113 changes: 113 additions & 0 deletions pkg/abi/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package abi

import (
"testing"

"github.com/goccy/go-json"
"github.com/stretchr/testify/require"
)

func TestEventItem_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
want EventItem
data []byte
}{
{
name: "new Transfer",
data: []byte(`{
"kind": "struct",
"name": "openzeppelin::token::erc20_v070::erc20::ERC20::Transfer",
"type": "event",
"members": [
{
"kind": "data",
"name": "from",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"kind": "data",
"name": "to",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"kind": "data",
"name": "value",
"type": "core::integer::u256"
}
]
}`),
want: EventItem{
Type: Type{
Name: "openzeppelin::token::erc20_v070::erc20::ERC20::Transfer",
Type: "event",
Kind: "struct",
},
Data: []Type{
{
Kind: "data",
Name: "from",
Type: "core::starknet::contract_address::ContractAddress",
}, {
Kind: "data",
Name: "to",
Type: "core::starknet::contract_address::ContractAddress",
}, {
Kind: "data",
Name: "value",
Type: "core::integer::u256",
},
},
Keys: []Type{},
},
}, {
name: "old Transfer",
data: []byte(`{
"name": "transfer",
"type": "event",
"data": [
{
"name": "from",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "to",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "value",
"type": "core::integer::u256"
}
],
"keys":[]
}`),
want: EventItem{
Type: Type{
Name: "transfer",
Type: "event",
},
Data: []Type{
{
Name: "from",
Type: "core::starknet::contract_address::ContractAddress",
}, {
Name: "to",
Type: "core::starknet::contract_address::ContractAddress",
}, {
Name: "value",
Type: "core::integer::u256",
},
},
Keys: []Type{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var item EventItem
err := json.Unmarshal(tt.data, &item)
require.NoError(t, err)
require.Equal(t, tt.want, item)
})
}
}
6 changes: 4 additions & 2 deletions pkg/abi/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ func DecodeEventData(data []string, typ EventItem, structs map[string]*StructIte
result = make(map[string]any, 0)
tail = data
err error

eventData = append(typ.Keys, typ.Data...)
)

for _, input := range typ.Data {
tail, err = decodeItem(tail, input, structs, enums, result)
for i := range eventData {
tail, err = decodeItem(tail, eventData[i], structs, enums, result)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 27d5587

Please sign in to comment.