Skip to content

Commit

Permalink
Merge branch 'feature/multiple-contracts-code-gen' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Oct 12, 2023
2 parents 82ce6f1 + 3d44d88 commit ff69904
Show file tree
Hide file tree
Showing 33 changed files with 1,657 additions and 158 deletions.
347 changes: 298 additions & 49 deletions cmd/substreams/init.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions codegen/sinkchoice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package codegen

//go:generate go-enum -f=$GOFILE --marshal --names --nocase

// ENUM(
//
// No
// Db
// Graph
//
// )
type SinkChoice int
96 changes: 96 additions & 0 deletions codegen/sinkchoice_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions codegen/templates/ethereum/Cargo.toml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ prost = "0.11"
prost-types = "0.11"
substreams = "0.5"
substreams-ethereum = "0.9"
{{- if eq .sinkChoice 1 }}
substreams-database-change = "1"
{{- else if eq .sinkChoice 2 }}
substreams-entity-change = "1"
{{- end }}

# Required so that ethabi > ethereum-types build correctly under wasm32-unknown-unknown
[target.wasm32-unknown-unknown.dependencies]
Expand Down
5 changes: 2 additions & 3 deletions codegen/templates/ethereum/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ENDPOINT ?= mainnet.eth.streamingfast.io:443
CARGO_VERSION := $(shell cargo version 2>/dev/null)

.PHONY: build
Expand All @@ -12,11 +11,11 @@ endif

.PHONY: run
run: build
substreams run -e $(ENDPOINT) substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))
substreams run substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))

.PHONY: gui
gui: build
substreams gui -e $(ENDPOINT) substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))
substreams gui substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))

.PHONY: protogen
protogen:
Expand Down
5 changes: 2 additions & 3 deletions codegen/templates/ethereum/Makefile.gotmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ENDPOINT ?= mainnet.eth.streamingfast.io:443
CARGO_VERSION := $(shell cargo version 2>/dev/null)

.PHONY: build
Expand All @@ -12,11 +11,11 @@ endif

.PHONY: run
run: build
substreams run -e $(ENDPOINT) substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))
substreams run substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))

.PHONY: gui
gui: build
substreams gui -e $(ENDPOINT) substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))
substreams gui substreams.yaml map_events $(if $(START_BLOCK),-s $(START_BLOCK)) $(if $(STOP_BLOCK),-t $(STOP_BLOCK))

.PHONY: protogen
protogen:
Expand Down
34 changes: 23 additions & 11 deletions codegen/templates/ethereum/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@ use std::fs::File;
use std::io::Write;

