Skip to content

Commit

Permalink
Merge pull request #9 from streamingfast/feature/with_index_module
Browse files Browse the repository at this point in the history
add index module on codegens
  • Loading branch information
sduchesneau authored Oct 30, 2024
2 parents 89df0a4 + 0ec3982 commit b660d6a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
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

0 comments on commit b660d6a

Please sign in to comment.