Skip to content

Commit

Permalink
refactor insert transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Aug 1, 2024
1 parent 87cce63 commit f522b92
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
- [ ] TON
- **EVM**
- [x] Ethereum
- [x] Arbitrum
- [x] BNB
- [x] Polygon
- [ ] Fantom
Expand All @@ -45,7 +44,10 @@
- [ ] [Injective](https://injective.com/) (L2 on Cosmos)
- [ ] [Rootstock](https://rootstock.io/) (L2 on Bitcoin)
- [ ] [IoTex](https://iotex.io/)
- [ ] [Celo](https://celo.org/) (L2 on ETH)
- **Optimistic Rollup (EVM)**
- [x] Arbitrum One (L2 on ETH)
- [ ] Arbitrum Nova (L2 on ETH using AnyTrust protocol)
- [ ] Optimism (OP)
- [ ] opBNB
- [ ] Blast
Expand All @@ -66,6 +68,7 @@
- [ ] Scroll
- [ ] Linea
- [ ] Fuse
- [ ] [Polygon zkEVM](https://polygon.technology/polygon-zkevm)
- [ ] [Astar zkEVM](https://astar.network/) (L2)
- **Polkadot's Parachain**
- [ ] Moonbeam
Expand Down
22 changes: 21 additions & 1 deletion blocks/evm/src/balance_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use common::blocks::insert_timestamp;
use common::utils::bytes_to_hex;
use common::{keys::balance_changes_keys, utils::optional_bigint_to_string};
use substreams::pb::substreams::Clock;
use substreams_database_change::pb::database::{table_change, DatabaseChanges};
use substreams_database_change::pb::database::{table_change, DatabaseChanges, TableChange};
use substreams_ethereum::pb::eth::v2::BalanceChange;

pub fn balance_change_reason_to_string(reason: i32) -> String {
Expand Down Expand Up @@ -33,6 +33,13 @@ pub fn balance_change_reason_to_string(reason: i32) -> String {
}
}

// Block balance changes (ex: RewardMineBlock, RewardMineUncle, Withdraw, Burn)
pub fn insert_balance_changes(tables: &mut DatabaseChanges, clock: &Clock, balance_changes: &Vec<BalanceChange>) {
for balance_change in balance_changes {
insert_balance_change(tables, &clock, &balance_change);
}
}

// https://github.com/streamingfast/firehose-ethereum/blob/1bcb32a8eb3e43347972b6b5c9b1fcc4a08c751e/proto/sf/ethereum/type/v2/type.proto#L658
pub fn insert_balance_change(tables: &mut DatabaseChanges, clock: &Clock, balance_change: &BalanceChange) {
let address = bytes_to_hex(balance_change.address.clone());
Expand All @@ -53,3 +60,16 @@ pub fn insert_balance_change(tables: &mut DatabaseChanges, clock: &Clock, balanc

insert_timestamp(row, clock, false);
}

pub fn insert_balance_change_counts(row: &mut TableChange, all_balance_changes_reason: Vec<i32>) {
// transaction counts
let total_balance_changes = all_balance_changes_reason.len();
let mut total_withdrawals = 0;
for reason in all_balance_changes_reason {
if reason == 16 {
total_withdrawals += 1;
}
}
row.change("total_balance_changes", ("", total_balance_changes.to_string().as_str()))
.change("total_withdrawals", ("", total_withdrawals.to_string().as_str()));
}
13 changes: 11 additions & 2 deletions blocks/evm/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use substreams::pb::substreams::Clock;
use substreams_database_change::pb::database::{table_change, DatabaseChanges};
use substreams_ethereum::pb::eth::v2::Block;

use crate::balance_changes::insert_balance_change_counts;

// https://github.com/streamingfast/firehose-ethereum/blob/develop/proto/sf/ethereum/type/v2/type.proto
pub fn insert_blocks(tables: &mut DatabaseChanges, clock: &Clock, block: &Block) {
let header = block.header.clone().unwrap_or_default();
Expand All @@ -15,7 +17,7 @@ pub fn insert_blocks(tables: &mut DatabaseChanges, clock: &Clock, block: &Block)
let transactions_root = bytes_to_hex(header.transactions_root);
let state_root = bytes_to_hex(header.state_root);
let receipts_root = bytes_to_hex(header.receipt_root);
let miner = bytes_to_hex(header.coinbase);
let miner = bytes_to_hex(header.coinbase); // EVM Address
let size = block.size;
let mix_hash = bytes_to_hex(header.mix_hash);
let extra_data = bytes_to_hex(header.extra_data);
Expand Down Expand Up @@ -59,5 +61,12 @@ pub fn insert_blocks(tables: &mut DatabaseChanges, clock: &Clock, block: &Block)
.change("blob_gas_used", ("", blob_gas_used.to_string().as_str()));

insert_timestamp(row, clock, true);
insert_transaction_counts(row, &block.transaction_traces);

// transaction status counts
let all_transaction_status: Vec<i32> = block.transaction_traces.iter().map(|transaction| transaction.status).collect();
insert_transaction_counts(row, all_transaction_status);

// balance changes counts
let all_balance_changes_reason: Vec<i32> = block.balance_changes.iter().map(|balance_change| balance_change.reason).collect();
insert_balance_change_counts(row, all_balance_changes_reason);
}
6 changes: 3 additions & 3 deletions blocks/evm/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub fn insert_log(tables: &mut DatabaseChanges, clock: &Clock, log: &Log, transa
let index = log.index;
let tx_hash = bytes_to_hex(transaction.hash.to_vec());
let tx_index = transaction.index;
let tx_from = bytes_to_hex(transaction.from.to_vec());
let tx_to = bytes_to_hex(transaction.to.to_vec());
let contract_address = bytes_to_hex(log.address.to_vec());
let tx_from = bytes_to_hex(transaction.from.to_vec()); // EVM Address
let tx_to = bytes_to_hex(transaction.to.to_vec()); // EVM Address
let contract_address = bytes_to_hex(log.address.to_vec()); // EVM Address
let topics = log.topics.clone();
let topic0 = extract_topic(&topics, 0);
let topic1 = extract_topic(&topics, 1);
Expand Down
24 changes: 14 additions & 10 deletions blocks/evm/src/sinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ use substreams::pb::substreams::Clock;
use substreams_database_change::pb::database::DatabaseChanges;
use substreams_ethereum::pb::eth::v2::Block;

use crate::balance_changes::insert_balance_change;
use crate::balance_changes::insert_balance_changes;
use crate::blocks::insert_blocks;
use crate::transactions::insert_transaction;
use crate::transactions::insert_transactions;

#[substreams::handlers::map]
pub fn ch_out(clock: Clock, block: Block) -> Result<DatabaseChanges, Error> {
let mut tables: DatabaseChanges = DatabaseChanges::default();
// blocks
insert_blocks(&mut tables, &clock, &block);
for balance_change in block.balance_changes.clone() {
insert_balance_change(&mut tables, &clock, &balance_change);
}
// transactions
for transaction in block.transaction_traces.iter() {
insert_transaction(&mut tables, &clock, &transaction);
}
insert_balance_changes(&mut tables, &clock, &block.balance_changes);
insert_transactions(&mut tables, &clock, &block.transaction_traces);

Ok(tables)
}

// TO-DO: Implement the `graph_out` function using EntityChanges
#[substreams::handlers::map]
pub fn graph_out(clock: Clock, block: Block) -> Result<DatabaseChanges, Error> {
let mut tables: DatabaseChanges = DatabaseChanges::default();
insert_blocks(&mut tables, &clock, &block);
// TO-DO: Convert DatabaseChanges to EntityChanges
Ok(tables)
}
6 changes: 6 additions & 0 deletions blocks/evm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ pub fn is_transaction_success(status: i32) -> bool {
status == 1
}

pub fn insert_transactions(tables: &mut DatabaseChanges, clock: &Clock, transactions: &Vec<TransactionTrace>) {
for transaction in transactions {
insert_transaction(tables, clock, &transaction);
}
}

// https://github.com/streamingfast/firehose-ethereum/blob/1bcb32a8eb3e43347972b6b5c9b1fcc4a08c751e/proto/sf/ethereum/type/v2/type.proto#L658
pub fn insert_transaction(tables: &mut DatabaseChanges, clock: &Clock, transaction: &TransactionTrace) {
let index = transaction.index;
Expand Down
8 changes: 3 additions & 5 deletions common/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use substreams::pb::substreams::Clock;
use substreams_database_change::pb::database::TableChange;
use substreams_ethereum::pb::eth::v2::TransactionTrace;

use crate::utils::block_time_to_date;

Expand All @@ -18,14 +17,13 @@ pub fn insert_timestamp(row: &mut TableChange, clock: &Clock, is_block: bool) {
.change(format!("{}hash", prefix).as_str(), ("", block_hash.as_str()));
}

// TO-DO: rewrite using only `all_transaction_status: &Vec<i32>`
pub fn insert_transaction_counts(row: &mut TableChange, transaction_traces: &Vec<TransactionTrace>) {
pub fn insert_transaction_counts(row: &mut TableChange, all_transaction_status: Vec<i32>) {
// transaction counts
let mut total_transactions = 0;
let mut successful_transactions = 0;
let mut failed_transactions = 0;
for traces in transaction_traces.iter() {
if traces.status == 1 {
for status in all_transaction_status {
if status == 1 {
successful_transactions += 1;
} else {
failed_transactions += 1;
Expand Down

0 comments on commit f522b92

Please sign in to comment.