From 567eb94054b84fb230fe76191c55da358e45bc6e Mon Sep 17 00:00:00 2001 From: zolting Date: Mon, 18 Nov 2024 17:31:45 -0500 Subject: [PATCH] Add AccountRamDeltas + CreationTrees --- blocks/antelope/src/account_ram_deltas.rs | 34 +++++++++++++++++++++++ blocks/antelope/src/creation_tree.rs | 28 +++++++++++++++++++ blocks/antelope/src/sinks.rs | 9 +++--- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/blocks/antelope/src/account_ram_deltas.rs b/blocks/antelope/src/account_ram_deltas.rs index 048e924..fa7f474 100644 --- a/blocks/antelope/src/account_ram_deltas.rs +++ b/blocks/antelope/src/account_ram_deltas.rs @@ -5,7 +5,11 @@ use substreams_antelope::pb::{ActionTrace, TransactionTrace}; use substreams_database_change::pb::database::{table_change, DatabaseChanges}; use crate::keys::account_ram_delta_keys; +use crate::pb::antelope::AccountRamDelta as RawAccountRamDelta; use crate::transactions::insert_transaction_metadata; +use crate::transactions::is_transaction_success; +use common::structs::BlockTimestamp; +use substreams_antelope::Block; pub fn insert_account_ram_delta(tables: &mut DatabaseChanges, clock: &Clock, action: &ActionTrace, transaction: &TransactionTrace, account_ram_delta: &AccountRamDelta, index: &u32) { // transaction @@ -30,3 +34,33 @@ pub fn insert_account_ram_delta(tables: &mut DatabaseChanges, clock: &Clock, act insert_transaction_metadata(row, transaction); insert_timestamp(row, clock, false, false); } + +pub fn collect_account_ram_deltas(block: &Block, timestamp: &BlockTimestamp) -> Vec { + let mut account_ram_deltas: Vec = vec![]; + + for transaction in block.transaction_traces() { + let tx_hash = &transaction.id; + let tx_success = is_transaction_success(transaction.receipt.clone().unwrap_or_default().status); + + for action_trace in transaction.action_traces.iter() { + let action_index = action_trace.execution_index; + + for (index, delta) in action_trace.account_ram_deltas.iter().enumerate() { + account_ram_deltas.push(RawAccountRamDelta { + block_time: Some(timestamp.time.clone()), + block_number: timestamp.number, + block_hash: timestamp.hash.clone(), + block_date: timestamp.date.clone(), + tx_hash: tx_hash.clone(), + tx_success, + action_index, + index: index as u32, + account: delta.account.clone(), + delta: delta.delta, + }); + } + } + } + + account_ram_deltas +} diff --git a/blocks/antelope/src/creation_tree.rs b/blocks/antelope/src/creation_tree.rs index a31561b..e9f7170 100644 --- a/blocks/antelope/src/creation_tree.rs +++ b/blocks/antelope/src/creation_tree.rs @@ -3,7 +3,11 @@ use substreams::pb::substreams::Clock; use substreams_antelope::pb::{CreationFlatNode, TransactionTrace}; use substreams_database_change::pb::database::{table_change, DatabaseChanges}; +use crate::pb::antelope::CreationTree as RawCreationTree; +use crate::transactions::is_transaction_success; use crate::{keys::creation_tree_keys, transactions::insert_transaction_metadata}; +use common::structs::BlockTimestamp; +use substreams_antelope::Block; pub fn insert_creation_tree(tables: &mut DatabaseChanges, clock: &Clock, transaction: &TransactionTrace, creation_flat_node: &CreationFlatNode) { let creator_action_index = creation_flat_node.creator_action_index; @@ -17,3 +21,27 @@ pub fn insert_creation_tree(tables: &mut DatabaseChanges, clock: &Clock, transac insert_transaction_metadata(row, transaction); insert_timestamp(row, clock, false, false); } + +pub fn collect_creation_trees(block: &Block, timestamp: &BlockTimestamp) -> Vec { + let mut creation_trees: Vec = vec![]; + + for transaction in block.transaction_traces() { + let tx_hash = &transaction.id; + let tx_success = is_transaction_success(transaction.receipt.clone().unwrap_or_default().status); + + for creation_flat_node in transaction.creation_tree.iter() { + creation_trees.push(RawCreationTree { + block_time: Some(timestamp.time.clone()), + block_number: timestamp.number, + block_hash: timestamp.hash.clone(), + block_date: timestamp.date.clone(), + tx_hash: tx_hash.clone(), + tx_success, + creator_action_index: creation_flat_node.creator_action_index, + execution_action_index: creation_flat_node.execution_action_index, + }); + } + } + + creation_trees +} diff --git a/blocks/antelope/src/sinks.rs b/blocks/antelope/src/sinks.rs index f36519b..aab5a5f 100644 --- a/blocks/antelope/src/sinks.rs +++ b/blocks/antelope/src/sinks.rs @@ -4,8 +4,9 @@ use substreams::pb::substreams::Clock; use substreams_antelope::pb::Block; use crate::{ - actions::collect_actions, auth_sequences::collect_auth_sequences, authority::collect_authority_vectors, authorizations::collect_authorizations, blocks::collect_block, db_ops::collect_db_ops, - feature_ops::collect_feature_ops, pb::antelope::Events, perm_ops::collect_perm_ops, ram_ops::collect_ram_ops, table_ops::collect_table_ops, transactions::collect_transactions, + account_ram_deltas::collect_account_ram_deltas, actions::collect_actions, auth_sequences::collect_auth_sequences, authority::collect_authority_vectors, authorizations::collect_authorizations, + blocks::collect_block, creation_tree::collect_creation_trees, db_ops::collect_db_ops, feature_ops::collect_feature_ops, pb::antelope::Events, perm_ops::collect_perm_ops, ram_ops::collect_ram_ops, + table_ops::collect_table_ops, transactions::collect_transactions, }; #[substreams::handlers::map] @@ -27,7 +28,7 @@ pub fn map_events(clock: Clock, block: Block) -> Result { ram_ops: collect_ram_ops(&block, ×tamp), authorizations: collect_authorizations(&block, ×tamp), auth_sequences: collect_auth_sequences(&block, ×tamp), - account_ram_deltas: vec![], - creation_trees: vec![], + account_ram_deltas: collect_account_ram_deltas(&block, ×tamp), + creation_trees: collect_creation_trees(&block, ×tamp), }) }