Skip to content

Commit

Permalink
Merge branch 'develop' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Oct 30, 2024
2 parents 2b6ddba + b660d6a commit 41b090c
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
env:
TEST_LOCAL_CODEGEN: "true"
RUN_INTEGRATION_TESTS: "true"
CI: "true"
CODEGEN_MAINNET_API_KEY: ${{ secrets.CODEGEN_MAINNET_API_KEY }}
REGISTRY: ghcr.io
steps:
Expand Down
8 changes: 4 additions & 4 deletions evm-events-calls/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func (c *Convo) NextStep() (out loop.Cmd) {
if dynContract.Abi == nil {
// if the user pasted an empty ABI, we would restart the process or choosing a contract address
if dynContract.emptyABI {
dynContract.referenceContractAddress = "" // reset the reference address
dynContract.ReferenceContractAddress = "" // reset the reference address
dynContract.emptyABI = false // reset the flag
return notifyContext(cmd(AskContractAddress{}))
}
if dynContract.RawABI == nil {
if dynContract.referenceContractAddress == "" {
if dynContract.ReferenceContractAddress == "" {
if p.ChainConfig().ApiEndpoint == "" {
return notifyContext(cmd(AskDynamicContractABI{}))
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
}

contract := c.State.dynamicContractOf(factory.Name)
contract.referenceContractAddress = inputAddress
contract.ReferenceContractAddress = inputAddress

return c.NextStep()

Expand Down Expand Up @@ -422,7 +422,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
contract := c.State.dynamicContractOf(factory.Name)
if msg.err != nil {
return loop.Seq(
c.Msg().Messagef("Cannot fetch the ABI for dynamic contract %q (%s)", contract.referenceContractAddress, msg.err).Cmd(),
c.Msg().Messagef("Cannot fetch the ABI for dynamic contract %q (%s)", contract.ReferenceContractAddress, msg.err).Cmd(),
cmd(AskDynamicContractABI{}),
)
}
Expand Down
84 changes: 77 additions & 7 deletions evm-events-calls/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,74 @@ func isValidChainName(input string) bool {
return ChainConfigByID[input] != nil
}

func (p *Project) ApplyEventsBlockFilter() bool {
for _, dcontract := range p.DynamicContracts {
if dcontract.TrackEvents {
return false
}
}

for _, contract := range p.Contracts {
if contract.TrackEvents {
return true
}
}

return false
}

func (p *Project) ApplyCallsBlockFilter() bool {
for _, dcontract := range p.DynamicContracts {
if dcontract.TrackCalls {
return false
}
}

for _, contract := range p.Contracts {
if contract.TrackCalls {
return true
}
}

return false
}

func (p *Project) GenerateEventsBlockFilterQuery() string {
var query string
for _, contract := range p.Contracts {
if !contract.TrackEvents {
continue
}

if query == "" {
query = fmt.Sprintf("evt_addr:%s", contract.Address)
continue
}

query += fmt.Sprintf(" || evt_addr:%s", contract.Address)
}

return query
}

func (p *Project) GenerateCallsBlockFilterQuery() string {
var query string
for _, contract := range p.Contracts {
if !contract.TrackCalls {
continue
}

if query == "" {
query = fmt.Sprintf("call_to:%s", contract.Address)
continue
}

query += fmt.Sprintf(" || call_to:%s", contract.Address)
}

return query
}

func (p *Project) TrackAnyCalls() bool {
for _, contract := range p.Contracts {
if contract.TrackCalls {
Expand Down Expand Up @@ -268,18 +336,20 @@ type DynamicContract struct {
ParentContractName string `json:"parentContractName"`

parentContract *Contract
referenceContractAddress string
ReferenceContractAddress string `json:"referenceContractAddress"`
}

func (d DynamicContract) FactoryInitialBlock() uint64 {
return *d.parentContract.InitialBlock
}

func (d DynamicContract) GenerateStoreQuery() string {
return fmt.Sprintf("evt_addr:%s && evt_sig:%s", d.parentContract.Address, "0x"+d.parentContract.FactoryCreationEvent)
}
func (d DynamicContract) ParentContract() *Contract { return d.parentContract }
func (d DynamicContract) Identifier() string { return d.Name }
func (d DynamicContract) IdentifierSnakeCase() string { return kace.Snake(d.Name) }
func (d DynamicContract) FetchABI(chainConfig *ChainConfig) (abi string, err error) {
a, err := getContractABIFollowingProxy(context.Background(), d.referenceContractAddress, chainConfig)
a, err := getContractABIFollowingProxy(context.Background(), d.ReferenceContractAddress, chainConfig)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -317,7 +387,7 @@ func validateContractAddress(p *Project, address string) error {
}

for _, dynamicContract := range p.DynamicContracts {
if dynamicContract.referenceContractAddress == address {
if dynamicContract.ReferenceContractAddress == address {
return fmt.Errorf("contract address %s already exists in the project", address)
}
}
Expand Down Expand Up @@ -347,12 +417,12 @@ func validateIncomingState(p *Project) error {
return fmt.Errorf("contract with name %s already exists in the project", dynamicContract.Name)
}

if _, found := uniqueContractAddresses[dynamicContract.referenceContractAddress]; found {
return fmt.Errorf("contract address %s already exists in the project", dynamicContract.referenceContractAddress)
if _, found := uniqueContractAddresses[dynamicContract.ReferenceContractAddress]; found {
return fmt.Errorf("contract address %s already exists in the project", dynamicContract.ReferenceContractAddress)
}

uniqueContractNames[dynamicContract.Name] = struct{}{}
uniqueContractAddresses[dynamicContract.referenceContractAddress] = struct{}{}
uniqueContractAddresses[dynamicContract.ReferenceContractAddress] = struct{}{}
}

return nil
Expand Down
11 changes: 11 additions & 0 deletions evm-events-calls/templates/sql/substreams.yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package:
imports:
sql: https://github.com/streamingfast/substreams-sink-sql/releases/download/protodefs-v{{ .SQLImportVersion }}/substreams-sink-sql-protodefs-v{{ .SQLImportVersion }}.spkg
database_change: https://github.com/streamingfast/substreams-sink-database-changes/releases/download/v{{ .DatabaseChangeImportVersion }}/substreams-database-change-v{{ .DatabaseChangeImportVersion }}.spkg
ethcommon: https://spkg.io/streamingfast/ethereum-common-v0.3.0.spkg

protobuf:
files:
Expand Down Expand Up @@ -33,6 +34,11 @@ modules:
- name: map_events
kind: map
initialBlock: {{ .MustLowestStartBlock }}
blockFilter:
module: ethcommon:index_events
{{- $eventQuery := .GenerateEventsBlockFilterQuery }}
query:
string: {{ $eventQuery }}
inputs:
- source: sf.ethereum.type.v2.Block
{{- range $index, $ddsContract := .DynamicContracts }}
Expand Down Expand Up @@ -60,6 +66,11 @@ modules:
- name: map_calls
kind: map
initialBlock: {{ .MustLowestStartBlock }}
blockFilter:
module: ethcommon:index_calls
{{ $callQuery := .GenerateCallsBlockFilterQuery }}
query:
string: {{ $callQuery }}
inputs:
- source: sf.ethereum.type.v2.Block
{{- range $ddsContract := .DynamicContracts }}
Expand Down
21 changes: 21 additions & 0 deletions evm-events-calls/templates/substreams.yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package:
name: {{ .ModuleName }}
version: v0.1.0

imports:
ethcommon: https://spkg.io/streamingfast/ethereum-common-v0.3.0.spkg

protobuf:
files:
- contract.proto
Expand All @@ -24,6 +27,10 @@ modules:
initialBlock: {{ with $ddsContract.ParentContract.InitialBlock }}{{ . }}{{ else }}0{{ end }}
updatePolicy: set
valueType: proto:dynamic_datasource
blockFilter:
module: ethcommon:index_events
query:
string: {{ $ddsContract.GenerateStoreQuery }}
inputs:
- source: sf.ethereum.type.v2.Block
{{- end}}
Expand All @@ -32,6 +39,13 @@ modules:
- name: map_events
kind: map
initialBlock: {{ .MustLowestStartBlock }}
{{- if .ApplyEventsBlockFilter }}
blockFilter:
module: ethcommon:index_events
{{- $eventQuery := .GenerateEventsBlockFilterQuery }}
query:
string: {{ $eventQuery }}
{{- end }}
inputs:
- source: sf.ethereum.type.v2.Block
{{- range $index, $ddsContract := .DynamicContracts }}
Expand All @@ -46,6 +60,13 @@ modules:
- name: map_calls
kind: map
initialBlock: {{ .MustLowestStartBlock }}
{{- if .ApplyCallsBlockFilter }}
blockFilter:
module: ethcommon:index_calls
{{- $callQuery := .GenerateCallsBlockFilterQuery }}
query:
string: {{ $callQuery }}
{{- end }}
inputs:
- source: sf.ethereum.type.v2.Block
{{- range $index, $ddsContract := .DynamicContracts }}
Expand Down
66 changes: 59 additions & 7 deletions starknet-events/abi.go
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}
}
}
Loading

0 comments on commit 41b090c

Please sign in to comment.