305 lines
11 KiB
Rust
Raw Normal View History

2025-12-16 14:05:34 +02:00
use anyhow::Result;
use clap::Subcommand;
2025-12-18 11:44:38 +02:00
use nssa::AccountId;
2025-12-16 14:05:34 +02:00
use crate::{
2025-12-18 11:44:38 +02:00
WalletCore,
2026-05-14 21:19:25 -04:00
account::AccountIdWithPrivacy,
cli::{CliAccountMention, SubcommandReturnValue, WalletSubcommand},
2025-12-22 04:42:32 +02:00
program_facades::amm::Amm,
2025-12-16 14:05:34 +02:00
};
2026-03-10 00:17:43 +03:00
/// Represents generic CLI subcommand for a wallet working with amm program.
2025-12-16 14:05:34 +02:00
#[derive(Subcommand, Debug, Clone)]
pub enum AmmProgramAgnosticSubcommand {
2026-03-10 00:17:43 +03:00
/// Produce a new pool.
2025-12-16 14:05:34 +02:00
///
2026-03-03 23:21:08 +03:00
/// `user_holding_a` and `user_holding_b` must be owned.
2025-12-18 11:44:38 +02:00
///
2026-03-10 00:17:43 +03:00
/// Only public execution allowed.
2025-12-16 14:05:34 +02:00
New {
2026-05-14 21:19:25 -04:00
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_a: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_b: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_lp: CliAccountMention,
2025-12-16 14:05:34 +02:00
#[arg(long)]
balance_a: u128,
#[arg(long)]
2026-04-26 21:29:54 -04:00
balance_b: u128,
2025-12-16 14:05:34 +02:00
},
/// Swap specifying exact input amount.
2025-12-16 14:05:34 +02:00
///
2026-03-10 00:17:43 +03:00
/// The account associated with swapping token must be owned.
2025-12-18 11:44:38 +02:00
///
2026-03-10 00:17:43 +03:00
/// Only public execution allowed.
SwapExactInput {
2026-05-14 21:19:25 -04:00
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_a: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_b: CliAccountMention,
2025-12-16 14:05:34 +02:00
#[arg(long)]
amount_in: u128,
#[arg(long)]
min_amount_out: u128,
2026-03-10 00:17:43 +03:00
/// `token_definition` - valid 32 byte base58 string WITHOUT privacy prefix.
2025-12-16 14:05:34 +02:00
#[arg(long)]
2026-05-14 21:19:25 -04:00
token_definition: AccountId,
2025-12-16 14:05:34 +02:00
},
/// Swap specifying exact output amount.
///
/// The account associated with swapping token must be owned.
///
/// Only public execution allowed.
SwapExactOutput {
2026-05-14 21:19:25 -04:00
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
2026-05-14 21:19:25 -04:00
user_holding_a: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
2026-05-14 21:19:25 -04:00
user_holding_b: CliAccountMention,
#[arg(long)]
exact_amount_out: u128,
#[arg(long)]
max_amount_in: u128,
/// `token_definition` - valid 32 byte base58 string WITHOUT privacy prefix.
#[arg(long)]
2026-05-14 21:19:25 -04:00
token_definition: AccountId,
},
2026-03-10 00:17:43 +03:00
/// Add liquidity.
2025-12-16 14:05:34 +02:00
///
2026-03-03 23:21:08 +03:00
/// `user_holding_a` and `user_holding_b` must be owned.
2025-12-18 11:44:38 +02:00
///
2026-03-10 00:17:43 +03:00
/// Only public execution allowed.
2025-12-16 14:05:34 +02:00
AddLiquidity {
2026-05-14 21:19:25 -04:00
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_a: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_b: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_lp: CliAccountMention,
2025-12-16 14:05:34 +02:00
#[arg(long)]
min_amount_lp: u128,
#[arg(long)]
max_amount_a: u128,
#[arg(long)]
max_amount_b: u128,
},
2026-03-10 00:17:43 +03:00
/// Remove liquidity.
2025-12-16 14:05:34 +02:00
///
2026-03-03 23:21:08 +03:00
/// `user_holding_lp` must be owned.
2025-12-18 11:44:38 +02:00
///
2026-03-10 00:17:43 +03:00
/// Only public execution allowed.
2025-12-16 14:05:34 +02:00
RemoveLiquidity {
2026-05-14 21:19:25 -04:00
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_a: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_b: CliAccountMention,
/// Either 32 byte base58 account id string with privacy prefix or a label.
#[arg(long)]
user_holding_lp: CliAccountMention,
2025-12-16 14:05:34 +02:00
#[arg(long)]
balance_lp: u128,
#[arg(long)]
2025-12-19 19:12:58 +02:00
min_amount_a: u128,
2025-12-16 14:05:34 +02:00
#[arg(long)]
2025-12-19 19:12:58 +02:00
min_amount_b: u128,
2025-12-16 14:05:34 +02:00
},
}
impl WalletSubcommand for AmmProgramAgnosticSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2026-03-09 18:27:56 +03:00
Self::New {
2025-12-16 14:05:34 +02:00
user_holding_a,
user_holding_b,
user_holding_lp,
balance_a,
balance_b,
} => {
2026-05-15 18:15:54 -04:00
let a_id = user_holding_a.resolve(wallet_core.storage())?;
let b_id = user_holding_b.resolve(wallet_core.storage())?;
let lp_id = user_holding_lp.resolve(wallet_core.storage())?;
match (a_id, b_id, lp_id) {
2025-12-18 11:44:38 +02:00
(
2026-05-15 18:15:54 -04:00
AccountIdWithPrivacy::Public(a),
AccountIdWithPrivacy::Public(b),
AccountIdWithPrivacy::Public(lp),
2025-12-18 11:44:38 +02:00
) => {
2025-12-22 04:42:32 +02:00
Amm(wallet_core)
.send_new_definition(
2026-05-15 18:15:54 -04:00
a,
b,
lp,
2025-12-18 11:44:38 +02:00
balance_a,
balance_b,
2026-05-15 18:15:54 -04:00
&user_holding_a,
&user_holding_b,
&user_holding_lp,
2025-12-18 11:44:38 +02:00
)
.await?;
Ok(SubcommandReturnValue::Empty)
}
_ => {
// ToDo: Implement after private multi-chain calls is available
2025-12-22 04:42:32 +02:00
anyhow::bail!("Only public execution allowed for Amm calls");
2025-12-18 11:44:38 +02:00
}
2025-12-16 14:05:34 +02:00
}
}
Self::SwapExactInput {
2025-12-16 14:05:34 +02:00
user_holding_a,
user_holding_b,
amount_in,
min_amount_out,
token_definition,
} => {
2026-05-15 18:15:54 -04:00
let a_id = user_holding_a.resolve(wallet_core.storage())?;
let b_id = user_holding_b.resolve(wallet_core.storage())?;
match (a_id, b_id) {
2026-05-17 12:32:43 -04:00
(AccountIdWithPrivacy::Public(a), AccountIdWithPrivacy::Public(b)) => {
2025-12-22 04:42:32 +02:00
Amm(wallet_core)
.send_swap_exact_input(
2026-05-15 18:15:54 -04:00
a,
b,
2025-12-18 11:44:38 +02:00
amount_in,
min_amount_out,
2026-05-14 21:19:25 -04:00
token_definition,
2026-05-15 18:15:54 -04:00
&user_holding_a,
&user_holding_b,
2025-12-18 11:44:38 +02:00
)
.await?;
2025-12-16 14:05:34 +02:00
2025-12-18 11:44:38 +02:00
Ok(SubcommandReturnValue::Empty)
}
_ => {
// ToDo: Implement after private multi-chain calls is available
2025-12-22 04:42:32 +02:00
anyhow::bail!("Only public execution allowed for Amm calls");
2025-12-18 11:44:38 +02:00
}
2025-12-16 14:05:34 +02:00
}
}
Self::SwapExactOutput {
user_holding_a,
user_holding_b,
exact_amount_out,
max_amount_in,
token_definition,
} => {
2026-05-15 18:15:54 -04:00
let a_id = user_holding_a.resolve(wallet_core.storage())?;
let b_id = user_holding_b.resolve(wallet_core.storage())?;
match (a_id, b_id) {
2026-05-17 12:32:43 -04:00
(AccountIdWithPrivacy::Public(a), AccountIdWithPrivacy::Public(b)) => {
Amm(wallet_core)
.send_swap_exact_output(
2026-05-15 18:15:54 -04:00
a,
b,
exact_amount_out,
max_amount_in,
2026-05-14 21:19:25 -04:00
token_definition,
2026-05-15 18:15:54 -04:00
&user_holding_a,
&user_holding_b,
)
.await?;
Ok(SubcommandReturnValue::Empty)
}
_ => {
// ToDo: Implement after private multi-chain calls is available
anyhow::bail!("Only public execution allowed for Amm calls");
}
}
2025-12-16 14:05:34 +02:00
}
2026-03-09 18:27:56 +03:00
Self::AddLiquidity {
2025-12-16 14:05:34 +02:00
user_holding_a,
user_holding_b,
user_holding_lp,
min_amount_lp,
max_amount_a,
max_amount_b,
} => {
2026-05-15 18:15:54 -04:00
let a_id = user_holding_a.resolve(wallet_core.storage())?;
let b_id = user_holding_b.resolve(wallet_core.storage())?;
let lp_id = user_holding_lp.resolve(wallet_core.storage())?;
match (a_id, b_id, lp_id) {
2025-12-18 11:44:38 +02:00
(
2026-05-15 18:15:54 -04:00
AccountIdWithPrivacy::Public(a),
AccountIdWithPrivacy::Public(b),
AccountIdWithPrivacy::Public(lp),
2025-12-18 11:44:38 +02:00
) => {
2025-12-22 04:42:32 +02:00
Amm(wallet_core)
.send_add_liquidity(
2026-05-15 18:15:54 -04:00
a,
b,
lp,
2025-12-18 11:44:38 +02:00
min_amount_lp,
max_amount_a,
max_amount_b,
2026-05-15 18:15:54 -04:00
&user_holding_a,
&user_holding_b,
2026-05-17 12:32:43 -04:00
&user_holding_lp,
2025-12-18 11:44:38 +02:00
)
.await?;
Ok(SubcommandReturnValue::Empty)
}
_ => {
// ToDo: Implement after private multi-chain calls is available
2025-12-22 04:42:32 +02:00
anyhow::bail!("Only public execution allowed for Amm calls");
2025-12-18 11:44:38 +02:00
}
2025-12-16 14:05:34 +02:00
}
}
2026-03-09 18:27:56 +03:00
Self::RemoveLiquidity {
2025-12-16 14:05:34 +02:00
user_holding_a,
user_holding_b,
user_holding_lp,
balance_lp,
2025-12-19 19:12:58 +02:00
min_amount_a,
min_amount_b,
2025-12-16 14:05:34 +02:00
} => {
2026-05-15 18:15:54 -04:00
let a_id = user_holding_a.resolve(wallet_core.storage())?;
let b_id = user_holding_b.resolve(wallet_core.storage())?;
let lp_id = user_holding_lp.resolve(wallet_core.storage())?;
match (a_id, b_id, lp_id) {
2025-12-18 11:44:38 +02:00
(
2026-05-15 18:15:54 -04:00
AccountIdWithPrivacy::Public(a),
AccountIdWithPrivacy::Public(b),
AccountIdWithPrivacy::Public(lp),
2025-12-18 11:44:38 +02:00
) => {
2025-12-22 04:42:32 +02:00
Amm(wallet_core)
.send_remove_liquidity(
2026-05-15 18:15:54 -04:00
a,
b,
lp,
2025-12-18 11:44:38 +02:00
balance_lp,
2025-12-19 19:12:58 +02:00
min_amount_a,
min_amount_b,
2026-05-15 18:15:54 -04:00
&user_holding_lp,
2025-12-18 11:44:38 +02:00
)
.await?;
2025-12-16 14:05:34 +02:00
2025-12-18 11:44:38 +02:00
Ok(SubcommandReturnValue::Empty)
}
_ => {
// ToDo: Implement after private multi-chain calls is available
2025-12-22 04:42:32 +02:00
anyhow::bail!("Only public execution allowed for Amm calls");
2025-12-18 11:44:38 +02:00
}
2025-12-16 14:05:34 +02:00
}
}
}
}
}