Skip to content

Commit

Permalink
indexer: add lib.rs to allow using crate in another binary
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Apr 19, 2024
1 parent 2e3e9dd commit 5191f67
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
1 change: 0 additions & 1 deletion apps/indexer/src/ethereum_indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ where
xchain_txor_config: XchainTxConfig,
) -> Result<EthereumIndexer<T>> {
let client = EthereumClient::new(config.clone()).await?;
/// TODO: should we add moralis api key to configuration file?
let pricer = MoralisPrice::new(None);
Ok(EthereumIndexer {
client,
Expand Down
13 changes: 13 additions & 0 deletions apps/indexer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub mod config;
pub mod ethereum_indexer;
pub mod handlers;
pub mod price;
pub mod starknet_indexer;
pub mod storage;
pub mod utils;

#[derive(Clone, Debug)]
pub struct ChainsBlocks {
pub sn: u64,
pub eth: u64,
}
39 changes: 7 additions & 32 deletions apps/indexer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
//! Starklane indexer main entry point.
extern crate config as external_crate_config;

use crate::config::StarklaneIndexerConfig;
use starklane_indexer::config::StarklaneIndexerConfig;
use anyhow::Result;
use axum::{http::Request, middleware::Next, response::Response, routing::get, Router, Server};
use clap::Parser;
use ethereum_indexer::EthereumIndexer;
use handlers::{requests, AppState};
use starknet_indexer::StarknetIndexer;
use starklane_indexer::ethereum_indexer::EthereumIndexer;
use starklane_indexer::handlers::{requests, AppState};
use starklane_indexer::starknet_indexer::StarknetIndexer;
use std::sync::Arc;
use storage::mongo::MongoStore;
use starklane_indexer::storage::{extract_database_name, mongo::MongoStore};
use starklane_indexer::ChainsBlocks;
use tokio::sync::RwLock as AsyncRwLock;

pub mod config;
pub mod ethereum_indexer;
pub mod handlers;
pub mod price;
pub mod starknet_indexer;
pub mod storage;
pub mod utils;


const ENV_PREFIX: &'static str = "INDEXER";
const ENV_SEPARATOR: &'static str = "__"; // "_" can't be used since we have key with '_' in json
Expand All @@ -36,11 +31,6 @@ struct Args {
api_server_ip: Option<String>,
}

#[derive(Clone, Debug)]
pub struct ChainsBlocks {
sn: u64,
eth: u64,
}

async fn version_header<B>(req: Request<B>, next: Next<B>) -> Response {
let mut response = next.run(req).await;
Expand Down Expand Up @@ -153,18 +143,3 @@ async fn main() -> Result<()> {
Ok(())
}

/// Extracts database name from connection string.
/// Expecting the database name to be the latest fragment
/// of the string after the right most '/'.
fn extract_database_name(connection_string: &str) -> Option<&str> {
if let Some(pos) = connection_string.rfind('/') {
let db_name_start = pos + 1;
if let Some(pos) = connection_string[db_name_start..].find('?') {
Some(&connection_string[db_name_start..db_name_start + pos])
} else {
Some(&connection_string[db_name_start..])
}
} else {
None
}
}
17 changes: 17 additions & 0 deletions apps/indexer/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,20 @@ pub struct PendingWithdraw {

pub message_hash: [u8; 32],
}


/// Extracts database name from connection string.
/// Expecting the database name to be the latest fragment
/// of the string after the right most '/'.
pub fn extract_database_name(connection_string: &str) -> Option<&str> {
if let Some(pos) = connection_string.rfind('/') {
let db_name_start = pos + 1;
if let Some(pos) = connection_string[db_name_start..].find('?') {
Some(&connection_string[db_name_start..db_name_start + pos])
} else {
Some(&connection_string[db_name_start..])
}
} else {
None
}
}

0 comments on commit 5191f67

Please sign in to comment.