Skip to content

Commit 18cfac7

Browse files
Fix: decoding event from members (#10)
* Fix: decoding event from members * Fix: typo * Fixes * Fix: workflows * Security issue
1 parent 27d5587 commit 18cfac7

File tree

8 files changed

+151
-42
lines changed

8 files changed

+151
-42
lines changed

.github/workflows/tests.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ jobs:
66
name: lint
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/setup-go@v3
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
- name: Set up Go
12+
uses: actions/setup-go@v5
1013
with:
11-
go-version: '1.19'
12-
- uses: actions/checkout@v3
14+
go-version: '1.23'
15+
cache: false
1316
- name: golangci-lint
14-
uses: golangci/golangci-lint-action@v3
17+
uses: golangci/golangci-lint-action@v6
1518
with:
16-
version: v1.51.2
17-
args: --timeout=3m
19+
version: v1.60.3
20+
args: --timeout=5m
21+
1822
test:
23+
name: Test
1924
runs-on: ubuntu-latest
2025
steps:
21-
- name: install Go
22-
uses: actions/setup-go@v2
23-
with:
24-
go-version: 1.19.x
25-
- name: checkout code
26-
uses: actions/checkout@v2
27-
- uses: actions/cache@v2
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Install Go
30+
uses: actions/setup-go@v5
2831
with:
29-
path: ~/go/pkg/mod
30-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
31-
restore-keys: |
32-
${{ runner.os }}-go-
33-
- name: golang tests
32+
go-version: 1.23.x
33+
- name: Golang tests
3434
env:
3535
GO111MODULE: on
3636
run: |
3737
go mod download
38-
go test ./...
38+
go test -p 8 ./...

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/dipdup-io/starknet-go-api
22

3-
go 1.21
3+
go 1.23.2
44

55
require (
66
github.com/dipdup-net/indexer-sdk v0.0.0-20230614153224-11cee5bf9248
@@ -19,7 +19,7 @@ require (
1919
github.com/mattn/go-colorable v0.1.13 // indirect
2020
github.com/mattn/go-isatty v0.0.16 // indirect
2121
github.com/pmezard/go-difflib v1.0.0 // indirect
22-
golang.org/x/crypto v0.17.0 // indirect
23-
golang.org/x/sys v0.15.0 // indirect
22+
golang.org/x/crypto v0.31.0 // indirect
23+
golang.org/x/sys v0.28.0 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
2525
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ github.com/tfkhsr/jsonschema v0.0.0-20180218143334-273afdd5a88c h1:FiJHojQ8AwCcl
3333
github.com/tfkhsr/jsonschema v0.0.0-20180218143334-273afdd5a88c/go.mod h1:zhGMpmE6P0Eml0MgFIc5TljSWlr/hbNSmig8KiVEodo=
3434
github.com/wealdtech/go-merkletree v1.0.1-0.20230205101955-ec7a95ea11ca h1:oK35INPN4CiubFw7ZPQkbxm2AsSU6Tjeb9YhciZnvIM=
3535
github.com/wealdtech/go-merkletree v1.0.1-0.20230205101955-ec7a95ea11ca/go.mod h1:bM9mDSjsti+gkjl8FjovMoUH3MPR5bwJ3+ucaYFY0Jk=
36-
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
37-
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
36+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
37+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
3838
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3939
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4040
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41-
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
42-
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
41+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
42+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4343
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
4444
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
4545
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

pkg/abi/data.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,29 @@ type FunctionItem struct {
7777
type EventItem struct {
7878
Type
7979

80-
Data []Type `json:"data"`
81-
Keys []Type `json:"keys"`
82-
Inputs []Type `json:"inputs"`
80+
Members []Type `json:"members,omitempty"`
81+
Data []Type `json:"data"`
82+
Keys []Type `json:"keys"`
83+
Inputs []Type `json:"inputs"`
8384
}
8485

85-
type members struct {
86-
Members []Type `json:"members"`
86+
type Members struct {
8787
}
8888

8989
func (item *EventItem) UnmarshalJSON(data []byte) error {
9090
type buf EventItem
9191
if err := json.Unmarshal(data, (*buf)(item)); err != nil {
9292
return err
9393
}
94-
if item.Data == nil && item.Keys == nil {
95-
var m members
96-
if err := json.Unmarshal(data, &m); err != nil {
97-
return err
98-
}
94+
if item.Kind == StructType {
9995
item.Data = make([]Type, 0)
10096
item.Keys = make([]Type, 0)
101-
for i := range m.Members {
102-
switch m.Members[i].Kind {
97+
for i := range item.Members {
98+
switch item.Members[i].Kind {
10399
case "data":
104-
item.Data = append(item.Data, m.Members[i])
100+
item.Data = append(item.Data, item.Members[i])
105101
case "keys":
106-
item.Keys = append(item.Keys, m.Members[i])
102+
item.Keys = append(item.Keys, item.Members[i])
107103
}
108104
}
109105
}

pkg/abi/data_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ func TestEventItem_UnmarshalJSON(t *testing.T) {
4343
Type: "event",
4444
Kind: "struct",
4545
},
46+
Members: []Type{
47+
{
48+
Kind: "data",
49+
Name: "from",
50+
Type: "core::starknet::contract_address::ContractAddress",
51+
}, {
52+
Kind: "data",
53+
Name: "to",
54+
Type: "core::starknet::contract_address::ContractAddress",
55+
}, {
56+
Kind: "data",
57+
Name: "value",
58+
Type: "core::integer::u256",
59+
},
60+
},
4661
Data: []Type{
4762
{
4863
Kind: "data",

pkg/abi/decode.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,16 @@ func DecodeEventData(data []string, typ EventItem, structs map[string]*StructIte
112112
tail = data
113113
err error
114114

115-
eventData = append(typ.Keys, typ.Data...)
115+
eventData []Type
116116
)
117117

118+
if len(typ.Members) > 0 {
119+
eventData = typ.Members
120+
} else {
121+
eventData = append(eventData, typ.Keys...)
122+
eventData = append(eventData, typ.Data...)
123+
}
124+
118125
for i := range eventData {
119126
tail, err = decodeItem(tail, eventData[i], structs, enums, result)
120127
if err != nil {

pkg/abi/decode_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,94 @@ func TestDecodeFunctionCallData(t *testing.T) {
10701070
})
10711071
}
10721072
}
1073+
1074+
func TestDecodeEventData(t *testing.T) {
1075+
type args struct {
1076+
data []string
1077+
typ EventItem
1078+
structs map[string]*StructItem
1079+
enums map[string]*EnumItem
1080+
}
1081+
tests := []struct {
1082+
name string
1083+
args args
1084+
want map[string]any
1085+
wantErr bool
1086+
}{
1087+
{
1088+
name: "test 1",
1089+
args: args{
1090+
data: []string{
1091+
"0x2a",
1092+
"0x1a62446e05ee60540d94b2e731ed037a1798065f9b8e719e293180b493b91f7",
1093+
},
1094+
typ: EventItem{
1095+
Type: Type{
1096+
Name: "hello::HelloStarknet::BalanceIncreased",
1097+
Type: "event",
1098+
Kind: "struct",
1099+
},
1100+
Members: []Type{
1101+
{
1102+
Name: "amount",
1103+
Type: "core::felt252",
1104+
Kind: "data",
1105+
}, {
1106+
Name: "by",
1107+
Type: "core::starknet::contract_address::ContractAddress",
1108+
Kind: "key",
1109+
},
1110+
},
1111+
Data: []Type{
1112+
{
1113+
Name: "amount",
1114+
Type: "core::felt252",
1115+
Kind: "data",
1116+
},
1117+
},
1118+
Keys: []Type{
1119+
{
1120+
Name: "by",
1121+
Type: "core::starknet::contract_address::ContractAddress",
1122+
Kind: "key",
1123+
},
1124+
},
1125+
},
1126+
structs: map[string]*StructItem{
1127+
"hello::HelloStarknet::BalanceIncreased": {
1128+
Members: []Member{
1129+
{
1130+
Type: Type{
1131+
Name: "amount",
1132+
Type: "core::felt252",
1133+
Kind: "data",
1134+
},
1135+
}, {
1136+
Type: Type{
1137+
Name: "by",
1138+
Type: "core::starknet::contract_address::ContractAddress",
1139+
Kind: "key",
1140+
},
1141+
},
1142+
},
1143+
},
1144+
},
1145+
enums: map[string]*EnumItem{},
1146+
},
1147+
want: map[string]any{
1148+
"amount": "0x2a",
1149+
"by": "0x1a62446e05ee60540d94b2e731ed037a1798065f9b8e719e293180b493b91f7",
1150+
},
1151+
wantErr: false,
1152+
},
1153+
}
1154+
for _, tt := range tests {
1155+
t.Run(tt.name, func(t *testing.T) {
1156+
got, err := DecodeEventData(tt.args.data, tt.args.typ, tt.args.structs, tt.args.enums)
1157+
require.Equal(t, tt.wantErr, err != nil)
1158+
if !tt.wantErr {
1159+
require.Equal(t, tt.want, got)
1160+
}
1161+
})
1162+
}
1163+
}

pkg/rpc/syncing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (s *Syncing) UnmarshalJSON(data []byte) error {
3434
return nil
3535
}
3636

37-
// Syncing - Returns an object about the sync status, or false if the node is not synching
37+
// Syncing - Returns an object about the sync status, or false if the node is not syncing
3838
func (api API) Syncing(ctx context.Context, opts ...RequestOption) (*Response[Syncing], error) {
3939
request := api.prepareRequest("starknet_syncing", []any{}, opts...)
4040

0 commit comments

Comments
 (0)