1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::path::PathBuf;
use clap::Parser;
use fynd_rpc::config::defaults;
#[cfg(feature = "metrics")]
pub(crate) const METRICS_PORT: u16 = 9898;
#[cfg(feature = "metrics")]
pub(crate) const METRICS_HOST: &str = "0.0.0.0";
use crate::commands::derive_connector_tokens::DeriveConnectorTokensArgs;
/// Fynd - High-performance DEX solver built on Tycho
///
/// Finds optimal swap routes across multiple protocols using real-time market data.
#[derive(Parser, PartialEq, Debug)]
#[command(name = "fynd", version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
/// Available subcommands.
#[derive(clap::Subcommand, PartialEq, Debug)]
pub enum Commands {
/// Run the solver HTTP server
Serve(Box<ServeArgs>),
/// Print the OpenAPI spec as JSON to stdout
Openapi,
/// Analyze live Tycho market data and suggest connector tokens for routing
DeriveConnectorTokens(Box<DeriveConnectorTokensArgs>),
}
/// Arguments for the `serve` subcommand.
#[derive(clap::Args, PartialEq, Debug)]
pub struct ServeArgs {
/// Target chain (e.g. Ethereum)
#[arg(short, long, default_value = "Ethereum")]
pub chain: String,
/// HTTP host (e.g. 0.0.0.0)
#[arg(long, default_value = defaults::HTTP_HOST, env)]
pub http_host: String,
/// HTTP port
#[arg(long, default_value_t = defaults::HTTP_PORT, env)]
pub http_port: u16,
/// Tycho URL. Defaults to the Fynd endpoint for the selected chain.
#[arg(long, env)]
pub tycho_url: Option<String>,
/// Tycho API key
#[arg(long, env)]
pub tycho_api_key: Option<String>,
/// Disable TLS for Tycho connection
#[arg(long)]
pub disable_tls: bool,
/// Node RPC URL for the target chain. Defaults to a public endpoint if not set.
#[arg(long, env)]
pub rpc_url: Option<String>,
/// List of protocols to index (comma-separated, e.g., uniswap_v2,uniswap_v3).
/// If omitted, all on-chain protocols are fetched from Tycho RPC.
/// Use "all_onchain" to fetch all on-chain protocols and combine with explicit entries,
/// e.g., --protocols all_onchain,rfq:bebop.
/// Prefix a protocol with "exclusive:" to also stream its exclusive pools,
/// e.g., --protocols all_onchain,exclusive:ekubo_v3.
/// Prefix a protocol with "exclude:" to drop it from the list,
/// e.g., --protocols all_onchain,exclude:vm:fermiswap.
/// Use "pricelevelstream:<venue>" to serve a pAMM from the Titan price level stream,
/// e.g., --protocols all_onchain,exclude:vm:fermiswap,pricelevelstream:fermiswap.
#[arg(short, long, value_delimiter = ',', value_name = "PROTO1,PROTO2")]
pub protocols: Vec<String>,
/// Minimum TVL threshold in native token (e.g. ETH). Components below this threshold will be
/// removed from the market data. Defaults to a chain-specific value if not set.
#[arg(long)]
pub min_tvl: Option<f64>,
/// TVL buffer ratio.
/// Used to avoid fluctuations caused by components hovering around a single threshold.
/// Default is 1.1 (10% buffer). For example, if the minimum TVL is 10 ETH, components are
/// added when TVL >= 10 ETH and removed when TVL drops below 10 / 1.1 ≈ 9.09 ETH.
#[arg(long, default_value_t = defaults::TVL_BUFFER_RATIO)]
pub tvl_buffer_ratio: f64,
/// Minimum token quality filter.
#[arg(long, default_value_t = defaults::MIN_TOKEN_QUALITY)]
pub min_token_quality: i32,
/// Only include tokens traded within this many days.
#[arg(long, default_value_t = defaults::TRADED_N_DAYS_AGO)]
pub traded_n_days_ago: u64,
/// Gas price refresh interval in seconds
#[arg(long, default_value_t = defaults::GAS_REFRESH_INTERVAL.as_secs())]
pub gas_refresh_interval_secs: u64,
/// Reconnect delay on connection failure in seconds
#[arg(long, default_value_t = defaults::RECONNECT_DELAY.as_secs())]
pub reconnect_delay_secs: u64,
/// Worker router timeout in milliseconds
#[arg(long, default_value_t = defaults::WORKER_ROUTER_TIMEOUT_MS)]
pub worker_router_timeout_ms: u64,
/// Minimum solver responses before early return (0 = wait for all)
#[arg(long, default_value_t = defaults::ROUTER_MIN_RESPONSES)]
pub worker_router_min_responses: usize,
/// Path to worker pools TOML config file
#[arg(short, long, env, default_value = "worker_pools.toml")]
pub worker_pools_config: PathBuf,
/// Path to blocklist TOML config file. Components listed here are excluded from the
/// Tycho stream.
#[arg(long, env)]
pub blocklist_config: Option<PathBuf>,
/// Path to the custom-chains config (chains.yaml). Required to run a chain that Tycho does
/// not know as a built-in. Uses the same file the indexer reads.
#[arg(long, env = "TYCHO_CHAINS_CONFIG")]
pub chains_config: Option<PathBuf>,
/// Gas price staleness threshold in seconds. Health returns 503 when exceeded.
/// Disabled by default.
#[arg(long)]
pub gas_price_stale_threshold_secs: Option<u64>,
/// Enable partial block (flashblock) updates from the Tycho stream.
/// When enabled, component state updates arrive mid-block rather than only at finalization,
/// reducing latency. Only applies to on-chain protocols.
#[arg(long)]
pub partial_blocks: bool,
/// Enable price guard validation against external price sources.
/// Disabled by default.
#[arg(long)]
pub enable_price_guard: bool,
/// Enable simulation of encoded quotes against the latest block.
/// Disabled by default.
#[arg(long)]
pub enable_simulation: bool,
/// Watermark appended to every encoded transaction's calldata (e.g. "fynd"), so on-chain
/// observers can attribute router calls to this deployment. The EVM ignores calldata past
/// the ABI-encoded arguments, so the watermark does not change execution. When unset, the
/// calldata is stamped with `fynd/<version>` followed by a truncated SHA-256 of the Tycho
/// API key when one is configured.
#[arg(long, env)]
pub calldata_watermark: Option<String>,
/// URL of the hosted gateway fronting this instance. When set, a second Swagger UI is served
/// at /docs/hosted/, describing the gateway's per-chain paths and API-key auth. When unset,
/// only the self-hosted /docs/ is served.
#[arg(long, env = "FYND_HOSTED_SWAGGER_URL")]
pub hosted_swagger_url: Option<String>,
/// Port for the Prometheus metrics HTTP server (requires `metrics` feature).
#[cfg(feature = "metrics")]
#[arg(long, default_value_t = METRICS_PORT, env)]
pub metrics_port: u16,
/// Host/address the Prometheus metrics HTTP server binds to (requires `metrics` feature).
/// Defaults to all interfaces; set to `127.0.0.1` to expose metrics on loopback only.
#[cfg(feature = "metrics")]
#[arg(long, default_value = METRICS_HOST, env)]
pub metrics_host: String,
}
#[cfg(test)]
mod cli_tests {
use super::*;
#[test]
fn test_arg_parsing() {
let cli = Cli::try_parse_from(vec![
"fynd",
"serve",
"--chain",
"Ethereum",
"--http-host",
"127.0.0.1",
"--http-port",
"8080",
"--tycho-api-key",
"test-key",
"--rpc-url",
"https://rpc.example.com",
"--tycho-url",
"wss://custom.tycho.url",
"--protocols",
"uniswap_v2,uniswap_v3",
"--min-tvl",
"20.0",
"--worker-pools-config",
"new_worker_pools.toml",
"--hosted-swagger-url",
"https://gateway.example.com",
])
.expect("parse errored");
let Commands::Serve(args) = cli.command else {
panic!("expected Serve command");
};
assert_eq!(args.chain, "Ethereum");
assert_eq!(args.http_host, "127.0.0.1");
assert_eq!(args.http_port, 8080);
assert_eq!(args.tycho_api_key, Some("test-key".to_string()));
assert_eq!(args.rpc_url, Some("https://rpc.example.com".to_string()));
assert_eq!(args.tycho_url, Some("wss://custom.tycho.url".to_string()));
assert_eq!(args.protocols, vec!["uniswap_v2", "uniswap_v3"]);
assert_eq!(args.min_tvl, Some(20.0));
assert_eq!(args.worker_pools_config, PathBuf::from("new_worker_pools.toml"));
assert_eq!(args.blocklist_config, None);
assert_eq!(args.hosted_swagger_url, Some("https://gateway.example.com".to_string()));
}
#[test]
fn test_arg_parsing_defaults() {
// Clear ambient env vars so the test is deterministic regardless of the shell environment.
std::env::remove_var("RPC_URL");
std::env::remove_var("TYCHO_API_KEY");
std::env::remove_var("TYCHO_URL");
std::env::remove_var("HTTP_HOST");
std::env::remove_var("HTTP_PORT");
std::env::remove_var("FYND_HOSTED_SWAGGER_URL");
let cli = Cli::try_parse_from(vec!["fynd", "serve"]).expect("parse errored");
let Commands::Serve(args) = cli.command else {
panic!("expected Serve command");
};
assert_eq!(args.chain, "Ethereum");
assert_eq!(args.http_host, "0.0.0.0");
assert_eq!(args.http_port, 3000);
assert_eq!(args.tycho_api_key, None);
assert_eq!(args.rpc_url, None);
assert_eq!(args.tycho_url, None);
assert!(args.protocols.is_empty());
assert_eq!(args.min_tvl, None);
assert_eq!(args.tvl_buffer_ratio, 1.1);
assert_eq!(args.gas_refresh_interval_secs, 30);
assert_eq!(args.reconnect_delay_secs, 5);
assert_eq!(args.worker_router_timeout_ms, 100);
assert_eq!(args.worker_router_min_responses, 0);
assert_eq!(args.blocklist_config, None);
assert_eq!(args.hosted_swagger_url, None);
assert!(!args.partial_blocks);
#[cfg(feature = "metrics")]
assert_eq!(args.metrics_port, METRICS_PORT);
}
#[test]
fn test_arg_parsing_default_worker_pools() {
let cli = Cli::try_parse_from(vec!["fynd", "serve", "--tycho-api-key", "test-key"])
.expect("parse errored");
let Commands::Serve(args) = cli.command else {
panic!("expected Serve command");
};
assert_eq!(args.worker_pools_config, PathBuf::from("worker_pools.toml"));
}
#[test]
fn test_openapi_subcommand() {
let cli = Cli::try_parse_from(vec!["fynd", "openapi"]).expect("parse errored");
assert_eq!(cli.command, Commands::Openapi);
}
#[test]
fn test_parses_calldata_watermark() {
std::env::remove_var("CALLDATA_WATERMARK");
let cli = Cli::try_parse_from(vec!["fynd", "serve", "--calldata-watermark", "fynd"])
.expect("parse errored");
let Commands::Serve(args) = cli.command else { panic!("expected serve") };
assert_eq!(args.calldata_watermark, Some("fynd".to_string()));
let cli = Cli::try_parse_from(vec!["fynd", "serve"]).expect("parse errored");
let Commands::Serve(args) = cli.command else { panic!("expected serve") };
assert_eq!(args.calldata_watermark, None);
}
#[test]
fn parses_chains_config() {
let cli = Cli::try_parse_from(vec!["fynd", "serve", "--chains-config", "chains.yaml"])
.expect("parse errored");
let Commands::Serve(args) = cli.command else { panic!("expected serve") };
assert_eq!(args.chains_config, Some(PathBuf::from("chains.yaml")));
}
}