Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ simplicity-lang = { version = "0.6.0" }
simplicityhl = { version = "0.2.0" }
simplicityhl-core = { git = "https://github.com/BlockstreamResearch/simplicity-contracts.git", rev = "baa8ab7", package = "simplicityhl-core", features = ["encoding"] }
sled = { version = "0.34.7" }
tempfile = { version = "3.23.0" }
thiserror = { version = "2.0.17" }
tokio = { version = "1.48.0", features = ["macros", "test-util", "rt", "rt-multi-thread", "tracing" ] }
toml = { version = "0.9.8" }
tracing = { version = "0.1.41" }
tracing-appender = { version = "0.2.3" }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
3 changes: 3 additions & 0 deletions crates/dex-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true}
toml = { workspace = true }
6 changes: 3 additions & 3 deletions crates/dex-cli/src/cli/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ pub enum MakerCommands {
#[arg(long = "filler-utxo")]
filler_token_utxo: OutPoint,
/// UTXO containing Maker grantor collateral tokens to be locked or burned
#[arg(long = "grant-coll-utxo")]
#[arg(long = "grantor-collateral-utxo")]
grantor_collateral_token_utxo: OutPoint,
/// UTXO containing Maker grantor settlement tokens to be locked or burned
#[arg(long = "grant-settl-utxo")]
#[arg(long = "grantor-settlement-utxo")]
grantor_settlement_token_utxo: OutPoint,
/// UTXO providing the settlement asset (e.g. LBTC) for the DCD contract
#[arg(long = "settl-asset-utxo")]
#[arg(long = "settlement-asset-utxo")]
settlement_asset_utxo: OutPoint,
/// UTXO used to pay miner fees for the Maker funding transaction
#[arg(long = "fee-utxo")]
Expand Down
134 changes: 67 additions & 67 deletions crates/dex-cli/src/cli/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub enum Command {
#[derive(Debug, Clone)]
struct CliAppContext {
agg_config: AggregatedConfig,
relay_processor: RelayProcessor,
}

struct MakerSettlementCliContext {
Expand Down Expand Up @@ -159,28 +158,14 @@ struct MergeTokens4CliContext {
maker_order_event_id: EventId,
}

impl Cli {
/// Initialize aggregated CLI configuration from CLI args, config file and env.
///
/// # Errors
///
/// Returns an error if building or validating the aggregated configuration
/// (including loading the config file or environment overrides) fails.
pub fn init_config(&self) -> crate::error::Result<AggregatedConfig> {
AggregatedConfig::new(self)
}

impl CliAppContext {
/// Initialize the relay processor using the provided relays and optional keypair.
///
/// # Errors
///
/// Returns an error if creating or configuring the underlying Nostr relay
/// client fails, or if connecting to the specified relays fails.
pub async fn init_relays(
&self,
relays: &[RelayUrl],
keypair: Option<Keys>,
) -> crate::error::Result<RelayProcessor> {
pub async fn init_relays(relays: &[RelayUrl], keypair: Option<Keys>) -> crate::error::Result<RelayProcessor> {
let relay_processor = RelayProcessor::try_from_config(
relays,
keypair,
Expand All @@ -191,6 +176,18 @@ impl Cli {
.await?;
Ok(relay_processor)
}
}

impl Cli {
/// Initialize aggregated CLI configuration from CLI args, config file and env.
///
/// # Errors
///
/// Returns an error if building or validating the aggregated configuration
/// (including loading the config file or environment overrides) fails.
pub fn init_config(&self) -> crate::error::Result<AggregatedConfig> {
AggregatedConfig::new(self)
}

/// Process the CLI command and execute the selected action.
///
Expand All @@ -205,14 +202,7 @@ impl Cli {
pub async fn process(self) -> crate::error::Result<()> {
let agg_config = self.init_config()?;

let relay_processor = self
.init_relays(&agg_config.relays, agg_config.nostr_keypair.clone())
.await?;

let cli_app_context = CliAppContext {
agg_config,
relay_processor,
};
let cli_app_context = CliAppContext { agg_config };
let msg = {
match self.command {
Command::ShowConfig => {
Expand Down Expand Up @@ -389,10 +379,7 @@ impl Cli {
}

async fn _process_maker_fund(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MakerFundCliContext {
filler_token_utxo,
grantor_collateral_token_utxo,
Expand All @@ -411,6 +398,8 @@ impl Cli {

agg_config.check_nostr_keypair_existence()?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(account_index, dcd_taproot_pubkey_gen)?;
let event_to_publish = processed_args.extract_event();
let (tx_id, args_to_save) = handle(
Expand All @@ -432,10 +421,7 @@ impl Cli {
}

async fn _process_maker_termination_collateral(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MakerCollateralTerminationCliContext {
grantor_collateral_token_utxo,
fee_utxo,
Expand All @@ -452,11 +438,14 @@ impl Cli {
use contract_handlers::maker_termination_collateral::{Utxos, handle, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = contract_handlers::maker_termination_collateral::process_args(
account_index,
grantor_collateral_amount_to_burn,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand All @@ -480,10 +469,7 @@ impl Cli {
}

async fn _process_maker_termination_settlement(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MakerSettlementTerminationCliContext {
fee_utxo,
settlement_asset_utxo,
Expand All @@ -500,11 +486,14 @@ impl Cli {
use contract_handlers::maker_termination_settlement::{Utxos, handle, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = contract_handlers::maker_termination_settlement::process_args(
account_index,
grantor_settlement_amount_to_burn,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand All @@ -529,10 +518,7 @@ impl Cli {

#[allow(clippy::too_many_lines)]
async fn _process_maker_settlement(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MakerSettlementCliContext {
grantor_collateral_token_utxo,
grantor_settlement_token_utxo,
Expand All @@ -552,13 +538,16 @@ impl Cli {
use contract_handlers::maker_settlement::{Utxos, handle, process_args, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(
account_index,
price_at_current_block_height,
oracle_signature,
grantor_amount_to_burn,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand All @@ -584,10 +573,7 @@ impl Cli {

#[allow(clippy::too_many_lines)]
async fn process_taker_commands(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
action: TakerCommands,
) -> crate::error::Result<String> {
Ok(match action {
Expand All @@ -602,11 +588,15 @@ impl Cli {
use contract_handlers::taker_funding::{Utxos, handle, process_args, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor =
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(
common_options.account_index,
collateral_amount_to_deposit,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand Down Expand Up @@ -637,11 +627,15 @@ impl Cli {
use contract_handlers::taker_early_termination::{Utxos, handle, process_args, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor =
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(
common_options.account_index,
filler_token_amount_to_return,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand Down Expand Up @@ -675,13 +669,17 @@ impl Cli {
use contract_handlers::taker_settlement::{Utxos, handle, process_args, save_args_to_cache};

agg_config.check_nostr_keypair_existence()?;

let relay_processor =
CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(
common_options.account_index,
price_at_current_block_height,
filler_amount_to_burn,
oracle_signature,
maker_order_event_id,
relay_processor,
&relay_processor,
)
.await?;
let (tx_id, args_to_save) = handle(
Expand Down Expand Up @@ -911,10 +909,7 @@ impl Cli {
}

async fn _process_helper_merge_tokens2(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MergeTokens2CliContext {
token_utxo_1,
token_utxo_2,
Expand All @@ -933,7 +928,10 @@ impl Cli {
};

agg_config.check_nostr_keypair_existence()?;
let processed_args = process_args(account_index, maker_order_event_id, relay_processor).await?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(account_index, maker_order_event_id, &relay_processor).await?;
let (tx_id, args_to_save) = handle(
processed_args,
Utxos2 {
Expand Down Expand Up @@ -962,10 +960,7 @@ impl Cli {
}

async fn _process_helper_merge_tokens3(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MergeTokens3CliContext {
token_utxo_1,
token_utxo_2,
Expand All @@ -985,7 +980,10 @@ impl Cli {
};

agg_config.check_nostr_keypair_existence()?;
let processed_args = process_args(account_index, maker_order_event_id, relay_processor).await?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(account_index, maker_order_event_id, &relay_processor).await?;
let (tx_id, args_to_save) = handle(
processed_args,
Utxos3 {
Expand Down Expand Up @@ -1016,10 +1014,7 @@ impl Cli {
}

async fn _process_helper_merge_tokens4(
CliAppContext {
agg_config,
relay_processor,
}: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
MergeTokens4CliContext {
token_utxo_1,
token_utxo_2,
Expand All @@ -1040,7 +1035,10 @@ impl Cli {
};

agg_config.check_nostr_keypair_existence()?;
let processed_args = process_args(account_index, maker_order_event_id, relay_processor).await?;

let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

let processed_args = process_args(account_index, maker_order_event_id, &relay_processor).await?;
let (tx_id, args_to_save) = handle(
processed_args,
Utxos4 {
Expand Down Expand Up @@ -1073,9 +1071,11 @@ impl Cli {
}

async fn process_dex_commands(
CliAppContext { relay_processor, .. }: &CliAppContext,
CliAppContext { agg_config, .. }: &CliAppContext,
action: DexCommands,
) -> crate::error::Result<String> {
let relay_processor = CliAppContext::init_relays(&agg_config.relays, agg_config.nostr_keypair.clone()).await?;

Ok(match action {
DexCommands::GetOrderReplies { event_id } => {
let res = relay_processor.get_order_replies(event_id).await?;
Expand Down
Loading