-
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
Showing
13 changed files
with
273 additions
and
44 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
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
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} | ||
} | ||
} |
Oops, something went wrong.