Skip to content

Commit

Permalink
init codegen: always set a prefix name
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Jan 30, 2024
1 parent 82ec4ea commit 2ea9458
Show file tree
Hide file tree
Showing 23 changed files with 320 additions and 436 deletions.
13 changes: 5 additions & 8 deletions cmd/substreams/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("running contract prompt: %w", err)
}

if len(ethereumContracts) != 1 {
// more than one contract to track, need to set the short names of the contracts
fmt.Printf("Tracking %d contracts, let's define a short name for each contract\n", len(ethereumContracts))
ethereumContracts, err = promptEthereumContractShortNames(ethereumContracts)
if err != nil {
return fmt.Errorf("running short name contract prompt: %w", err)
}
fmt.Printf("Tracking %d contract(s), let's define a short name for each contract\n", len(ethereumContracts))
ethereumContracts, err = promptEthereumContractShortNames(ethereumContracts)
if err != nil {
return fmt.Errorf("running short name contract prompt: %w", err)
}

fmt.Printf("Retrieving %s contract information (ABI & creation block)\n", chain.DisplayName)
Expand All @@ -139,7 +136,7 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {

for _, contract := range ethereumContracts {
fmt.Printf("Generating ABI Event models for %s\n", contract.GetName())
events, err := templates.BuildEventModels(contract.GetAbi(), len(ethereumContracts) > 1)
events, err := templates.BuildEventModels(contract.GetAbi())
if err != nil {
return fmt.Errorf("build ABI event models for contract [%s - %s]: %w", contract.GetAddress(), contract.GetName(), err)
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/ethereum/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::fs;

fn main() -> Result<(), anyhow::Error> {
let file_names = [
"abi/contract.abi.json",
"abi/bayc_contract.abi.json",
];
let file_output_names = [
"src/abi/contract.rs",
"src/abi/bayc_contract.rs",
];

let mut i = 0;
Expand Down
5 changes: 2 additions & 3 deletions codegen/templates/ethereum/build.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use substreams_ethereum::Abigen;
use std::fs;

fn main() -> Result<(), anyhow::Error> {
{{- $numberOfContracts := len .ethereumContracts }}
let file_names = [
{{- range $i, $contract := .ethereumContracts }}
"abi/{{ $contract.GetName }}{{ if eq $numberOfContracts 1 }}{{ else }}_{{ end }}contract.abi.json",
"abi/{{ $contract.GetName }}_contract.abi.json",
{{- end }}
];
let file_output_names = [
{{- range $i, $contract := .ethereumContracts }}
"src/abi/{{ $contract.GetName }}{{ if eq $numberOfContracts 1 }}{{ else }}_{{ end }}contract.rs",
"src/abi/{{ $contract.GetName }}_contract.rs",
{{- end }}
];

Expand Down
16 changes: 8 additions & 8 deletions codegen/templates/ethereum/proto/contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import "google/protobuf/timestamp.proto";
package contract.v1;

message Events {
repeated Approval approvals = 1;
repeated ApprovalForAll approval_for_alls = 2;
repeated OwnershipTransferred ownership_transferreds = 3;
repeated Transfer transfers = 4;
repeated bayc_Approval bayc_approvals = 1;
repeated bayc_ApprovalForAll bayc_approval_for_alls = 2;
repeated bayc_OwnershipTransferred bayc_ownership_transferreds = 3;
repeated bayc_Transfer bayc_transfers = 4;
}

message Approval {
message bayc_Approval {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand All @@ -21,7 +21,7 @@ message Approval {
string token_id = 7;
}

message ApprovalForAll {
message bayc_ApprovalForAll {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand All @@ -31,7 +31,7 @@ message ApprovalForAll {
bool approved = 7;
}

message OwnershipTransferred {
message bayc_OwnershipTransferred {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand All @@ -40,7 +40,7 @@ message OwnershipTransferred {
bytes new_owner = 6;
}

message Transfer {
message bayc_Transfer {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand Down
7 changes: 3 additions & 4 deletions codegen/templates/ethereum/proto/contract.proto.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ syntax = "proto3";
import "google/protobuf/timestamp.proto";

package contract.v1;
{{ $numberOfContracts := len .ethereumContracts }}
{{- $eventsCounter := 0 }}
{{ $eventsCounter := 0 }}
message Events {
{{- range $i, $contract := .ethereumContracts }}
{{- range $index, $event := $contract.GetEvents }}
{{- $proto := $event.Proto }}
{{- $eventsCounter = add $eventsCounter 1 }}
repeated {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{$proto.MessageName}} {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{$proto.OutputModuleFieldName}} = {{ $eventsCounter }};
repeated {{ $contract.GetName }}_{{$proto.MessageName}} {{ $contract.GetName }}_{{$proto.OutputModuleFieldName}} = {{ $eventsCounter }};
{{- end}}
{{- end}}
}

{{- range $i, $contract := .ethereumContracts}}
{{- range $event := $contract.GetEvents }}
{{ $proto := $event.Proto }}
message {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{ $proto.MessageName }} {
message {{ $contract.GetName }}_{{ $proto.MessageName }} {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand Down
8 changes: 4 additions & 4 deletions codegen/templates/ethereum/schema.clickhouse.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS approval (
CREATE TABLE IF NOT EXISTS bayc_approval (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand All @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS approval (
"owner" VARCHAR(40),
"token_id" UInt256
) ENGINE = MergeTree PRIMARY KEY ("evt_tx_hash","evt_index");
CREATE TABLE IF NOT EXISTS approval_for_all (
CREATE TABLE IF NOT EXISTS bayc_approval_for_all (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand All @@ -16,15 +16,15 @@ CREATE TABLE IF NOT EXISTS approval_for_all (
"operator" VARCHAR(40),
"owner" VARCHAR(40)
) ENGINE = MergeTree PRIMARY KEY ("evt_tx_hash","evt_index");
CREATE TABLE IF NOT EXISTS ownership_transferred (
CREATE TABLE IF NOT EXISTS bayc_ownership_transferred (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
"evt_block_number" UInt64,
"new_owner" VARCHAR(40),
"previous_owner" VARCHAR(40)
) ENGINE = MergeTree PRIMARY KEY ("evt_tx_hash","evt_index");
CREATE TABLE IF NOT EXISTS transfer (
CREATE TABLE IF NOT EXISTS bayc_transfer (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand Down
3 changes: 1 addition & 2 deletions codegen/templates/ethereum/schema.clickhouse.sql.gotmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{{- $numberOfContracts := len .ethereumContracts -}}
{{- range $idx, $contract := .ethereumContracts -}}
{{- range $event := $contract.GetEvents -}}
{{- $rust := $event.Rust -}}
{{- $numberOfAttributes := len $rust.ProtoFieldTableChangesMap -}}
CREATE TABLE IF NOT EXISTS {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{ $rust.TableChangeEntityName }} (
CREATE TABLE IF NOT EXISTS {{ $contract.GetName }}_{{ $rust.TableChangeEntityName }} (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand Down
8 changes: 4 additions & 4 deletions codegen/templates/ethereum/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type approval @entity {
type bayc_approval @entity {
id: ID!
evt_tx_hash: String!
evt_index: BigInt!
Expand All @@ -8,7 +8,7 @@ type approval @entity {
owner: String!
token_id: BigDecimal!
}
type approval_for_all @entity {
type bayc_approval_for_all @entity {
id: ID!
evt_tx_hash: String!
evt_index: BigInt!
Expand All @@ -18,7 +18,7 @@ type approval_for_all @entity {
operator: String!
owner: String!
}
type ownership_transferred @entity {
type bayc_ownership_transferred @entity {
id: ID!
evt_tx_hash: String!
evt_index: BigInt!
Expand All @@ -27,7 +27,7 @@ type ownership_transferred @entity {
new_owner: String!
previous_owner: String!
}
type transfer @entity {
type bayc_transfer @entity {
id: ID!
evt_tx_hash: String!
evt_index: BigInt!
Expand Down
3 changes: 1 addition & 2 deletions codegen/templates/ethereum/schema.graphql.gotmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{{- $numberOfContracts := len .ethereumContracts -}}
{{- range $idx, $contract := .ethereumContracts }}
{{- range $event := $contract.GetEvents -}}
{{- $rust := $event.Rust -}}
type {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{ $rust.TableChangeEntityName }} @entity {
type {{ $contract.GetName }}_{{ $rust.TableChangeEntityName }} @entity {
id: ID!
evt_tx_hash: String!
evt_index: BigInt!
Expand Down
8 changes: 4 additions & 4 deletions codegen/templates/ethereum/schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS approval (
CREATE TABLE IF NOT EXISTS bayc_approval (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand All @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS approval (
"token_id" DECIMAL,
PRIMARY KEY(evt_tx_hash,evt_index)
);
CREATE TABLE IF NOT EXISTS approval_for_all (
CREATE TABLE IF NOT EXISTS bayc_approval_for_all (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand All @@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS approval_for_all (
"owner" VARCHAR(40),
PRIMARY KEY(evt_tx_hash,evt_index)
);
CREATE TABLE IF NOT EXISTS ownership_transferred (
CREATE TABLE IF NOT EXISTS bayc_ownership_transferred (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand All @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS ownership_transferred (
"previous_owner" VARCHAR(40),
PRIMARY KEY(evt_tx_hash,evt_index)
);
CREATE TABLE IF NOT EXISTS transfer (
CREATE TABLE IF NOT EXISTS bayc_transfer (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand Down
3 changes: 1 addition & 2 deletions codegen/templates/ethereum/schema.sql.gotmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{{- $numberOfContracts := len .ethereumContracts -}}
{{- range $idx, $contract := .ethereumContracts -}}
{{- range $event := $contract.GetEvents -}}
{{- $rust := $event.Rust -}}
{{- $numberOfAttributes := len $rust.ProtoFieldTableChangesMap -}}
CREATE TABLE IF NOT EXISTS {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{ $rust.TableChangeEntityName }} (
CREATE TABLE IF NOT EXISTS {{ $contract.GetName }}_{{ $rust.TableChangeEntityName }} (
"evt_tx_hash" VARCHAR(64),
"evt_index" INT,
"evt_block_time" TIMESTAMP,
Expand Down
3 changes: 2 additions & 1 deletion codegen/templates/ethereum/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod contract;

pub mod bayc_contract;
5 changes: 0 additions & 5 deletions codegen/templates/ethereum/src/abi/mod.rs.gotmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{{- $numberOfContracts := len .ethereumContracts }}
{{- if eq $numberOfContracts 1 -}}
pub mod contract;
{{else }}
{{- range $i, $contract := .ethereumContracts }}
pub mod {{ $contract.GetName }}_contract;
{{- end }}
{{- end }}
Loading

0 comments on commit 2ea9458

Please sign in to comment.