1use std::io::Write as _;
37
38use clap::{ArgAction, Args, CommandFactory as _, Parser, Subcommand, ValueEnum};
39use degenbot_cli_core::{
40 AaveCommand, CliContext, CliError, Command, ConfigCommand, DatabaseCommand, ExchangeCommand,
41 FleetCommand, PathCommand, PathDirection, PoolCommand, PoolFamily, PosturePatchEntry,
42 StrategyCommand, StrategyFacet, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_BLOCK_RANGE, DEFAULT_TO_BLOCK,
43 DEFAULT_VERIFY_ALL_INTERVAL,
44};
45use degenbot_config::{EnvVars, NodeTransport, ProcessEnv, DEFAULT_CHAIN_ID_ENV};
46
47use crate::VERSION_LINE;
48
49#[derive(Debug, Parser)]
51#[command(
52 name = "degenbot",
53 about = "Perform cli.",
54 version = VERSION_LINE,
55 arg_required_else_help = true,
56 disable_help_subcommand = true
57)]
58pub struct Cli {
59 #[command(subcommand)]
61 pub command: Option<Commands>,
62
63 #[arg(long, global = true, value_name = "PATH")]
66 pub database: Option<String>,
67
68 #[arg(long, global = true, value_name = "CHAIN_ID")]
70 pub chain_id: Option<String>,
71
72 #[arg(long = "node", global = true, value_name = "URI", value_parser = parse_node)]
78 pub node: Vec<NodeArg>,
79
80 #[arg(long, global = true, value_name = "PATH")]
83 pub config: Option<String>,
84}
85
86#[derive(Debug, Subcommand)]
88pub enum Commands {
89 Database {
91 #[command(subcommand)]
93 command: DatabaseSub,
94 },
95 Exchange {
97 #[command(subcommand)]
99 command: ExchangeSub,
100 },
101 Pool {
103 #[command(subcommand)]
105 command: PoolSub,
106 },
107 Aave {
109 #[command(subcommand)]
111 command: AaveSub,
112 },
113 Fleet {
115 #[command(subcommand)]
117 command: FleetSub,
118 },
119 Path {
121 #[command(subcommand)]
123 command: PathSub,
124 },
125 Config {
127 #[command(subcommand)]
129 command: ConfigSub,
130 },
131 Strategy {
133 #[command(subcommand)]
135 command: StrategySub,
136 },
137}
138
139#[derive(Debug, Subcommand)]
141pub enum DatabaseSub {
142 Backup,
144 Reset {
146 #[arg(long)]
148 force: bool,
149 },
150 Upgrade {
153 #[arg(long)]
155 force: bool,
156 },
157 Compact,
159 Cutover {
161 #[arg(long)]
163 dry_run: bool,
164 #[arg(long)]
166 force: bool,
167 },
168 Heal {
171 #[arg(long)]
173 dry_run: bool,
174 #[arg(long)]
176 force: bool,
177 },
178 Inspect,
180}
181
182#[derive(Debug, Subcommand)]
184pub enum ExchangeSub {
185 Activate {
188 #[arg(long, value_name = "CHAIN")]
191 chain: String,
192 #[arg(long, value_name = "NAME")]
195 name: String,
196 },
197 Deactivate {
200 #[arg(long, value_name = "CHAIN")]
203 chain: String,
204 #[arg(long, value_name = "NAME")]
206 name: String,
207 },
208 List {
210 #[arg(long, value_name = "CHAIN")]
212 chain: Option<String>,
213 },
214}
215
216#[derive(Debug, Subcommand)]
218pub enum PoolSub {
219 Update {
221 #[arg(long = "chunk", value_name = "BLOCKS", default_value_t = DEFAULT_CHUNK_SIZE)]
224 chunk_size: u64,
225 #[arg(long = "max-block-range", value_name = "BLOCKS", default_value_t = DEFAULT_MAX_BLOCK_RANGE)]
229 max_block_range: u64,
230 #[arg(long = "to-block", value_name = "BLOCK", default_value = DEFAULT_TO_BLOCK)]
236 to_block: String,
237 #[command(flatten)]
239 verify: VerifyFlags,
240 #[arg(
244 long = "verify-all-interval",
245 value_name = "BLOCKS",
246 default_value_t = DEFAULT_VERIFY_ALL_INTERVAL
247 )]
248 verify_all_interval: u64,
249 },
250 Verify {
252 #[arg(long = "rpc-url", value_name = "URL", required = true)]
254 rpc_url: String,
255 #[arg(long = "chain", value_name = "CHAIN_ID", required = true)]
259 pool_chain_id: i64,
260 #[arg(long = "block", value_name = "BLOCK", required = true)]
262 block_number: u64,
263 #[arg(
264 long = "pool",
265 value_name = "POOL",
266 required = true,
267 help = "The pool to verify. V3: the pool contract address. V4: the PoolId (pool_hash, bytes32 hex 0x…)."
268 )]
269 pool: String,
270 #[arg(
271 long = "family",
272 value_enum,
273 required = true,
274 help = "The pool family (selects ticks()/tickBitmap() vs PoolManager extsload)."
275 )]
276 family: FamilyArg,
277 #[arg(
278 long = "pool-manager",
279 value_name = "ADDRESS",
280 help = "(V4 only) The deployed V4 PoolManager singleton address (the V4 exchange's factory). Required for --family v4."
281 )]
282 pool_manager: Option<String>,
283 },
284}
285
286#[derive(Debug, Clone, Args)]
288#[expect(
289 clippy::struct_excessive_bools,
290 reason = "the two on/off gate pairs are argv vocabulary, not state: each pair is two mutually-overriding flags"
291)]
292pub struct VerifyFlags {
293 #[arg(long = "verify-chunk", action = ArgAction::SetTrue, overrides_with = "no_verify_chunk")]
297 pub verify_chunk: bool,
298 #[arg(long = "no-verify-chunk", action = ArgAction::SetTrue, overrides_with = "verify_chunk")]
300 pub no_verify_chunk: bool,
301 #[arg(long = "verify-all", action = ArgAction::SetTrue, overrides_with = "no_verify_all")]
305 pub verify_all: bool,
306 #[arg(long = "no-verify-all", action = ArgAction::SetTrue, overrides_with = "verify_all")]
308 pub no_verify_all: bool,
309}
310
311impl VerifyFlags {
312 #[must_use]
314 pub const fn chunk_gate(&self) -> bool {
315 self.verify_chunk || !self.no_verify_chunk
316 }
317
318 #[must_use]
320 pub const fn all_gate(&self) -> bool {
321 self.verify_all && !self.no_verify_all
322 }
323}
324
325#[derive(Debug, Clone, Copy, ValueEnum)]
327pub enum FamilyArg {
328 V3,
330 V4,
332}
333
334#[derive(Debug, Subcommand)]
336pub enum AaveSub {
337 Activate,
340 Deactivate {
343 #[arg(
345 long = "name",
346 value_name = "MARKET",
347 default_value = "Aave Ethereum Market"
348 )]
349 market_name: String,
350 },
351 Digest {
355 #[arg(long = "market-name", value_name = "MARKET")]
358 market_name: Option<String>,
359 #[arg(long = "drive-manifest", value_name = "PATH")]
363 drive_manifest: Option<std::path::PathBuf>,
364 },
365 Update {
367 #[arg(long = "chunk", value_name = "BLOCKS", default_value_t = DEFAULT_CHUNK_SIZE)]
370 chunk_size: u64,
371 #[arg(long = "max-block-range", value_name = "BLOCKS", default_value_t = DEFAULT_MAX_BLOCK_RANGE)]
375 max_block_range: u64,
376 #[arg(long = "to-block", value_name = "BLOCK", default_value = DEFAULT_TO_BLOCK)]
382 to_block: String,
383 #[command(flatten)]
385 verify: VerifyFlags,
386 #[arg(
390 long = "verify-all-interval",
391 value_name = "BLOCKS",
392 default_value_t = DEFAULT_VERIFY_ALL_INTERVAL
393 )]
394 verify_all_interval: u64,
395 #[arg(long = "one-chunk", action = ArgAction::SetTrue)]
397 stop_after_one_chunk: bool,
398 #[arg(long = "dry-run", action = ArgAction::SetTrue)]
400 dry_run: bool,
401 #[arg(long = "backup", action = ArgAction::SetTrue, overrides_with = "no_backup")]
404 backup: bool,
405 #[arg(long = "no-backup", action = ArgAction::SetTrue, overrides_with = "backup")]
407 no_backup: bool,
408 },
409 Reset {
412 #[arg(
414 long = "name",
415 value_name = "MARKET",
416 default_value = "Aave Ethereum Market"
417 )]
418 market_name: String,
419 #[arg(long = "dry-run", action = ArgAction::SetTrue)]
421 dry_run: bool,
422 },
423 Position {
425 #[command(subcommand)]
427 command: AavePositionSub,
428 },
429}
430
431#[derive(Debug, Subcommand)]
433pub enum AavePositionSub {
434 Show {
436 #[arg(value_name = "ADDRESS")]
438 address: String,
439 #[arg(
441 long = "market",
442 value_name = "MARKET",
443 default_value = "Aave Ethereum Market"
444 )]
445 market: String,
446 },
447}
448
449#[derive(Debug, Subcommand)]
451pub enum FleetSub {
452 Posture {
454 #[command(subcommand)]
456 command: FleetPostureSub,
457 },
458}
459
460#[derive(Debug, Subcommand)]
462pub enum FleetPostureSub {
463 Show {
465 #[arg(
466 long,
467 value_name = "PATH",
468 help = "Unix domain socket path of the running bot's OperatorServer."
469 )]
470 socket: Option<String>,
471 },
472 Set {
474 #[arg(
475 long,
476 value_name = "PATH",
477 help = "Unix domain socket path of the running bot's OperatorServer."
478 )]
479 socket: Option<String>,
480 #[arg(long = "cordon-enter-events", value_name = "COUNT")]
482 cordon_enter_events: Option<u64>,
483 #[arg(long = "cordon-duty-percent", value_name = "PERCENT")]
486 cordon_duty_percent: Option<f64>,
487 #[arg(long = "cordon-enter-window-ms", value_name = "MS")]
489 cordon_enter_window_ms: Option<u64>,
490 #[arg(long = "cordon-duty-window-ms", value_name = "MS")]
492 cordon_duty_window_ms: Option<u64>,
493 #[arg(long = "cordon-exit-clean-ms", value_name = "MS")]
495 cordon_exit_clean_ms: Option<u64>,
496 #[arg(
497 long = "cordon-sim-intake-floor",
498 value_name = "COUNT|null",
499 help = "SimDriver new-lease cap while cordoned. The literal null restores half the slot cap."
500 )]
501 cordon_sim_intake_floor: Option<String>,
502 },
503}
504
505#[derive(Debug, Subcommand)]
507pub enum PathSub {
508 Add {
510 #[arg(
511 long,
512 value_name = "PATH",
513 help = "Unix domain socket path of the running bot's OperatorServer."
514 )]
515 socket: Option<String>,
516 #[arg(long = "hop", value_name = "HOP", required = true)]
519 hops: Vec<String>,
520 #[arg(long = "direction", value_enum)]
524 direction: Option<DirectionArg>,
525 },
526 Discover {
528 #[arg(
529 long,
530 value_name = "PATH",
531 help = "Unix domain socket path of the running bot's OperatorServer."
532 )]
533 socket: Option<String>,
534 #[arg(long = "bound", value_name = "COUNT")]
536 bound: Option<u64>,
537 },
538}
539
540#[derive(Debug, Clone, Copy, ValueEnum)]
542pub enum DirectionArg {
543 Zfo,
545 Ozf,
547}
548
549#[derive(Debug, Subcommand)]
552pub enum StrategySub {
553 List,
555 Show {
558 #[arg(value_enum)]
560 facet: FacetArg,
561 },
562 Activate {
566 #[arg(value_enum)]
568 facet: FacetArg,
569 #[arg(
571 long = "endpoints",
572 value_name = "URLS",
573 conflicts_with = "endpoints_default"
574 )]
575 endpoints: Option<String>,
576 #[arg(long = "endpoints-default")]
578 endpoints_default: bool,
579 },
580 Deactivate {
582 #[arg(value_enum)]
584 facet: FacetArg,
585 },
586 Set {
588 #[arg(value_enum)]
590 facet: FacetArg,
591 key: String,
593 value: String,
595 },
596 Default {
599 #[arg(value_enum)]
601 facet: FacetArg,
602 key: String,
604 },
605 Remove {
607 #[arg(value_enum)]
609 facet: FacetArg,
610 key: String,
612 },
613}
614
615#[derive(Debug, Subcommand)]
617pub enum ConfigSub {
618 Show {
621 #[arg(long)]
623 resolved: bool,
624 },
625 Path,
627 Get {
629 key: String,
631 },
632 Set {
634 key: String,
636 value: String,
638 #[arg(long)]
640 force: bool,
641 },
642 Unset {
644 key: String,
646 #[arg(long)]
648 force: bool,
649 },
650}
651
652#[derive(Debug, Clone, Copy, ValueEnum)]
654pub enum FacetArg {
655 Settlement,
657 #[value(name = "mevblocker_backrun")]
659 MevblockerBackrun,
660 #[value(name = "txpool_backrun")]
662 TxpoolBackrun,
663}
664
665#[derive(Debug, Clone, PartialEq, Eq)]
669pub struct NodeArg {
670 pub uri: String,
672 pub transport: NodeTransport,
674}
675
676fn parse_node(raw: &str) -> Result<NodeArg, String> {
683 let Some(transport) = NodeTransport::classify(raw) else {
684 let forms: Vec<String> = NodeTransport::ALL
685 .iter()
686 .map(|transport| format!("{} — {}", transport.key_path(), transport.expected()))
687 .collect();
688 return Err(format!(
689 "{raw:?} names no node transport; accepted forms: {}",
690 forms.join("; ")
691 ));
692 };
693 Ok(NodeArg {
694 uri: raw.to_string(),
695 transport,
696 })
697}
698
699#[must_use]
701pub fn context<'a>(cli: &Cli, env: &'a dyn EnvVars) -> CliContext<'a> {
702 let mut ctx = CliContext::new(env);
703 if let Some(database) = &cli.database {
704 ctx = ctx.with_database(database.clone());
705 }
706 if let Some(chain_id) = &cli.chain_id {
707 ctx = ctx.with_chain_id(chain_id.clone());
708 }
709 for node in &cli.node {
710 ctx = ctx.with_node(node.uri.clone(), node.transport);
711 }
712 if let Some(config) = &cli.config {
713 ctx = ctx.with_config(config.clone());
714 }
715 ctx
716}
717
718pub fn resolve(cli: &Cli) -> Result<Command, CliError> {
726 resolve_with_env(cli, &ProcessEnv)
727}
728
729pub fn resolve_with_env(cli: &Cli, env: &dyn EnvVars) -> Result<Command, CliError> {
740 let ctx = context(cli, env);
741 let Some(command) = &cli.command else {
742 return Err(CliError::InvalidArgument(
743 "a subcommand is required".to_string(),
744 ));
745 };
746 match command {
747 Commands::Database { command } => Ok(Command::Database(database(command))),
748 Commands::Exchange { command } => Ok(Command::Exchange(exchange(command))),
749 Commands::Pool { command } => Ok(Command::Pool(pool(command))),
750 Commands::Aave { command } => Ok(Command::Aave(aave(command, cli, &ctx)?)),
751 Commands::Fleet { command } => Ok(Command::Fleet(fleet(command)?)),
752 Commands::Path { command } => Ok(Command::Path(path(command))),
753 Commands::Strategy { command } => Ok(Command::Strategy(strategy(command))),
754 Commands::Config { command } => Ok(Command::Config(config(command))),
755 }
756}
757
758pub fn write_missing_subcommand_error() {
760 let mut command = Cli::command();
761 let usage = command.render_usage().to_string();
762 let _ = writeln!(
763 std::io::stderr().lock(),
764 "error: a subcommand is required\n\n{usage}"
765 );
766}
767
768fn database(command: &DatabaseSub) -> DatabaseCommand {
769 match command {
770 DatabaseSub::Backup => DatabaseCommand::Backup,
771 DatabaseSub::Reset { force } => DatabaseCommand::Reset { force: *force },
772 DatabaseSub::Upgrade { force } => DatabaseCommand::Upgrade { force: *force },
773 DatabaseSub::Compact => DatabaseCommand::Compact,
774 DatabaseSub::Cutover { dry_run, force } => DatabaseCommand::Cutover {
775 dry_run: *dry_run,
776 force: *force,
777 },
778 DatabaseSub::Heal { dry_run, force } => DatabaseCommand::Heal {
779 dry_run: *dry_run,
780 force: *force,
781 },
782 DatabaseSub::Inspect => DatabaseCommand::Inspect,
783 }
784}
785
786fn exchange(command: &ExchangeSub) -> ExchangeCommand {
787 match command {
788 ExchangeSub::Activate { chain, name } => ExchangeCommand::Activate {
789 chain: chain.clone(),
790 name: name.clone(),
791 },
792 ExchangeSub::Deactivate { chain, name } => ExchangeCommand::Deactivate {
793 chain: chain.clone(),
794 name: name.clone(),
795 },
796 ExchangeSub::List { chain } => ExchangeCommand::List {
797 chain: chain.clone(),
798 },
799 }
800}
801
802fn pool(command: &PoolSub) -> PoolCommand {
803 match command {
804 PoolSub::Update {
805 chunk_size,
806 max_block_range,
807 to_block,
808 verify,
809 verify_all_interval,
810 } => PoolCommand::Update {
811 chunk_size: *chunk_size,
812 max_block_range: *max_block_range,
813 to_block: to_block.clone(),
814 verify_chunk: verify.chunk_gate(),
815 verify_all: verify.all_gate(),
816 verify_all_interval: *verify_all_interval,
817 },
818 PoolSub::Verify {
819 rpc_url,
820 pool_chain_id,
821 block_number,
822 pool,
823 family,
824 pool_manager,
825 } => PoolCommand::Verify {
826 rpc_url: rpc_url.clone(),
827 chain_id: *pool_chain_id,
828 block_number: *block_number,
829 pool: pool.clone(),
830 family: match family {
831 FamilyArg::V3 => PoolFamily::V3,
832 FamilyArg::V4 => PoolFamily::V4,
833 },
834 pool_manager: pool_manager.clone(),
835 },
836 }
837}
838
839fn aave(command: &AaveSub, cli: &Cli, ctx: &CliContext<'_>) -> Result<AaveCommand, CliError> {
840 match command {
841 AaveSub::Activate => Ok(AaveCommand::Activate {
842 chain_id: chain_or_default(cli, ctx, 1)?,
843 }),
844 AaveSub::Deactivate { market_name } => Ok(AaveCommand::Deactivate {
845 chain_id: chain_or_default(cli, ctx, 1)?,
846 market_name: market_name.clone(),
847 }),
848 AaveSub::Digest {
849 market_name,
850 drive_manifest,
851 } => Ok(AaveCommand::Digest {
852 chain_id: chain_or_default(cli, ctx, 1)?,
853 market_name: market_name.clone(),
854 drive_manifest: drive_manifest.clone(),
855 }),
856 AaveSub::Update {
857 chunk_size,
858 max_block_range,
859 to_block,
860 verify,
861 verify_all_interval,
862 stop_after_one_chunk,
863 dry_run,
864 backup,
865 no_backup,
866 } => Ok(AaveCommand::Update {
867 chunk_size: *chunk_size,
868 max_block_range: *max_block_range,
869 to_block: to_block.clone(),
870 verify_chunk: verify.chunk_gate(),
871 verify_all: verify.all_gate(),
872 verify_all_interval: *verify_all_interval,
873 stop_after_one_chunk: *stop_after_one_chunk,
874 dry_run: *dry_run,
875 enable_backup: *backup && !*no_backup,
876 }),
877 AaveSub::Reset {
878 market_name,
879 dry_run,
880 } => Ok(AaveCommand::Reset {
881 chain_id: chain_or_default(cli, ctx, 1)?,
882 market_name: Some(market_name.clone()),
883 dry_run: *dry_run,
884 }),
885 AaveSub::Position { command } => match command {
886 AavePositionSub::Show { address, market } => Ok(AaveCommand::PositionShow {
887 address: address.clone(),
888 market: market.clone(),
889 chain_id: chain_or_default(cli, ctx, 1)?,
890 }),
891 },
892 }
893}
894
895fn fleet(command: &FleetSub) -> Result<FleetCommand, CliError> {
896 match command {
897 FleetSub::Posture { command } => match command {
898 FleetPostureSub::Show { socket } => Ok(FleetCommand::PostureShow {
899 socket: socket.clone(),
900 }),
901 FleetPostureSub::Set {
902 socket,
903 cordon_enter_events,
904 cordon_duty_percent,
905 cordon_enter_window_ms,
906 cordon_duty_window_ms,
907 cordon_exit_clean_ms,
908 cordon_sim_intake_floor,
909 } => {
910 let mut patch = Vec::new();
911 if let Some(value) = cordon_enter_events {
912 patch.push(PosturePatchEntry::int("cordon_enter_events", *value));
913 }
914 if let Some(value) = cordon_duty_percent {
915 patch.push(PosturePatchEntry::float("cordon_duty_percent", *value));
916 }
917 if let Some(value) = cordon_enter_window_ms {
918 patch.push(PosturePatchEntry::int("cordon_enter_window_ms", *value));
919 }
920 if let Some(value) = cordon_duty_window_ms {
921 patch.push(PosturePatchEntry::int("cordon_duty_window_ms", *value));
922 }
923 if let Some(value) = cordon_exit_clean_ms {
924 patch.push(PosturePatchEntry::int("cordon_exit_clean_ms", *value));
925 }
926 if let Some(value) = cordon_sim_intake_floor {
927 patch.push(PosturePatchEntry::new(
928 "cordon_sim_intake_floor",
929 degenbot_cli_core::parse_sim_intake_floor(value)?,
930 ));
931 }
932 Ok(FleetCommand::PostureSet {
933 socket: socket.clone(),
934 patch,
935 })
936 }
937 },
938 }
939}
940
941fn path(command: &PathSub) -> PathCommand {
942 match command {
943 PathSub::Add {
944 socket,
945 hops,
946 direction,
947 } => PathCommand::Add {
948 socket: socket.clone(),
949 hops: hops.clone(),
950 direction: direction.map(|direction| match direction {
951 DirectionArg::Zfo => PathDirection::Zfo,
952 DirectionArg::Ozf => PathDirection::Ozf,
953 }),
954 },
955 PathSub::Discover { socket, bound } => PathCommand::Discover {
956 socket: socket.clone(),
957 bound: *bound,
958 },
959 }
960}
961
962fn strategy(command: &StrategySub) -> StrategyCommand {
963 match command {
964 StrategySub::List => StrategyCommand::List,
965 StrategySub::Show { facet } => StrategyCommand::Show {
966 facet: facet_of(*facet),
967 },
968 StrategySub::Activate {
969 facet,
970 endpoints,
971 endpoints_default,
972 } => StrategyCommand::Activate {
973 facet: facet_of(*facet),
974 endpoints: endpoints.clone(),
975 endpoints_default: *endpoints_default,
976 },
977 StrategySub::Deactivate { facet } => StrategyCommand::Deactivate {
978 facet: facet_of(*facet),
979 },
980 StrategySub::Set { facet, key, value } => StrategyCommand::Set {
981 facet: facet_of(*facet),
982 key: key.clone(),
983 value: value.clone(),
984 },
985 StrategySub::Default { facet, key } => StrategyCommand::Default {
986 facet: facet_of(*facet),
987 key: key.clone(),
988 },
989 StrategySub::Remove { facet, key } => StrategyCommand::Remove {
990 facet: facet_of(*facet),
991 key: key.clone(),
992 },
993 }
994}
995
996fn config(command: &ConfigSub) -> ConfigCommand {
997 match command {
998 ConfigSub::Show { resolved } => ConfigCommand::Show {
999 resolved: *resolved,
1000 },
1001 ConfigSub::Path => ConfigCommand::Path,
1002 ConfigSub::Get { key } => ConfigCommand::Get { key: key.clone() },
1003 ConfigSub::Set { key, value, force } => ConfigCommand::Set {
1004 key: key.clone(),
1005 value: value.clone(),
1006 force: *force,
1007 },
1008 ConfigSub::Unset { key, force } => ConfigCommand::Unset {
1009 key: key.clone(),
1010 force: *force,
1011 },
1012 }
1013}
1014
1015fn facet_of(arg: FacetArg) -> StrategyFacet {
1016 match arg {
1017 FacetArg::Settlement => StrategyFacet::Settlement,
1018 FacetArg::MevblockerBackrun => StrategyFacet::MevblockerBackrun,
1019 FacetArg::TxpoolBackrun => StrategyFacet::TxpoolBackrun,
1020 }
1021}
1022
1023fn chain_or_default(cli: &Cli, ctx: &CliContext<'_>, default: u64) -> Result<u64, CliError> {
1027 let cli_layer = cli
1028 .chain_id
1029 .as_deref()
1030 .is_some_and(|value| !value.is_empty());
1031 let env_layer = ctx
1032 .env()
1033 .get(DEFAULT_CHAIN_ID_ENV)
1034 .is_some_and(|value| !value.is_empty());
1035 let file_layer = ctx.loaded_config()?.config.session.chain_id.is_some();
1038 if cli_layer || env_layer || file_layer {
1039 return Ok(ctx.chain_id()?.value);
1040 }
1041 Ok(default)
1042}