Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add index module on codegens #9

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading