Skip to content

Commit

Permalink
init clock
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Oct 21, 2024
1 parent 7f50988 commit 0c8276d
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/substreams.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ jobs:
- name: Install substreams
uses: supplypike/setup-bin@v3
with:
uri: 'https://github.com/streamingfast/substreams/releases/download/v1.1.14/substreams_linux_x86_64.tar.gz'
uri: 'https://github.com/streamingfast/substreams/releases/download/v1.10.7/substreams_linux_x86_64.tar.gz'
name: 'substreams'
version: '1.1.14'
version: '1.10.7'

- run: make
- uses: softprops/action-gh-release@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dev/
buf.gen.yaml
*.db
data/
state.yaml

### Substreams template
.envrc
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ maintenance = { status = "actively-developed" }
crate-type = ["cdylib"]

[dependencies]
substreams = "0.5"
substreams-entity-change = "1.3"
substreams = "0.6"
substreams-entity-change = "2"
13 changes: 4 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ graph:
info:
substreams info

.PHONY: run
run:
substreams run -e eth.substreams.pinax.network:443 map_clock -s -100 --production-mode

.PHONY: gui
gui:
substreams gui -e eth.substreams.pinax.network:443 map_clock -s 1 -t 100000 --production-mode
substreams gui substreams.yaml -e eth.substreams.pinax.network:443 map_clock -s 0 -t 0 --production-mode

