diff --git a/nyx-chain-watcher/src/chain_scraper/mod.rs b/nyx-chain-watcher/src/chain_scraper/mod.rs index 93f768a236..fd4f0c6854 100644 --- a/nyx-chain-watcher/src/chain_scraper/mod.rs +++ b/nyx-chain-watcher/src/chain_scraper/mod.rs @@ -60,7 +60,7 @@ pub(crate) async fn run_chain_scraper( }) .with_tx_module(EventScraperModule::new( db_pool, - config.payment_watcher_config.clone().unwrap_or_default(), + config.payment_watcher_config.clone(), )); let instance = scraper.build_and_start().await?; diff --git a/nyx-chain-watcher/src/cli/commands/run/mod.rs b/nyx-chain-watcher/src/cli/commands/run/mod.rs index 65f5de15d5..2e16a1788d 100644 --- a/nyx-chain-watcher/src/cli/commands/run/mod.rs +++ b/nyx-chain-watcher/src/cli/commands/run/mod.rs @@ -155,11 +155,11 @@ pub(crate) async fn execute(args: Args, http_port: u16) -> Result<(), NyxChainWa // 2. payment listener let token = cancellation_token.clone(); + let payment_watcher_config = config.payment_watcher_config.clone(); { tasks.spawn(async move { token .run_until_cancelled(async move { - let payment_watcher_config = config.payment_watcher_config.unwrap_or_default(); payment_listener::run_payment_listener( payment_watcher_config, price_scraper_pool, @@ -185,7 +185,8 @@ pub(crate) async fn execute(args: Args, http_port: u16) -> Result<(), NyxChainWa } // 4. http api - let http_server = http::server::build_http_api(storage.pool_owned(), http_port).await?; + let http_server = + http::server::build_http_api(storage.pool_owned(), &config, http_port).await?; { let token = cancellation_token.clone(); tasks.spawn(async move { diff --git a/nyx-chain-watcher/src/config/mod.rs b/nyx-chain-watcher/src/config/mod.rs index 73419a2e9e..3535acce18 100644 --- a/nyx-chain-watcher/src/config/mod.rs +++ b/nyx-chain-watcher/src/config/mod.rs @@ -92,7 +92,7 @@ impl ConfigBuilder { Config { logging: self.logging.unwrap_or_default(), save_path: Some(self.config_path), - payment_watcher_config: self.payment_watcher_config, + payment_watcher_config: self.payment_watcher_config.unwrap_or_default(), data_dir: self.data_dir, db_path: self.db_path, chain_scraper_db_path: self.chain_scraper_db_path, @@ -116,7 +116,8 @@ pub struct Config { #[serde(skip)] chain_scraper_db_path: Option, - pub payment_watcher_config: Option, + #[serde(default)] + pub payment_watcher_config: PaymentWatcherConfig, #[serde(default)] pub logging: LoggingSettings, diff --git a/nyx-chain-watcher/src/config/payments_watcher.rs b/nyx-chain-watcher/src/config/payments_watcher.rs index a335830a85..78956d5762 100644 --- a/nyx-chain-watcher/src/config/payments_watcher.rs +++ b/nyx-chain-watcher/src/config/payments_watcher.rs @@ -1,12 +1,21 @@ use nym_validator_client::nyxd::AccountId; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] +#[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct PaymentWatcherConfig { pub watchers: Vec, } +impl PaymentWatcherConfig { + pub fn watched_transfer_accounts(&self) -> Vec<&AccountId> { + self.watchers + .iter() + .filter_map(|e| e.watch_for_transfer_recipient_accounts.as_ref()) + .flat_map(|a| a) + .collect() + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PaymentWatcherEntry { pub id: String, diff --git a/nyx-chain-watcher/src/http/api/watcher.rs b/nyx-chain-watcher/src/http/api/watcher.rs index 68e6f4f91c..121c86b91c 100644 --- a/nyx-chain-watcher/src/http/api/watcher.rs +++ b/nyx-chain-watcher/src/http/api/watcher.rs @@ -1,9 +1,7 @@ -use crate::config::Config; -use crate::env; use crate::http::error::HttpResult; use crate::http::state::AppState; +use axum::extract::State; use axum::{Json, Router}; -use std::env::var; pub(crate) fn routes() -> Router { Router::new().route("/addresses", axum::routing::get(get_addresses)) @@ -19,30 +17,6 @@ pub(crate) fn routes() -> Router { )] /// Fetch the addresses being watched by the chain watcher -async fn get_addresses() -> HttpResult>> { - let addresses = match Config::read_from_toml_file_in_default_location() { - Ok(config) => config - .payment_watcher_config - .as_ref() - .and_then(|config| { - config.watchers.iter().find_map(|watcher| { - watcher - .watch_for_transfer_recipient_accounts - .as_ref() - .map(|accounts| { - accounts - .iter() - .map(|account| account.to_string()) - .collect::>() - }) - }) - }) - .unwrap_or_default(), - // If the config file doesn't exist, fall back to env variable - Err(_) => var(env::vars::NYX_CHAIN_WATCHER_WATCH_ACCOUNTS) - .map(|accounts| accounts.split(',').map(String::from).collect()) - .unwrap_or_default(), - }; - - Ok(Json(addresses)) +async fn get_addresses(State(state): State) -> HttpResult>> { + Ok(Json(state.watched_addresses.clone())) } diff --git a/nyx-chain-watcher/src/http/server.rs b/nyx-chain-watcher/src/http/server.rs index a328d86b5d..20aa7042c4 100644 --- a/nyx-chain-watcher/src/http/server.rs +++ b/nyx-chain-watcher/src/http/server.rs @@ -3,15 +3,27 @@ use core::net::SocketAddr; use tokio::net::TcpListener; use tokio_util::sync::WaitForCancellationFutureOwned; +use crate::config::Config; use crate::{ db::DbPool, http::{api::RouterBuilder, state::AppState}, }; -pub(crate) async fn build_http_api(db_pool: DbPool, http_port: u16) -> anyhow::Result { +pub(crate) async fn build_http_api( + db_pool: DbPool, + config: &Config, + http_port: u16, +) -> anyhow::Result { let router_builder = RouterBuilder::with_default_routes(); - let state = AppState::new(db_pool); + let watched_accounts = config + .payment_watcher_config + .watched_transfer_accounts() + .iter() + .map(|a| a.to_string()) + .collect(); + + let state = AppState::new(db_pool, watched_accounts); let router = router_builder.with_state(state); let bind_addr = format!("0.0.0.0:{}", http_port); diff --git a/nyx-chain-watcher/src/http/state.rs b/nyx-chain-watcher/src/http/state.rs index 3f72135cf1..99fc155b74 100644 --- a/nyx-chain-watcher/src/http/state.rs +++ b/nyx-chain-watcher/src/http/state.rs @@ -3,11 +3,15 @@ use crate::db::DbPool; #[derive(Debug, Clone)] pub(crate) struct AppState { db_pool: DbPool, + pub(crate) watched_addresses: Vec, } impl AppState { - pub(crate) fn new(db_pool: DbPool) -> Self { - Self { db_pool } + pub(crate) fn new(db_pool: DbPool, watched_addresses: Vec) -> Self { + Self { + db_pool, + watched_addresses, + } } pub(crate) fn db_pool(&self) -> &DbPool {