use anyhow::Result;
use clap::Args;
use tonic::transport::Channel;
use tonic::Request;
use crate::cdk_mint_client::CdkMintClient;
use crate::RotateNextKeysetRequest;
#[derive(Args, Debug)]
pub struct RotateNextKeysetCommand {
#[arg(short, long)]
#[arg(default_value = "sat")]
unit: String,
#[arg(short, long)]
amounts: Option<String>,
#[arg(short, long)]
input_fee_ppk: Option<u64>,
#[arg(long)]
use_keyset_v2: Option<bool>,
}
pub async fn rotate_next_keyset(
client: &mut CdkMintClient<Channel>,
sub_command_args: &RotateNextKeysetCommand,
) -> Result<()> {
let amounts = if let Some(amounts_str) = &sub_command_args.amounts {
amounts_str
.split(',')
.map(|s| s.trim().parse::<u64>())
.collect::<Result<Vec<u64>, _>>()?
} else {
vec![]
};
let response = client
.rotate_next_keyset(Request::new(RotateNextKeysetRequest {
unit: sub_command_args.unit.clone(),
amounts,
input_fee_ppk: sub_command_args.input_fee_ppk,
use_keyset_v2: sub_command_args.use_keyset_v2,
}))
.await?;
let response = response.into_inner();
println!(
"Rotated to new keyset {} for unit {} with amounts {} and fee of {}",
response.id,
response.unit,
serde_json::to_string(&response.amounts)?,
response.input_fee_ppk
);
Ok(())
}