fn main() -> Result<(), anyhow::Error> {
let contents = fs::read_to_string("abi/contract.abi.json")
.expect("Should have been able to read the file");
let file_names = [
"abi/contract.abi.json",
];
let file_output_names = [
"src/abi/contract.rs",
];

// sanitize fields and attributes starting with an underscore
let regex = Regex::new(r#"("\w+"\s?:\s?")_(\w+")"#).unwrap();
let sanitized_abi_file = regex.replace_all(contents.as_str(), "${1}u_${2}");
let mut i = 0;
for f in file_names {
let contents = fs::read_to_string(f)
.expect("Should have been able to read the file");

// do not modify the original abi
let mut file = File::create("/tmp/contract.abi.json")?;
file.write_all(sanitized_abi_file.as_bytes())?;
// sanitize fields and attributes starting with an underscore
let regex = Regex::new(r#"("\w+"\s?:\s?")_(\w+")"#).unwrap();
let sanitized_abi_file = regex.replace_all(contents.as_str(), "${1}u_${2}");

Abigen::new("Contract", "/tmp/contract.abi.json")?
.generate()?
.write_to_file("src/abi/contract.rs")?;
// do not modify the original abi
let mut file = File::create("/tmp/contract.abi.json")?;
file.write_all(sanitized_abi_file.as_bytes())?;

Abigen::new("Contract", "/tmp/contract.abi.json")?
.generate()?
.write_to_file(file_output_names[i])?;

i = i+1;
}

Ok(())
}
42 changes: 42 additions & 0 deletions codegen/templates/ethereum/build.rs.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::{Ok, Result};
use regex::Regex;
use substreams_ethereum::Abigen;
use std::fs;
use std::fs::File;
use std::io::Write;

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",
{{- end }}
];
let file_output_names = [
{{- range $i, $contract := .ethereumContracts }}
"src/abi/{{ $contract.GetName }}{{ if eq $numberOfContracts 1 }}{{ else }}_{{ end }}contract.rs",
{{- end }}
];

let mut i = 0;
for f in file_names {
let contents = fs::read_to_string(f)
.expect("Should have been able to read the file");

// sanitize fields and attributes starting with an underscore
let regex = Regex::new(r#"("\w+"\s?:\s?")_(\w+")"#).unwrap();
let sanitized_abi_file = regex.replace_all(contents.as_str(), "${1}u_${2}");

// do not modify the original abi
let mut file = File::create("/tmp/contract.abi.json")?;
file.write_all(sanitized_abi_file.as_bytes())?;

Abigen::new("Contract", "/tmp/contract.abi.json")?
.generate()?
.write_to_file(file_output_names[i])?;

i = i+1;
}

Ok(())
}
20 changes: 19 additions & 1 deletion codegen/templates/ethereum/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/streamingfast/substreams/codegen"

"github.com/streamingfast/cli"
"github.com/streamingfast/eth-go"
"github.com/streamingfast/substreams/codegen/templates"
Expand All @@ -20,7 +22,23 @@ func main() {

chain := templates.EthereumChainsByID["Mainnet"]

project, err := templates.NewEthereumProject("substreams-init-test", "substreams_init_test", chain, eth.MustNewAddress("0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"), abi, string(abiContent), 123)
ethereumContracts := []*templates.EthereumContract{templates.NewEthereumContract(
"substreams-init-tests",
eth.MustNewAddress("0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"),
nil,
abi,
string(abiContent),
)}

project, err := templates.NewEthereumProject(
"substreams-init-test",
"substreams_init_test",
chain,
ethereumContracts,
123,
codegen.SinkChoiceDb,
)

cli.NoError(err, "Unable to create Ethereum project")

files, err := project.Render()
Expand Down
16 changes: 11 additions & 5 deletions codegen/templates/ethereum/proto/contract.proto.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ syntax = "proto3";
import "google/protobuf/timestamp.proto";

package contract.v1;

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

{{- range $event := $.events }}
{{- range $i, $contract := .ethereumContracts}}
{{- range $event := $contract.GetEvents }}
{{ $proto := $event.Proto }}
message {{$proto.MessageName}} {
message {{ if ne $numberOfContracts 1 }}{{ $contract.GetName }}_{{ end }}{{ $proto.MessageName }} {
string evt_tx_hash = 1;
uint32 evt_index = 2;
google.protobuf.Timestamp evt_block_time = 3;
Expand All @@ -23,3 +28,4 @@ message {{$proto.MessageName}} {
{{- end}}
}
{{- end}}
{{- end}}
33 changes: 33 additions & 0 deletions codegen/templates/ethereum/results/dbsink/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "substreams-init-test"
version = "0.0.1"
edition = "2021"

[lib]
name = "substreams"
crate-type = ["cdylib"]

[dependencies]
ethabi = "17"
hex-literal = "0.3.4"
num-bigint = "0.4"
num-traits = "0.2.15"
prost = "0.11"
prost-types = "0.11"
substreams = "0.5"
substreams-ethereum = "0.9"
substreams-database-change = "1"

# Required so that ethabi > ethereum-types build correctly under wasm32-unknown-unknown
[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["custom"] }

[build-dependencies]
anyhow = "1"
substreams-ethereum = "0.9"
regex = "1.8"

[profile.release]
lto = true
opt-level = 's'
strip = "debuginfo"
Loading

0 comments on commit ff69904

Please sign in to comment.