From 5ec2a323e924702ad8f947811809dcd7520ff8d9 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Fri, 5 Jul 2024 15:38:53 +0200 Subject: [PATCH] add block_min --- Cargo.toml | 2 +- Makefile | 4 ++-- src/index.rs | 14 ++++++++++++++ src/lib.rs | 22 +++------------------- src/sinks.rs | 21 +++++++++++++++++++++ src/stores.rs | 8 ++++++++ substreams.yaml | 26 +++++++++++++++++++++++--- 7 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 src/index.rs create mode 100644 src/sinks.rs create mode 100644 src/stores.rs diff --git a/Cargo.toml b/Cargo.toml index c494845..c638589 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clock" -version = "0.1.0" +version = "0.2.0" authors = ["Denis ", "Etienne "] description = "Block ID, number & timestamp" license = "MIT" diff --git a/Makefile b/Makefile index 50a8981..fe96de0 100644 --- a/Makefile +++ b/Makefile @@ -23,11 +23,11 @@ info: .PHONY: run run: - substreams run -e eth.substreams.pinax.network:9000 graph_out -s -1 + substreams run -e eth.substreams.pinax.network:443 graph_out -s -100 .PHONY: gui gui: - substreams gui -e eth.substreams.pinax.network:9000 graph_out -s -1 + substreams gui -e eth.substreams.pinax.network:443 graph_out -s 1000 -t 1001 .PHONY: deploy deploy: diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 0000000..e83352c --- /dev/null +++ b/src/index.rs @@ -0,0 +1,14 @@ +use substreams::errors::Error; +use substreams::pb::substreams::store_delta::Operation; +use substreams::pb::sf::substreams::index::v1::Keys; +use substreams::store::{DeltaInt64, Deltas}; + +#[substreams::handlers::map] +fn block_index(min_block: Deltas) -> Result { + Ok(match min_block.deltas[0].operation != Operation::Create { + true => Keys::default(), + false => Keys { + keys: vec!["clock".to_string()], + }, + }) +} diff --git a/src/lib.rs b/src/lib.rs index eea49d6..6d65a94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,3 @@ -use substreams::errors::Error; -use substreams::pb::substreams::Clock; -use substreams_entity_change::pb::entity::EntityChanges; -use substreams_entity_change::tables::Tables; - -#[substreams::handlers::map] -pub fn graph_out(clock: Clock) -> Result { - let mut tables = Tables::new(); - let timestamp = clock.timestamp.unwrap(); - - tables - .create_row("Clock", &clock.id) - .set_bigint("number", &clock.number.to_string()) - .set_bigint("seconds", ×tamp.seconds.to_string()) - .set_bigint("nanos", ×tamp.nanos.to_string()) - .set("timestamp", timestamp.to_string()); - - Ok(tables.to_entity_changes()) -} \ No newline at end of file +mod sinks; +mod index; +mod stores; \ No newline at end of file diff --git a/src/sinks.rs b/src/sinks.rs new file mode 100644 index 0000000..0fedcd4 --- /dev/null +++ b/src/sinks.rs @@ -0,0 +1,21 @@ +use substreams::errors::Error; +use substreams::pb::substreams::Clock; +use substreams_entity_change::pb::entity::EntityChanges; +use substreams_entity_change::tables::Tables; + +#[substreams::handlers::map] +pub fn graph_out(clock: Clock) -> Result { + let mut tables = Tables::new(); + let timestamp = clock.timestamp.unwrap(); + let day = timestamp.seconds / 86400; + + tables + .create_row("Clock", &clock.id) + .set_bigint("day", &day.to_string()) + .set_bigint("number", &clock.number.to_string()) + .set_bigint("seconds", ×tamp.seconds.to_string()) + .set_bigint("nanos", ×tamp.nanos.to_string()) + .set("timestamp", timestamp.to_string()); + + Ok(tables.to_entity_changes()) +} \ No newline at end of file diff --git a/src/stores.rs b/src/stores.rs new file mode 100644 index 0000000..28e6ae8 --- /dev/null +++ b/src/stores.rs @@ -0,0 +1,8 @@ +use substreams::pb::substreams::Clock; +use substreams::store::{StoreMin, StoreMinInt64, StoreNew}; + +#[substreams::handlers::store] +pub fn store_min_block(clock: Clock, store: StoreMinInt64) { + let day = clock.timestamp.unwrap().seconds / 86400; + store.min(0, day.to_string(), clock.number as i64); +} \ No newline at end of file diff --git a/substreams.yaml b/substreams.yaml index 9276058..11c669c 100644 --- a/substreams.yaml +++ b/substreams.yaml @@ -1,12 +1,12 @@ specVersion: v0.1.0 package: name: clock - version: v0.1.0 - url: https://github.com/pinax-network/subgraphs + version: v0.2.0 + url: https://github.com/pinax-network/substreams-clock doc: Block ID, number & timestamp imports: - entities: https://github.com/streamingfast/substreams-sink-entity-changes/releases/download/v1.3.0/substreams-sink-entity-changes-v1.3.0.spkg + entities: https://github.com/streamingfast/substreams-sink-entity-changes/releases/download/v1.3.2/substreams-sink-entity-changes-v1.3.2.spkg binaries: default: @@ -14,9 +14,29 @@ binaries: file: ./target/wasm32-unknown-unknown/release/clock.wasm modules: + - name: store_min_block + kind: store + updatePolicy: min + valueType: int64 + inputs: + - source: sf.substreams.v1.Clock + + - name: block_index + kind: blockIndex + inputs: + - store: store_min_block + mode: deltas + output: + type: proto:sf.substreams.index.v1.Keys + - name: graph_out kind: map inputs: - source: sf.substreams.v1.Clock + blockFilter: + module: block_index + query: + string: clock + params: false output: type: proto:sf.substreams.sink.entity.v1.EntityChanges \ No newline at end of file