use std::fmt::Write as _;
use clap::Command;
use nautilus_blockchain::exchanges::{
DEX_SUPPORTED_CHAINS, DexCapability, dex_capabilities_for_chain,
};
pub(crate) fn augment_blockchain_help(command: Command) -> Command {
command.mut_subcommand("blockchain", |blockchain| {
blockchain
.mut_subcommand("sync-dex", |c| c.after_long_help(render_discovery_help()))
.mut_subcommand("analyze-pool", |c| {
c.after_long_help(render_snapshot_help())
})
.mut_subcommand("analyze-pools", |c| {
c.after_long_help(render_snapshot_help())
})
})
}
fn render_discovery_help() -> String {
let mut out = String::from("Discoverable DEXes by chain (sync-dex):\n");
out.push_str(&capability_block(|c| c.discovery, |_| String::new()));
out.push_str(
"\nDEXes not listed lack a PoolCreated parser, so sync-dex rejects them before syncing.",
);
out
}
fn render_snapshot_help() -> String {
let mut out = String::from("Snapshot-capable DEXes by chain (analyze-pool, analyze-pools):\n");
out.push_str(&capability_block(|c| c.snapshots, snapshot_marker));
out.push_str(
"\n * replay-ready: SetFeeProtocol tracked across replay\
\n + analysis only: not discoverable via sync-dex, register the pool another way\
\nDEXes not listed lack the Initialize/Swap/Mint/Burn/Collect parsers, so analyze-pool(s) \
reject them before syncing.",
);
out
}
fn snapshot_marker(capability: DexCapability) -> String {
let mut marker = String::new();
if capability.replay_ready {
marker.push_str(" *");
}
if !capability.discovery {
marker.push_str(" +");
}
marker
}
fn capability_block(
keep: impl Fn(&DexCapability) -> bool,
marker: impl Fn(DexCapability) -> String,
) -> String {
let mut block = String::new();
for blockchain in DEX_SUPPORTED_CHAINS {
let names: Vec<String> = dex_capabilities_for_chain(blockchain)
.into_iter()
.filter(|c| keep(c))
.map(|c| format!("{}{}", c.dex_type, marker(c)))
.collect();
if names.is_empty() {
continue;
}
let label = format!("{}:", blockchain.to_string().to_lowercase());
let _ = writeln!(block, " {label:<10}{}", names.join(", "));
}
block
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
fn discovery_help_lists_discoverable_dexes() {
let help = render_discovery_help();
assert!(help.contains("UniswapV2"));
assert!(help.contains("UniswapV3"));
assert!(!help.contains("AerodromeSlipstream"));
assert!(!help.contains("SushiSwapV2"));
}
#[rstest]
fn snapshot_help_lists_snapshot_dexes_with_markers() {
let help = render_snapshot_help();
assert!(help.contains("UniswapV3 *")); assert!(help.contains("PancakeSwapV3"));
assert!(help.contains("AerodromeSlipstream +")); assert!(!help.contains("PancakeSwapV3 *"));
assert!(!help.contains("UniswapV2"));
assert!(!help.contains("SushiSwapV2"));
}
}