-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a473e76
commit 89df0a4
Showing
5 changed files
with
151 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,78 @@ | ||
package starknet_events | ||
|
||
import ( | ||
starknetABI "github.com/dipdup-io/starknet-go-api/pkg/abi" | ||
"encoding/json" | ||
|
||
"github.com/streamingfast/substreams-codegen/loop" | ||
) | ||
|
||
type ABI struct { | ||
decodedAbi *starknetABI.Abi | ||
raw string | ||
decodedEvents StarknetEvents | ||
raw string | ||
} | ||
|
||
type StarknetEvents []*StarknetEvent | ||
|
||
type StarknetEvent struct { | ||
CommonAttribute | ||
|
||
Variants []CommonAttribute `json:"variants"` | ||
} | ||
|
||
type StarknetABI struct { | ||
type OtherItem struct { | ||
CommonAttribute | ||
} | ||
type CommonAttribute struct { | ||
Type string `json:"type"` | ||
Name string `json:"name"` | ||
Kind string `json:"kind"` | ||
} | ||
|
||
const ( | ||
EventType = "event" | ||
) | ||
|
||
func (s *StarknetEvents) ExtractEvents(data []byte) error { | ||
var Attributes []CommonAttribute | ||
if err := json.Unmarshal(data, &Attributes); err != nil { | ||
return err | ||
} | ||
|
||
items := make([]interface{}, 0) | ||
|
||
for _, attribute := range Attributes { | ||
switch attribute.Type { | ||
case EventType: | ||
items = append(items, &StarknetEvent{}) | ||
default: | ||
items = append(items, &OtherItem{}) | ||
} | ||
} | ||
|
||
if err := json.Unmarshal(data, &items); err != nil { | ||
return err | ||
} | ||
|
||
for _, item := range items { | ||
switch i := item.(type) { | ||
case *StarknetEvent: | ||
*s = append(*s, i) | ||
default: | ||
continue | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CmdDecodeABI(contract *Contract) loop.Cmd { | ||
return func() loop.Msg { | ||
contractABI := starknetABI.Abi{} | ||
err := contractABI.UnmarshalJSON(contract.RawABI) | ||
events := StarknetEvents{} | ||
err := events.ExtractEvents(contract.RawABI) | ||
if err != nil { | ||
panic("decoding contract abi") | ||
} | ||
|
||
return ReturnRunDecodeContractABI{Abi: &ABI{&contractABI, string(contract.RawABI)}, Err: err} | ||
return ReturnRunDecodeContractABI{Abi: &ABI{events, string(contract.RawABI)}, Err: err} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package starknet_events | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func setNonGoldenAliases(potentialsGoldenEvent map[string]*StarknetEvent, goldenName string, aliases []*Alias) []*Alias { | ||
for _, event := range potentialsGoldenEvent { | ||
eventName := event.Name | ||
|
||
if eventName == goldenName { | ||
continue | ||
} | ||
|
||
_, newName := eventNameInfo(eventName) | ||
|
||
alias := NewAlias(event.Name, newName) | ||
aliases = append(aliases, alias) | ||
} | ||
|
||
return aliases | ||
} | ||
|
||
func eventNameInfo(eventName string) (lastPart, aliasName string) { | ||
splitEventName := strings.Split(eventName, "::") | ||
if len(splitEventName) < 2 { | ||
panic("parsed event name does not contain enough parts to have an alias") | ||
} | ||
|
||
lastPart = splitEventName[len(splitEventName)-1] | ||
return lastPart, splitEventName[len(splitEventName)-2] + lastPart | ||
} | ||
|
||
func detectGoldenEvent(potentialsGoldenEvent map[string]*StarknetEvent) string { | ||
for _, event := range potentialsGoldenEvent { | ||
seen := make(map[string]struct{}) | ||
for _, variant := range event.Variants { | ||
if _, found := potentialsGoldenEvent[variant.Type]; found { | ||
seen[variant.Type] = struct{}{} | ||
} | ||
|
||
// Equivalent: Current Event Enum contains all other potentials "golden" events | ||
if len(seen) == (len(potentialsGoldenEvent) - 1) { | ||
return event.Name | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} |