remove fallback to env values for watched addresses

This commit is contained in:
Jędrzej Stuczyński
2025-03-10 11:33:14 +00:00
parent 1a334b575d
commit d7ef68d8d1
7 changed files with 41 additions and 40 deletions
+1 -1
View File
@@ -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?;
@@ -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 {
+3 -2
View File
@@ -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<String>,
pub payment_watcher_config: Option<PaymentWatcherConfig>,
#[serde(default)]
pub payment_watcher_config: PaymentWatcherConfig,
#[serde(default)]
pub logging: LoggingSettings,
@@ -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<PaymentWatcherEntry>,
}
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,
+3 -29
View File
@@ -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<AppState> {
Router::new().route("/addresses", axum::routing::get(get_addresses))
@@ -19,30 +17,6 @@ pub(crate) fn routes() -> Router<AppState> {
)]
/// Fetch the addresses being watched by the chain watcher
async fn get_addresses() -> HttpResult<Json<Vec<String>>> {
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::<Vec<_>>()
})
})
})
.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<AppState>) -> HttpResult<Json<Vec<String>>> {
Ok(Json(state.watched_addresses.clone()))
}
+14 -2
View File
@@ -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<HttpServer> {
pub(crate) async fn build_http_api(
db_pool: DbPool,
config: &Config,
http_port: u16,
) -> anyhow::Result<HttpServer> {
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);
+6 -2
View File
@@ -3,11 +3,15 @@ use crate::db::DbPool;
#[derive(Debug, Clone)]
pub(crate) struct AppState {
db_pool: DbPool,
pub(crate) watched_addresses: Vec<String>,
}
impl AppState {
pub(crate) fn new(db_pool: DbPool) -> Self {
Self { db_pool }
pub(crate) fn new(db_pool: DbPool, watched_addresses: Vec<String>) -> Self {
Self {
db_pool,
watched_addresses,
}
}
pub(crate) fn db_pool(&self) -> &DbPool {