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 3902723 commit 59b639f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/dipdup-io/starknet-indexer
go 1.22

require (
github.com/dipdup-io/starknet-go-api v0.0.0-20240616154215-7c9a3f27407d
github.com/dipdup-io/starknet-go-api v0.0.0-20240912083038-27d5587efb86
github.com/dipdup-io/workerpool v0.0.4
github.com/dipdup-net/go-lib v0.3.3
github.com/dipdup-net/indexer-sdk v0.0.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+
github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo=
github.com/dipdup-io/starknet-go-api v0.0.0-20240616154215-7c9a3f27407d h1:i7TIDgh0vyr+2zFm9PsOy8iPn5bzmAOIiM84U7d7dp8=
github.com/dipdup-io/starknet-go-api v0.0.0-20240616154215-7c9a3f27407d/go.mod h1:y3KGLFQtwzUBcT0X2LMj6CxocUimr/A9XYg+j0KIRDE=
github.com/dipdup-io/starknet-go-api v0.0.0-20240912083038-27d5587efb86 h1:XBcAdrZc6FsaNK/WqLCMpkI1nU8fFdHzQZ0pf7k8kLE=
github.com/dipdup-io/starknet-go-api v0.0.0-20240912083038-27d5587efb86/go.mod h1:y3KGLFQtwzUBcT0X2LMj6CxocUimr/A9XYg+j0KIRDE=
github.com/dipdup-io/workerpool v0.0.4 h1:m58fuFY3VIPRc+trWpjw2Lsm4FvIgtjP/4VRe79r+/s=
github.com/dipdup-io/workerpool v0.0.4/go.mod h1:m6YMqx7M+fORTyabHD/auKymBRpbDax0t1aPZ1i7GZA=
github.com/dipdup-net/go-lib v0.3.3 h1:vTUI+sT4L+x+eiMf712Cg8EtlqUCMiN6M3vcNaPlCw8=
Expand Down
10 changes: 8 additions & 2 deletions pkg/indexer/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ func CalldataForL1Handler(contractAbi abi.Abi, selector []byte, calldata []strin

// Event -
func Event(contractAbi abi.Abi, keys []string, data []string) (map[string]any, string, error) {
if len(keys) != 1 {
values := make([]string, 0)
switch len(keys) {
case 0:
return nil, "", nil
case 1:
values = append(values, data...)
default:
values = append(keys[1:], data...)

Check failure on line 66 in pkg/indexer/decode/decode.go

View workflow job for this annotation

GitHub Actions / Linter

appendAssign: append result not assigned to the same slice (gocritic)
}
selector := encoding.EncodeHex(encoding.MustDecodeHex(keys[0]))
event, ok := contractAbi.GetEventBySelector(selector)
if !ok {
return nil, "", nil
}
parsed, err := abi.DecodeEventData(data, *event, contractAbi.Structs, contractAbi.Enums)
parsed, err := abi.DecodeEventData(values, *event, contractAbi.Structs, contractAbi.Enums)
return parsed, event.Name, err
}

Expand Down
121 changes: 121 additions & 0 deletions pkg/indexer/decode/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package decode

import (
"testing"

"github.com/dipdup-io/starknet-go-api/pkg/abi"
"github.com/stretchr/testify/require"
)

func TestEvent(t *testing.T) {
type args struct {
contractAbi abi.Abi
keys []string
data []string
}
tests := []struct {
name string
args args
want map[string]any
want1 string
}{
{
name: "transfer before v0.13.3",
args: args{
contractAbi: abi.Abi{
EventsBySelector: map[string]*abi.EventItem{
"99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9": {
Type: abi.Type{
Name: "Transfer",
Type: "event",
},
Data: []abi.Type{
{
Name: "from",
Type: "core::starknet::contract_address::ContractAddress",
},
{
Name: "to",
Type: "core::starknet::contract_address::ContractAddress",
},
{
Name: "amount",
Type: "core::integer::u256",
},
},
Keys: []abi.Type{},
},
},
},
keys: []string{
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
},
data: []string{
"0x3a39bfdf7aad9504978afc793d5c1e2c8d9fc6f2e02720aebc52c9817a83e42",
"0x0",
"0x21dcf820ad93f",
"0x0",
},
},
want: map[string]any{
"from": "0x3a39bfdf7aad9504978afc793d5c1e2c8d9fc6f2e02720aebc52c9817a83e42",
"to": "0x0",
"amount": "595727030606143",
},
want1: "Transfer",
}, {
name: "transfer after v0.13.3",
args: args{
contractAbi: abi.Abi{
EventsBySelector: map[string]*abi.EventItem{
"99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9": {
Type: abi.Type{
Name: "Transfer",
Type: "event",
},
Data: []abi.Type{
{
Name: "amount",
Type: "core::integer::u256",
},
},
Keys: []abi.Type{
{
Name: "from",
Type: "core::starknet::contract_address::ContractAddress",
},
{
Name: "to",
Type: "core::starknet::contract_address::ContractAddress",
},
},
},
},
},
keys: []string{
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
"0x3a39bfdf7aad9504978afc793d5c1e2c8d9fc6f2e02720aebc52c9817a83e42",
"0x0",
},
data: []string{
"0x21dcf820ad93f",
"0x0",
},
},
want: map[string]any{
"from": "0x3a39bfdf7aad9504978afc793d5c1e2c8d9fc6f2e02720aebc52c9817a83e42",
"to": "0x0",
"amount": "595727030606143",
},
want1: "Transfer",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := Event(tt.args.contractAbi, tt.args.keys, tt.args.data)
require.NoError(t, err)
require.Equal(t, tt.want, got)
require.Equal(t, tt.want1, got1)
})
}
}

0 comments on commit 59b639f

Please sign in to comment.