Skip to content

Commit

Permalink
feat(torii): index whitelisted erc20/erc721
Browse files Browse the repository at this point in the history
commit-id:5231a946
  • Loading branch information
lambda-0x committed Sep 18, 2024
1 parent 8613c78 commit d53a2e4
Show file tree
Hide file tree
Showing 21 changed files with 928 additions and 94 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 76 additions & 9 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use std::cmp;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -29,6 +30,9 @@ use tokio::sync::broadcast;
use tokio::sync::broadcast::Sender;
use tokio_stream::StreamExt;
use torii_core::engine::{Engine, EngineConfig, Processors};
use torii_core::processors::erc20_legacy_transfer::Erc20LegacyTransferProcessor;
use torii_core::processors::erc20_transfer::Erc20TransferProcessor;
use torii_core::processors::erc721_transfer::Erc721TransferProcessor;
use torii_core::processors::event_message::EventMessageProcessor;
use torii_core::processors::generate_event_processors_map;
use torii_core::processors::metadata_update::MetadataUpdateProcessor;
Expand All @@ -40,7 +44,7 @@ use torii_core::processors::store_update_member::StoreUpdateMemberProcessor;
use torii_core::processors::store_update_record::StoreUpdateRecordProcessor;
use torii_core::simple_broker::SimpleBroker;
use torii_core::sql::Sql;
use torii_core::types::Model;
use torii_core::types::{ErcContract, ErcType, Model, ToriiConfig};
use torii_server::proxy::Proxy;
use tracing::{error, info};
use tracing_subscriber::{fmt, EnvFilter};
Expand Down Expand Up @@ -130,11 +134,38 @@ struct Args {
/// Max concurrent tasks
#[arg(long, default_value = "100")]
max_concurrent_tasks: usize,

/// ERC contract addresses to index
#[arg(long, value_parser = parse_erc_contracts)]
#[arg(conflicts_with = "config")]
erc_contracts: Option<std::vec::Vec<ErcContract>>,

/// Configuration file
#[arg(long)]
config: Option<PathBuf>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();

let mut start_block = args.start_block;

let mut config = if let Some(path) = args.config {
ToriiConfig::load_from_path(&path)?
} else {
ToriiConfig::default()
};

if let Some(erc_contracts) = args.erc_contracts {
config.erc_contracts = erc_contracts;
}

for address in &config.erc_contracts {
if address.start_block < start_block {
start_block = address.start_block;
}
}
let filter_layer = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info,hyper_reverse_proxy=off"));

Expand Down Expand Up @@ -179,17 +210,26 @@ async fn main() -> anyhow::Result<()> {
// Get world address
let world = WorldContractReader::new(args.world_address, provider.clone());

let db = Sql::new(pool.clone(), args.world_address).await?;
let erc_contracts = config
.erc_contracts
.iter()
.map(|contract| (contract.contract_address, contract.clone()))
.collect();

let db = Sql::new(pool.clone(), args.world_address, &erc_contracts).await?;

let processors = Processors {
event: generate_event_processors_map(vec![
Arc::new(RegisterModelProcessor),
Arc::new(StoreSetRecordProcessor),
Arc::new(MetadataUpdateProcessor),
Arc::new(StoreDelRecordProcessor),
Arc::new(EventMessageProcessor),
Arc::new(StoreUpdateRecordProcessor),
Arc::new(StoreUpdateMemberProcessor),
Box::new(RegisterModelProcessor),
Box::new(StoreSetRecordProcessor),
Box::new(MetadataUpdateProcessor),
Box::new(StoreDelRecordProcessor),
Box::new(EventMessageProcessor),
Box::new(StoreUpdateRecordProcessor),
Box::new(StoreUpdateMemberProcessor),
Box::new(Erc20LegacyTransferProcessor),
Box::new(Erc20TransferProcessor),
Box::new(Erc721TransferProcessor),
])?,
transaction: vec![Box::new(StoreTransactionProcessor)],
..Processors::default()
Expand All @@ -211,6 +251,7 @@ async fn main() -> anyhow::Result<()> {
},
shutdown_tx.clone(),
Some(block_tx),
erc_contracts,
);

let shutdown_rx = shutdown_tx.subscribe();
Expand Down Expand Up @@ -307,3 +348,29 @@ async fn spawn_rebuilding_graphql_server(
}
}
}

// Parses clap cli argument which is expected to be in the format:
// - erc_type:address:start_block
// - address:start_block (erc_type defaults to ERC20)
fn parse_erc_contracts(s: &str) -> anyhow::Result<Vec<ErcContract>> {
let parts: Vec<&str> = s.split(',').collect();
let mut contracts = Vec::new();
for part in parts {
match part.split(':').collect::<Vec<&str>>().as_slice() {
[r#type, address, start_block] => {
let contract_address = Felt::from_str(address).unwrap();
let start_block = start_block.parse::<u64>()?;
let r#type = r#type.parse::<ErcType>()?;
contracts.push(ErcContract { contract_address, start_block, r#type });
}
[address, start_block] => {
let contract_address = Felt::from_str(address)?;
let start_block = start_block.parse::<u64>()?;
let r#type = ErcType::default();
contracts.push(ErcContract { contract_address, start_block, r#type });
}
_ => return Err(anyhow::anyhow!("Invalid ERC contract format")),
}
}
Ok(contracts)
}
8 changes: 8 additions & 0 deletions bin/torii/torii.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration file for Torii
# erc_contracts = [
# { contract_address = "0x1234567890abcdef1234567890abcdef12345678", start_block = 0, type = "ERC20" },
# { contract_address = "0xabcdef1234567890abcdef1234567890abcdef12", start_block = 1, type = "ERC721" },
# ]
# erc_contracts = [
# { type = "ERC20", contract_address = "0x07fc13cc1f43f0b0519f84df8bf13bea4d9fd5ce2d748c3baf27bf90a565f60a", start_block = 0 },
# ]
1 change: 1 addition & 0 deletions crates/torii/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = true }
tokio-stream = "0.1.11"
tokio-util = "0.7.7"
toml.workspace = true
tracing.workspace = true

[dev-dependencies]
Expand Down
Loading

0 comments on commit d53a2e4

Please sign in to comment.