Skip to content

Commit 252ab60

Browse files
committed
Remove unnecessary connections to relays when they are not needed
1 parent faac292 commit 252ab60

File tree

1 file changed

+67
-67
lines changed

1 file changed

+67
-67
lines changed

crates/dex-cli/src/cli/processor.rs

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ pub enum Command {
8989
#[derive(Debug, Clone)]
9090
struct CliAppContext {
9191
agg_config: AggregatedConfig,
92-
relay_processor: RelayProcessor,
9392
}
9493

9594
struct MakerSettlementCliContext {
@@ -167,28 +166,14 @@ struct MergeTokens4CliContext {
167166
maker_order_event_id: EventId,
168167
}
169168

170-
impl Cli {
171-
/// Initialize aggregated CLI configuration from CLI args, config file and env.
172-
///
173-
/// # Errors
174-
///
175-
/// Returns an error if building or validating the aggregated configuration
176-
/// (including loading the config file or environment overrides) fails.
177-
pub fn init_config(&self) -> crate::error::Result<AggregatedConfig> {
178-
AggregatedConfig::new(self)
179-
}
180-
169+
impl CliAppContext {
181170
/// Initialize the relay processor using the provided relays and optional keypair.
182171
///
183172
/// # Errors
184173
///
185174
/// Returns an error if creating or configuring the underlying Nostr relay
186175
/// client fails, or if connecting to the specified relays fails.
187-
pub async fn init_relays(
188-
&self,
189-
relays: &[RelayUrl],
190-
keypair: Option<Keys>,
191-
) -> crate::error::Result<RelayProcessor> {
176+
pub async fn init_relays(relays: &[RelayUrl], keypair: Option<Keys>) -> crate::error::Result<RelayProcessor> {
192177
let relay_processor = RelayProcessor::try_from_config(
193178
relays,
194179
keypair,
@@ -199,6 +184,18 @@ impl Cli {
199184
.await?;
200185
Ok(relay_processor)
201186
}
187+
}
188+
189+
impl Cli {
190+
/// Initialize aggregated CLI configuration from CLI args, config file and env.
191+
///
192+
/// # Errors
193+
///
194+
/// Returns an error if building or validating the aggregated configuration
195+
/// (including loading the config file or environment overrides) fails.
196+
pub fn init_config(&self) -> crate::error::Result<AggregatedConfig> {
197+
AggregatedConfig::new(self)
198+
}
202199

203200
/// Process the CLI command and execute the selected action.
204201
///
@@ -213,14 +210,7 @@ impl Cli {
213210
pub async fn process(self) -> crate::error::Result<()> {
214211
let agg_config = self.init_config()?;
215212

216-
let relay_processor = self
217-
.init_relays(&agg_config.relays, agg_config.nostr_keypair.clone())
218-
.await?;
219-
220-
let cli_app_context = CliAppContext {
221-
agg_config,
222-
relay_processor,
223-
};
213+
let cli_app_context = CliAppContext { agg_config };
224214
let msg = {
225215
match self.command {
226216
Command::ShowConfig => {
@@ -401,10 +391,7 @@ impl Cli {
401391
}
402392

403393
async fn _process_maker_fund(
404-
CliAppContext {
405-
agg_config,
406-
relay_processor,
407-
}: &CliAppContext,
394+
CliAppContext { agg_config, .. }: &CliAppContext,
408395
MakerFundCliContext {
409396
filler_token_utxo,
410397
grantor_collateral_token_utxo,
@@ -424,6 +411,8 @@ impl Cli {
424411
agg_config.check_nostr_keypair_existence()?;
425412
agg_config.check_seed_hex_existence()?;
426413

414+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
415+
427416
let processed_args = process_args(account_index, dcd_taproot_pubkey_gen, agg_config)?;
428417
let event_to_publish = processed_args.extract_event();
429418
let (tx_id, args_to_save) = handle(
@@ -448,10 +437,7 @@ impl Cli {
448437
}
449438

450439
async fn _process_maker_termination_collateral(
451-
CliAppContext {
452-
agg_config,
453-
relay_processor,
454-
}: &CliAppContext,
440+
CliAppContext { agg_config, .. }: &CliAppContext,
455441
MakerCollateralTerminationCliContext {
456442
grantor_collateral_token_utxo,
457443
fee_utxo,
@@ -469,11 +455,14 @@ impl Cli {
469455

470456
agg_config.check_nostr_keypair_existence()?;
471457
agg_config.check_seed_hex_existence()?;
458+
459+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
460+
472461
let processed_args = contract_handlers::maker_termination_collateral::process_args(
473462
account_index,
474463
grantor_collateral_amount_to_burn,
475464
maker_order_event_id,
476-
relay_processor,
465+
&relay_processor,
477466
agg_config,
478467
)
479468
.await?;
@@ -498,10 +487,7 @@ impl Cli {
498487
}
499488

500489
async fn _process_maker_termination_settlement(
501-
CliAppContext {
502-
agg_config,
503-
relay_processor,
504-
}: &CliAppContext,
490+
CliAppContext { agg_config, .. }: &CliAppContext,
505491
MakerSettlementTerminationCliContext {
506492
fee_utxo,
507493
settlement_asset_utxo,
@@ -519,11 +505,14 @@ impl Cli {
519505

520506
agg_config.check_nostr_keypair_existence()?;
521507
agg_config.check_seed_hex_existence()?;
508+
509+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
510+
522511
let processed_args = contract_handlers::maker_termination_settlement::process_args(
523512
account_index,
524513
grantor_settlement_amount_to_burn,
525514
maker_order_event_id,
526-
relay_processor,
515+
&relay_processor,
527516
agg_config,
528517
)
529518
.await?;
@@ -549,10 +538,7 @@ impl Cli {
549538

550539
#[allow(clippy::too_many_lines)]
551540
async fn _process_maker_settlement(
552-
CliAppContext {
553-
agg_config,
554-
relay_processor,
555-
}: &CliAppContext,
541+
CliAppContext { agg_config, .. }: &CliAppContext,
556542
MakerSettlementCliContext {
557543
grantor_collateral_token_utxo,
558544
grantor_settlement_token_utxo,
@@ -573,13 +559,16 @@ impl Cli {
573559

574560
agg_config.check_nostr_keypair_existence()?;
575561
agg_config.check_seed_hex_existence()?;
562+
563+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
564+
576565
let processed_args = process_args(
577566
account_index,
578567
price_at_current_block_height,
579568
oracle_signature,
580569
grantor_amount_to_burn,
581570
maker_order_event_id,
582-
relay_processor,
571+
&relay_processor,
583572
agg_config,
584573
)
585574
.await?;
@@ -606,10 +595,7 @@ impl Cli {
606595

607596
#[allow(clippy::too_many_lines)]
608597
async fn process_taker_commands(
609-
CliAppContext {
610-
agg_config,
611-
relay_processor,
612-
}: &CliAppContext,
598+
CliAppContext { agg_config, .. }: &CliAppContext,
613599
action: TakerCommands,
614600
) -> crate::error::Result<String> {
615601
Ok(match action {
@@ -625,11 +611,15 @@ impl Cli {
625611

626612
agg_config.check_nostr_keypair_existence()?;
627613
agg_config.check_seed_hex_existence()?;
614+
615+
let relay_processor =
616+
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
617+
628618
let processed_args = process_args(
629619
common_options.account_index,
630620
collateral_amount_to_deposit,
631621
maker_order_event_id,
632-
relay_processor,
622+
&relay_processor,
633623
agg_config,
634624
)
635625
.await?;
@@ -662,11 +652,15 @@ impl Cli {
662652

663653
agg_config.check_nostr_keypair_existence()?;
664654
agg_config.check_seed_hex_existence()?;
655+
656+
let relay_processor =
657+
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
658+
665659
let processed_args = process_args(
666660
common_options.account_index,
667661
filler_token_amount_to_return,
668662
maker_order_event_id,
669-
relay_processor,
663+
&relay_processor,
670664
agg_config,
671665
)
672666
.await?;
@@ -702,13 +696,17 @@ impl Cli {
702696

703697
agg_config.check_nostr_keypair_existence()?;
704698
agg_config.check_seed_hex_existence()?;
699+
700+
let relay_processor =
701+
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
702+
705703
let processed_args = process_args(
706704
common_options.account_index,
707705
price_at_current_block_height,
708706
filler_amount_to_burn,
709707
oracle_signature,
710708
maker_order_event_id,
711-
relay_processor,
709+
&relay_processor,
712710
agg_config,
713711
)
714712
.await?;
@@ -975,10 +973,7 @@ impl Cli {
975973
}
976974

977975
async fn _process_helper_merge_tokens2(
978-
CliAppContext {
979-
agg_config,
980-
relay_processor,
981-
}: &CliAppContext,
976+
CliAppContext { agg_config, .. }: &CliAppContext,
982977
MergeTokens2CliContext {
983978
token_utxo_1,
984979
token_utxo_2,
@@ -998,7 +993,10 @@ impl Cli {
998993

999994
agg_config.check_nostr_keypair_existence()?;
1000995
agg_config.check_seed_hex_existence()?;
1001-
let processed_args = process_args(account_index, maker_order_event_id, relay_processor, agg_config).await?;
996+
997+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
998+
999+
let processed_args = process_args(account_index, maker_order_event_id, &relay_processor, agg_config).await?;
10021000
let (tx_id, args_to_save) = handle(
10031001
processed_args,
10041002
Utxos2 {
@@ -1027,10 +1025,7 @@ impl Cli {
10271025
}
10281026

10291027
async fn _process_helper_merge_tokens3(
1030-
CliAppContext {
1031-
agg_config,
1032-
relay_processor,
1033-
}: &CliAppContext,
1028+
CliAppContext { agg_config, .. }: &CliAppContext,
10341029
MergeTokens3CliContext {
10351030
token_utxo_1,
10361031
token_utxo_2,
@@ -1051,7 +1046,10 @@ impl Cli {
10511046

10521047
agg_config.check_nostr_keypair_existence()?;
10531048
agg_config.check_seed_hex_existence()?;
1054-
let processed_args = process_args(account_index, maker_order_event_id, relay_processor, agg_config).await?;
1049+
1050+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
1051+
1052+
let processed_args = process_args(account_index, maker_order_event_id, &relay_processor, agg_config).await?;
10551053
let (tx_id, args_to_save) = handle(
10561054
processed_args,
10571055
Utxos3 {
@@ -1082,10 +1080,7 @@ impl Cli {
10821080
}
10831081

10841082
async fn _process_helper_merge_tokens4(
1085-
CliAppContext {
1086-
agg_config,
1087-
relay_processor,
1088-
}: &CliAppContext,
1083+
CliAppContext { agg_config, .. }: &CliAppContext,
10891084
MergeTokens4CliContext {
10901085
token_utxo_1,
10911086
token_utxo_2,
@@ -1107,7 +1102,10 @@ impl Cli {
11071102

11081103
agg_config.check_nostr_keypair_existence()?;
11091104
agg_config.check_seed_hex_existence()?;
1110-
let processed_args = process_args(account_index, maker_order_event_id, relay_processor, agg_config).await?;
1105+
1106+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
1107+
1108+
let processed_args = process_args(account_index, maker_order_event_id, &relay_processor, agg_config).await?;
11111109
let (tx_id, args_to_save) = handle(
11121110
processed_args,
11131111
Utxos4 {
@@ -1140,9 +1138,11 @@ impl Cli {
11401138
}
11411139

11421140
async fn process_dex_commands(
1143-
CliAppContext { relay_processor, .. }: &CliAppContext,
1141+
CliAppContext { agg_config, .. }: &CliAppContext,
11441142
action: DexCommands,
11451143
) -> crate::error::Result<String> {
1144+
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;
1145+
11461146
Ok(match action {
11471147
DexCommands::GetOrderReplies { event_id } => {
11481148
let res = relay_processor.get_order_replies(event_id).await?;

0 commit comments

Comments
 (0)