-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/dds
also fix tiny Makefile template issue in test case
- Loading branch information
Showing
8 changed files
with
194 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
codegen/templates/ethereum/src/multiple_contracts_lib.rs.gotmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
mod abi; | ||
mod pb; | ||
use hex_literal::hex; | ||
use pb::contract::v1 as contract; | ||
use substreams::Hex; | ||
use substreams_database_change::pb::database::DatabaseChanges; | ||
use substreams_database_change::tables::Tables as DatabaseChangeTables; | ||
use substreams_entity_change::pb::entity::EntityChanges; | ||
use substreams_entity_change::tables::Tables as EntityChangesTables; | ||
use substreams_ethereum::pb::eth::v2 as eth; | ||
use substreams_ethereum::Event; | ||
|
||
#[allow(unused_imports)] | ||
use num_traits::cast::ToPrimitive; | ||
use std::str::FromStr; | ||
use substreams::scalar::BigDecimal; | ||
|
||
substreams_ethereum::init!(); | ||
|
||
{{ range $i, $contract := .ethereumContracts -}} | ||
const {{ toUpper $contract.GetName }}_TRACKED_CONTRACT: [u8; 20] = hex!("{{ $contract.GetAddress }}"); | ||
{{ end -}} | ||
{{ $numberOfContracts := len .ethereumContracts }} | ||
|
||
{{- range $i, $contract := .ethereumContracts }} | ||
fn map_{{ $contract.GetName }}_events(blk: ð::Block, events: &mut contract::Events) { | ||
{{- range $event := $contract.GetEvents }} | ||
{{- $rust := $event.Rust }} | ||
events.{{ $contract.GetName }}_{{ $rust.ProtoOutputModuleFieldName }}.append(&mut blk | ||
.receipts() | ||
.flat_map(|view| { | ||
view.receipt.logs.iter() | ||
.filter(|log| log.address == {{ toUpper $contract.GetName }}_TRACKED_CONTRACT) | ||
.filter_map(|log| { | ||
if let Some(event) = abi::{{ if eq $numberOfContracts 1 }}contract{{ else }}{{ $contract.GetName }}_contract{{ end }}::events::{{$rust.ABIStructName}}::match_and_decode(log) { | ||
return Some(contract::{{ capitalizeFirst $contract.GetName }}{{$rust.ProtoMessageName}} { | ||
evt_tx_hash: Hex(&view.transaction.hash).to_string(), | ||
evt_index: log.block_index, | ||
evt_block_time: Some(blk.timestamp().to_owned()), | ||
evt_block_number: blk.number, | ||
{{- range $protoField, $abiToProtoConversion := $rust.ProtoFieldABIConversionMap }} | ||
{{$protoField}}: {{$abiToProtoConversion}}, | ||
{{- end}} | ||
}); | ||
} | ||
|
||
None | ||
}) | ||
}) | ||
.collect()); | ||
{{- end }} | ||
} | ||
{{ end }} | ||
|
||
{{- range $i, $contract := .ethereumContracts }} | ||
fn db_{{ $contract.GetName }}_out(events: &contract::Events, tables: &mut DatabaseChangeTables) { | ||
// Loop over all the abis events to create table changes | ||
{{- range $event := $contract.GetEvents }} | ||
{{- $rust := $event.Rust }} | ||
events.{{ $contract.GetName }}_{{ $rust.ProtoOutputModuleFieldName }}.iter().for_each(|evt| { | ||
tables | ||
.create_row("{{ if eq $numberOfContracts 1 }}{{ $rust.TableChangeEntityName }}{{ else }}{{ $contract.GetName }}_{{ $rust.TableChangeEntityName }}{{ end }}", [("evt_tx_hash", evt.evt_tx_hash.to_string()),("evt_index", evt.evt_index.to_string())]) | ||
.set("evt_block_time", evt.evt_block_time.as_ref().unwrap()) | ||
.set("evt_block_number", evt.evt_block_number) | ||
{{- $numberOfAttributes := len $rust.ProtoFieldTableChangesMap }}{{ if eq $numberOfAttributes 0 }};{{ end }} | ||
{{- $i := 0 }} | ||
{{- range $protoField, $changesToProtoConversion := $rust.ProtoFieldTableChangesMap }} | ||
{{ $i = add $i 1 }}.{{$changesToProtoConversion.Setter}}("{{$protoField}}", {{$changesToProtoConversion.ValueAccessCode}}){{if eq $i $numberOfAttributes}};{{ end }} | ||
{{- end}} | ||
}); | ||
{{- end}} | ||
} | ||
{{- end }} | ||
|
||
{{ range $i, $contract := .ethereumContracts }} | ||
fn graph_{{ $contract.GetName }}_out(events: &contract::Events, tables: &mut EntityChangesTables) { | ||
// Loop over all the abis events to create table changes | ||
{{- range $event := $contract.GetEvents }} | ||
{{- $rust := $event.Rust }} | ||
events.{{ $contract.GetName }}_{{ $rust.ProtoOutputModuleFieldName }}.iter().for_each(|evt| { | ||
tables | ||
.create_row("{{ if eq $numberOfContracts 1 }}{{ $rust.TableChangeEntityName }}{{ else }}{{ $contract.GetName }}_{{ $rust.TableChangeEntityName }}{{ end }}", format!("{}-{}", evt.evt_tx_hash, evt.evt_index)) | ||
.set("evt_tx_hash", &evt.evt_tx_hash) | ||
.set("evt_index", evt.evt_index) | ||
.set("evt_block_time", evt.evt_block_time.as_ref().unwrap()) | ||
.set("evt_block_number", evt.evt_block_number) | ||
{{- $numberOfAttributes := len $rust.ProtoFieldTableChangesMap }}{{ if eq $numberOfAttributes 0 }};{{ end }} | ||
{{- $i := 0 }} | ||
{{- range $protoField, $changesToProtoConversion := $rust.ProtoFieldTableChangesMap }} | ||
{{ $i = add $i 1 }}.set("{{$protoField}}", {{$changesToProtoConversion.ValueAccessCode}}){{if eq $i $numberOfAttributes}};{{ end }} | ||
{{- end}} | ||
}); | ||
{{- end}} | ||
} | ||
{{- end }} | ||
|
||
#[substreams::handlers::map] | ||
fn map_events(blk: eth::Block) -> Result<contract::Events, substreams::errors::Error> { | ||
let mut events = contract::Events::default(); | ||
{{- range $i, $contract := .ethereumContracts }} | ||
map_{{ $contract.GetName }}_events(&blk, &mut events); | ||
{{- end }} | ||
Ok(events) | ||
} | ||
|
||
#[substreams::handlers::map] | ||
fn db_out(events: contract::Events) -> Result<DatabaseChanges, substreams::errors::Error> { | ||
// Initialize Database Changes container | ||
let mut tables = DatabaseChangeTables::new(); | ||
{{- range $i, $contract := .ethereumContracts }} | ||
db_{{ $contract.GetName }}_out(&events, &mut tables); | ||
{{- end }} | ||
Ok(tables.to_database_changes()) | ||
} | ||
|
||
#[substreams::handlers::map] | ||
fn graph_out(events: contract::Events) -> Result<EntityChanges, substreams::errors::Error> { | ||
// Initialize Database Changes container | ||
let mut tables = EntityChangesTables::new(); | ||
{{- range $i, $contract := .ethereumContracts }} | ||
graph_{{ $contract.GetName }}_out(&events, &mut tables); | ||
{{- end }} | ||
Ok(tables.to_entity_changes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.