.PHONY: deploy
deploy:
graph build
graph deploy --studio clock
.PHONY: cache
cache:
substreams-sink-noop eth.substreams.pinax.network:443 substreams.yaml map_clock 0:
82 changes: 31 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
![Version](https://img.shields.io/github/v/release/pinax-network/substreams-clock)
![License](https://img.shields.io/github/license/pinax-network/substreams-clock)

> Block ID, Number & Timestamp
> Emits `Clock` map modules at set intervals.
> `hour`, `day`, `month`, `year` are supported.
## Quickstart

```
$ gh repo clone pinax-network/substreams-clock
$ cd substreams-clock
$ make
$ make gui
gh repo clone pinax-network/substreams-clock
cd substreams-clock
make
make gui
```

## Releases `.spkg`

- https://github.com/pinax-network/substreams-clock/releases
- <https://github.com/pinax-network/substreams-clock/releases>

## Mermaid graph

```mermaid
graph TD;
graph_out[map: graph_out];
sf.substreams.v1.Clock[source: sf.substreams.v1.Clock] --> graph_out;
store_clock[store: store_clock];
sf.substreams.v1.Clock[source: sf.substreams.v1.Clock] --> store_clock;
map_clock[map: map_clock];
map_clock:params[params] --> map_clock;
sf.substreams.v1.Clock[source: sf.substreams.v1.Clock] --> map_clock;
store_clock -- deltas --> map_clock;
```

## Map Outputs
Expand All @@ -33,54 +38,29 @@ graph TD;

```json
{
"entityChanges": [
{
"entity": "Clock",
"id": "542d7e9a07a94d93879a51452d6f077a461d311018f0b03377b2d877bb72cdf0",
"ordinal": "0",
"operation": "OPERATION_CREATE",
"fields": [
{
"name": "number",
"newValue": {
"bigint": "18221781"
}
},
{
"name": "seconds",
"newValue": {
"bigint": "1695753443"
}
},
{
"name": "nanos",
"newValue": {
"bigint": "0"
}
},
{
"name": "timestamp",
"newValue": {
"string": "2023-09-26T18:37:23Z"
}
}
]
}
]
"id": "ab79f822909750f88dfb9dd0350c1ebe98d5495e9c969cdeb6e0ac993b80175b",
"number": "6912",
"timestamp": "2015-07-31T00:00:01Z"
}
```

### Modules

```yaml
Package name: clock
Version: v0.1.0
Doc: Block ID, number & timestamp
Modules:
----
Name: graph_out
Name: store_clock
Initial block: 0
Kind: store
Input: source: sf.substreams.v1.Clock
Value Type: string
Update Policy: set
Hash: 2810ecc8f812533b3d7d272392793a6590d863c0

Name: map_clock
Initial block: 0
Kind: map
Output Type: proto:sf.substreams.sink.entity.v1.EntityChanges
Hash: d2913e1f3c4966817a7cdbbd49db407a1a81ebc9
```
Input: params: day
Input: source: sf.substreams.v1.Clock
Input: store: store_clock
Output Type: proto:sf.substreams.v1.Clock
Hash: 1723a789b027af657c12fe9c37eefafd56595078
```
6 changes: 3 additions & 3 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.65.0"
components = [ "rustfmt" ]
targets = [ "wasm32-unknown-unknown" ]
channel = "1.76.0"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
7 changes: 0 additions & 7 deletions schema.graphql

This file was deleted.

6 changes: 0 additions & 6 deletions schema.sql

This file was deleted.

14 changes: 0 additions & 14 deletions src/index.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod maps;
mod index;
mod stores;
mod stores;
18 changes: 15 additions & 3 deletions src/maps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use substreams::errors::Error;
use substreams::pb::substreams::Clock;
use substreams::store::{DeltaString, Deltas};

#[substreams::handlers::map]
pub fn map_clock(clock: Clock) -> Result<Clock, Error> {
Ok(clock)
}
pub fn map_clock(params: String, clock: Clock, store: Deltas<DeltaString>) -> Result<Clock, Error> {
for delta in store.deltas {
if !params.is_empty() && delta.key != params {
continue;
}
if delta.old_value == "" {
continue; // skips genesis block
}
if delta.old_value != delta.new_value {
return Ok(clock);
}
}
Ok(Clock::default()) // empty clock, block will be skipped
}
51 changes: 46 additions & 5 deletions src/stores.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
use substreams::pb::substreams::Clock;
use substreams::store::{StoreMin, StoreMinInt64, StoreNew};
use substreams::store::{StoreNew, StoreSet, StoreSetString};

#[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);
}
pub fn store_clock(clock: Clock, store: StoreSetString) {
if clock.number == 0 {
return;
}
let timestamp = clock.timestamp.as_ref().expect("missing timestamp");
let block_date = to_date(&clock);
let hour = timestamp.seconds / 3600;
store.set(0, "hour", &hour.to_string());
store.set(0, "day", &block_date);
store.set(0, "month", &to_month(&block_date));
store.set(0, "year", &to_year(&block_date));
}

// Clock to date string
// ex: Clock => 2015-07-30
pub fn to_date(clock: &Clock) -> String {
let timestamp = clock.timestamp.as_ref().expect("missing timestamp");
timestamp
.to_string()
.split('T')
.next()
.expect("missing date")
.to_string()
}

// Timestamp to date conversion
// ex: 2015-07-30 => 2015-07
pub fn to_month(block_date: &str) -> String {
match block_date
.split('-')
.take(2)
.collect::<Vec<&str>>()
.join("-")
.as_str()
{
date => date.to_string(),
}
}

pub fn to_year(block_date: &str) -> String {
match block_date.split('-').next() {
Some(date) => date.to_string(),
None => "".to_string(),
}
}
27 changes: 10 additions & 17 deletions substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package:
name: clock
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.2/substreams-sink-entity-changes-v1.3.2.spkg
Expand All @@ -14,29 +13,23 @@ binaries:
file: ./target/wasm32-unknown-unknown/release/clock.wasm

modules:
- name: store_min_block
- name: store_clock
kind: store
updatePolicy: min
valueType: int64
updatePolicy: set
valueType: string
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: map_clock
kind: map
inputs:
- params: string
- source: sf.substreams.v1.Clock
blockFilter:
module: block_index
query:
string: daily
params: false
- store: store_clock
mode: deltas
output:
type: proto:sf.substreams.v1.Clock

params:
# options: "", hour, day, month, year
map_clock: day

0 comments on commit 0c8276d

Please sign in to comment.