Skip to content

Commit

Permalink
Add Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zolting committed Nov 25, 2024
1 parent 729ef32 commit d3f22b4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 237 deletions.
1 change: 1 addition & 0 deletions blocks/starknet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ substreams-bitcoin = { workspace = true }
substreams = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
serde_json = { workspace = true }
2 changes: 1 addition & 1 deletion blocks/starknet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ protogen:

.PHONY: parquet
parquet:
substreams-sink-files run starknet.substreams.pinax.network:443 substreams.yaml map_events './out' 0:500 --encoder parquet --file-block-count 100 --development-mode
substreams-sink-files run starknet.substreams.pinax.network:443 substreams.yaml map_events './out' 800000:800500 --encoder parquet --file-block-count 100 --development-mode
10 changes: 10 additions & 0 deletions blocks/starknet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## `Starknet` Raw Blockchain Data

> Starknet
> [`sf.starknet.type.v1.Block`](https://buf.build/streamingfast/firehose-starknet/docs/main:sf.starknet.type.v1)
- [x] **Blocks**
- [] **Transactions**
- [] **Events**
- [] **Calls**

87 changes: 87 additions & 0 deletions blocks/starknet/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use common::{structs::BlockTimestamp, utils::bytes_to_hex};
use serde_json::json;
use substreams::Hex;

use crate::pb::{
pinax::starknet::v1::Block as BlockOutput,
sf::starknet::r#type::v1::{Block, StateDiff},
};

pub fn collect_block(block: &Block, timestamp: &BlockTimestamp) -> BlockOutput {
let l1_data_gas_price = block.l1_data_gas_price.as_ref().expect("L1 data gas price missing");
let l1_gas_price = block.l1_gas_price.as_ref().expect("L1 gas price missing");
let state_diff = block.state_update.as_ref().expect("State diff missing").state_diff.as_ref().expect("State diff missing");

BlockOutput {
time: Some(timestamp.time.clone()),
number: timestamp.number,
date: timestamp.date.clone(),
hash: timestamp.hash.clone(),
l1_da_mode: l1_da_mode_to_string(block.l1_da_mode),
l1_data_gas_price_in_fri: l1_data_gas_price.price_in_fri.clone(),
l1_data_gas_price_in_wei: l1_data_gas_price.price_in_wei.clone(),
l1_gas_price_in_fri: l1_gas_price.price_in_fri.clone(),
l1_gas_price_in_wei: l1_gas_price.price_in_wei.clone(),
starknet_version: block.starknet_version.clone(),
tx_count: block.transactions.len() as u32,
new_root: bytes_to_hex(&block.new_root),
parent_hash: bytes_to_hex(&block.parent_hash),
sequencer_address: bytes_to_hex(&block.sequencer_address),
state_diff: state_diff_to_string(state_diff),
}
}

fn l1_da_mode_to_string(l1_da_mode: i32) -> String {
match l1_da_mode {
0 => "Unknown".to_string(),
1 => "Calldata".to_string(),
2 => "Blob".to_string(),
_ => "Unknown".to_string(),
}
}

fn state_diff_to_string(state_diff: &StateDiff) -> String {
let json_str = json!({
"deployed_contracts": state_diff.deployed_contracts.iter().map(|contract| {
json!({
"address": bytes_to_hex(&contract.address),
"class_hash": bytes_to_hex(&contract.class_hash),
})
}).collect::<Vec<_>>(),
"storage_diffs": state_diff.storage_diffs.iter().map(|diff| {
json!({
"address": bytes_to_hex(&diff.address),
"storage_entries": diff.storage_entries.iter().map(|entry| {
json!({
"key": bytes_to_hex(&entry.key),
"value": bytes_to_hex(&entry.value),
})
}).collect::<Vec<_>>(),
})
}).collect::<Vec<_>>(),
"nonces": state_diff.nonces.iter().map(|nonce| {
json!({
"contract_address": bytes_to_hex(&nonce.contract_address),
"nonce": Hex::encode(&nonce.nonce),
})
}).collect::<Vec<_>>(),
"deprecated_declared_classes": state_diff.deprecated_declared_classes.iter().map(|class_hash| {
bytes_to_hex(class_hash)
}).collect::<Vec<_>>(),
"declared_classes": state_diff.declared_classes.iter().map(|class| {
json!({
"class_hash": bytes_to_hex(&class.class_hash),
"compiled_class_hash": bytes_to_hex(&class.compiled_class_hash),

})
}).collect::<Vec<_>>(),
"replaced_classes": state_diff.replaced_classes.iter().map(|class| {
json!({
"contract_address": bytes_to_hex(&class.contract_address),
"class_hash": bytes_to_hex(&class.class_hash),
})
}).collect::<Vec<_>>(),
});

json_str.to_string()
}
1 change: 1 addition & 0 deletions blocks/starknet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod blocks;
mod map_events;
mod pb;
20 changes: 17 additions & 3 deletions blocks/starknet/src/map_events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
use common::utils::build_timestamp_with_prefix;
use substreams::{errors::Error, pb::substreams::Clock};

use crate::pb::pinax::starknet::v1::{Block, EventsOutput};
use crate::{
blocks::collect_block,
pb::{pinax::starknet::v1::EventsOutput, sf::starknet::r#type::v1::Block},
};

#[substreams::handlers::map]
pub fn map_events(_clock: Clock, _block: Block) -> Result<EventsOutput, Error> {
Ok(EventsOutput::default())
pub fn map_events(clock: Clock, block: Block) -> Result<EventsOutput, Error> {
let timestamp = build_timestamp_with_prefix(&clock);

let mut events = EventsOutput {
blocks: vec![collect_block(&block, &timestamp)],
transactions: vec![],
access_lists: vec![],
events: vec![],
calls: vec![],
};

Ok(events)
}
232 changes: 0 additions & 232 deletions blocks/starknet/src/pb/pinax.starknet.rs

This file was deleted.

2 changes: 1 addition & 1 deletion blocks/starknet/substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ modules:
- source: sf.substreams.v1.Clock
- source: sf.starknet.type.v1.Block
output:
type: proto:pinax.starknet.v1.Events
type: proto:pinax.starknet.v1.EventsOutput

0 comments on commit d3f22b4

Please sign in to comment.