Skip to content

Commit

Permalink
Merge pull request #4 from blocktorch-xyz/block-1295-generalize-for-o…
Browse files Browse the repository at this point in the history
…ther-chains

Generalise for other chains
  • Loading branch information
catalyst17 authored Nov 30, 2023
2 parents 9c0e326 + 65e4eab commit bb9c9ea
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
CHAIN_NAME=
CHRONICLE_ADDRESSES=
AA_ERC4337_ADDRESSES=
AA_SAFE_ADDRESSES=

ENDPOINT=
SUBSTREAMS_API_TOKEN=

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ build:

.PHONY: run
run: build
substreams run -e $(ENDPOINT) substreams.yaml db_out -s $(START_BLOCK) -t $(STOP_BLOCK)
substreams run -e $(ENDPOINT) substreams.yaml map_combine_transactions -s $(START_BLOCK) -t $(STOP_BLOCK)

.PHONY: sink
sink: build
Expand Down
11 changes: 11 additions & 0 deletions proto/combined_transactions.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package eth.transaction.v1;

import "aa_transaction.proto";
import "chronicle_transaction.proto";

message CombinedTransactions {
repeated AccountAbstractionTransaction accountAbstractionTransactions = 1;
repeated ChronicleTransaction chronicleTransactions = 2;
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod map_filter_chronicle_transactions;
mod map_filter_aa_transactions;
mod map_combine_transactions;
mod db_out;
mod pb;
mod abi;
Expand Down
9 changes: 9 additions & 0 deletions src/map_combine_transactions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::pb::eth::transaction::v1::{CombinedTransactions, AccountAbstractionTransactions, ChronicleTransactions};

#[substreams::handlers::map]
fn map_combine_transactions(
aa_trxs: AccountAbstractionTransactions,
chronicle_trxs: ChronicleTransactions
) -> Result<CombinedTransactions, Vec<substreams::errors::Error>> {
Ok(CombinedTransactions { account_abstraction_transactions: aa_trxs.transactions, chronicle_transactions: chronicle_trxs.transactions })
}
35 changes: 23 additions & 12 deletions src/map_filter_aa_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use substreams_ethereum::{Function, Event};
use substreams_ethereum::block_view::{CallView, LogView};
use substreams_ethereum::pb::eth::v2::{Block, TransactionTrace, CallType};

// #[derive(Deserialize)]
struct TransactionFilters {
filters: Vec<TransactionFilter>
}
Expand All @@ -17,7 +16,17 @@ struct TransactionFilter {
#[substreams::handlers::map]
fn map_filter_aa_transactions(blk: Block) -> Result<Transactions, Vec<substreams::errors::Error>> {
// let filters = parse_filters_from_params(params)?;
let filters = compose_filters();
let chain_name = option_env!("CHAIN_NAME").ok_or_else(|| {
vec![substreams::errors::Error::msg("CHAIN_NAME environment variable is not set")]
})?;
let erc4337_addresses_str = option_env!("AA_ERC4337_ADDRESSES").ok_or_else(|| {
vec![substreams::errors::Error::msg("AA_ERC4337_ADDRESSES environment variable is not set")]
})?;
let safe_addresses_str = option_env!("AA_SAFE_ADDRESSES").ok_or_else(|| {
vec![substreams::errors::Error::msg("AA_SAFE_ADDRESSES environment variable is not set")]
})?;

let filters = compose_filters(&erc4337_addresses_str, &safe_addresses_str);
let header = blk.header.unwrap();

let transactions: Vec<Transaction> = blk
Expand All @@ -29,7 +38,7 @@ fn map_filter_aa_transactions(blk: Block) -> Result<Transactions, Vec<substreams
from: Hex::encode(&trans.from),
to: Hex::encode(&trans.to),
hash: Hex::encode(&trans.hash),
chain: "ethereum".to_owned(),
chain: chain_name.to_owned(),
account_abstraction_type: aa_trans_type.unwrap(),
status: trans.status().as_str_name().to_owned(),
timestamp: Some(header.timestamp.as_ref().unwrap().clone())
Expand All @@ -43,20 +52,22 @@ fn map_filter_aa_transactions(blk: Block) -> Result<Transactions, Vec<substreams
Ok(Transactions { transactions })
}

fn compose_filters() -> TransactionFilters {
fn compose_filters(erc4337_addresses_str: &str, safe_addresses_str: &str) -> TransactionFilters {


let erc4337_filter = TransactionFilter {
to: vec!["0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789".to_string()],
to: erc4337_addresses_str
.split(',')
.map(|s| s.to_lowercase())
.collect::<Vec<_>>(),
account_abstraction_type: "erc4337".to_string(),
};

let safe_filter = TransactionFilter {
to: vec![
"0xb6029EA3B2c51D09a50B53CA8012FeEB05bDa35A".to_string(), // v1.0.0
"0x34CfAC646f301356fAa8B21e94227e3583Fe3F5F".to_string(), // v1.1.1
"0x6851D6fDFAfD08c0295C392436245E5bc78B0185".to_string(), // v1.2.0
"0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552".to_string(), // v1.3.0
"0x41675C099F32341bf84BFc5382aF534df5C7461a".to_string(), // v1.4.1
],
to: safe_addresses_str
.split(',')
.map(|s| s.to_lowercase())
.collect::<Vec<_>>(),
account_abstraction_type: "safe".to_string(),
};

Expand Down
25 changes: 14 additions & 11 deletions src/map_filter_chronicle_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ struct TransactionsFilter {

#[substreams::handlers::map]
fn map_filter_chronicle_transactions(blk: Block) -> Result<Transactions, Vec<substreams::errors::Error>> {
let filter = compose_filters();
let chain_name = option_env!("CHAIN_NAME").ok_or_else(|| {
vec![substreams::errors::Error::msg("CHAIN_NAME environment variable is not set")]
})?;
let chronicle_addresses_str = option_env!("CHRONICLE_ADDRESSES").ok_or_else(|| {
vec![substreams::errors::Error::msg("CHRONICLE_ADDRESSES environment variable is not set")]
})?;

let filter = compose_filters(&chronicle_addresses_str);
let header = blk.header.unwrap();

let transactions: Vec<Transaction> = blk
Expand All @@ -21,7 +28,7 @@ fn map_filter_chronicle_transactions(blk: Block) -> Result<Transactions, Vec<sub
from: Hex::encode(&trans.from),
to: Hex::encode(&trans.to),
hash: Hex::encode(&trans.hash),
chain: "ethereum".to_owned(),
chain: chain_name.to_owned(),
status: trans.status().as_str_name().to_owned(),
timestamp: Some(header.timestamp.as_ref().unwrap().clone())
})
Expand All @@ -30,16 +37,12 @@ fn map_filter_chronicle_transactions(blk: Block) -> Result<Transactions, Vec<sub
Ok(Transactions { transactions })
}

fn compose_filters() -> TransactionsFilter {
fn compose_filters(chronicle_addresses_str: &str) -> TransactionsFilter {
let filter = TransactionsFilter {
to: vec![
"0xe0F30cb149fAADC7247E953746Be9BbBB6B5751f".to_lowercase(), // BTC / USD
"0x64de91f5a373cd4c28de3600cb34c7c6ce410c85".to_lowercase(), // ETH / USD
"0x31bfa908637c29707e155cfac3a50c9823bf8723".to_lowercase(), // GNO / USD
"0xdbbe5e9b1daa91430cf0772fcebe53f6c6f137df".to_lowercase(), // MKR / USD
"0xf86360f0127f8a441cfca332c75992d1c692b3d1".to_lowercase(), // RETH / USD
"0x2f73b6567b866302e132273f67661fb89b5a66f2".to_lowercase(), // WSTETH / USD
]
to: chronicle_addresses_str
.split(',')
.map(|s| s.to_lowercase())
.collect::<Vec<_>>()
};

return filter;
Expand Down
8 changes: 8 additions & 0 deletions src/pb/eth.transaction.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ pub struct AccountAbstractionTransaction {
#[prost(message, optional, tag="7")]
pub timestamp: ::core::option::Option<::prost_types::Timestamp>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CombinedTransactions {
#[prost(message, repeated, tag="1")]
pub account_abstraction_transactions: ::prost::alloc::vec::Vec<AccountAbstractionTransaction>,
#[prost(message, repeated, tag="2")]
pub chronicle_transactions: ::prost::alloc::vec::Vec<ChronicleTransaction>,
}
// @@protoc_insertion_point(module)
12 changes: 9 additions & 3 deletions substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protobuf:
files:
- chronicle_transaction.proto
- aa_transaction.proto
- combined_transactions.proto
importPaths:
- ./proto

Expand All @@ -23,23 +24,28 @@ binaries:
modules:
- name: map_filter_aa_transactions
kind: map
initialBlock: 18664500
inputs:
- source: sf.ethereum.type.v2.Block
output:
type: proto:eth.transaction.v1.AccountAbstractionTransactions

- name: map_filter_chronicle_transactions
kind: map
initialBlock: 18664500
inputs:
- source: sf.ethereum.type.v2.Block
output:
type: proto:eth.transaction.v1.ChronicleTransactions

- name: map_combine_transactions
kind: map
inputs:
- map: map_filter_aa_transactions
- map: map_filter_chronicle_transactions
output:
type: proto:eth.transaction.v1.CombinedTransactions

- name: db_out
kind: map
initialBlock: 18664500
inputs:
- map: map_filter_aa_transactions
- map: map_filter_chronicle_transactions
Expand Down

0 comments on commit bb9c9ea

Please sign in to comment.