use eyre::Result;
use crate::output::OutputFormat;
fn parse_blockchain(
s: &str,
) -> Result<circle_developer_controlled_wallets::models::common::Blockchain> {
serde_json::from_str::<circle_developer_controlled_wallets::models::common::Blockchain>(
&format!("\"{}\"", s),
)
.map_err(|e| eyre::eyre!("Unrecognised blockchain '{}': {e}", s))
}
fn parse_tx_state(
s: &str,
) -> Result<circle_developer_controlled_wallets::models::transaction::TransactionState> {
serde_json::from_str::<
circle_developer_controlled_wallets::models::transaction::TransactionState,
>(&format!("\"{}\"", s))
.map_err(|e| eyre::eyre!("Unrecognised transaction state '{}': {e}", s))
}
#[derive(Debug, clap::Subcommand)]
pub enum DeveloperCommand {
ListWalletSets {
#[arg(long)]
page_size: Option<u32>,
},
GetWalletSet {
id: String,
},
ListWallets {
#[arg(long)]
blockchain: Option<String>,
#[arg(long)]
page_size: Option<u32>,
},
GetWallet {
id: String,
},
ListTransactions {
#[arg(long)]
blockchain: Option<String>,
#[arg(long)]
state: Option<String>,
#[arg(long)]
page_size: Option<u32>,
},
GetTransaction {
id: String,
},
GetToken {
id: String,
},
ValidateAddress {
#[arg(long)]
blockchain: String,
#[arg(long)]
address: String,
},
}
pub(crate) async fn run(
cmd: DeveloperCommand,
api_key: &str,
base_url: &str,
output: OutputFormat,
) -> Result<()> {
let client = circle_developer_controlled_wallets::DeveloperWalletsClient::with_base_url(
api_key, base_url,
);
match cmd {
DeveloperCommand::ListWalletSets { page_size } => {
use circle_developer_controlled_wallets::models::{
common::PageParams, wallet_set::ListWalletSetsParams,
};
let params =
ListWalletSetsParams { page: PageParams { page_size, ..Default::default() } };
let result = client.list_wallet_sets(¶ms).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::GetWalletSet { id } => {
let result = client.get_wallet_set(&id).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::ListWallets { blockchain, page_size } => {
use circle_developer_controlled_wallets::models::{
common::PageParams, wallet::ListWalletsParams,
};
let blockchain = blockchain.map(|s| parse_blockchain(&s)).transpose()?;
let params = ListWalletsParams {
blockchain,
page: PageParams { page_size, ..Default::default() },
..Default::default()
};
let result = client.list_wallets(¶ms).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::GetWallet { id } => {
let result = client.get_wallet(&id).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::ListTransactions { blockchain, state, page_size } => {
use circle_developer_controlled_wallets::models::transaction::ListTransactionsParams;
let blockchain = blockchain.map(|s| parse_blockchain(&s)).transpose()?;
let state = state.map(|s| parse_tx_state(&s)).transpose()?;
let params =
ListTransactionsParams { blockchain, state, page_size, ..Default::default() };
let result = client.list_transactions(¶ms).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::GetTransaction { id } => {
let result = client.get_transaction(&id).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::GetToken { id } => {
let result = client.get_token(&id).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
DeveloperCommand::ValidateAddress { blockchain, address } => {
use circle_developer_controlled_wallets::models::transaction::ValidateAddressRequest;
let req =
ValidateAddressRequest { blockchain: parse_blockchain(&blockchain)?, address };
let result = client.validate_address(&req).await.map_err(|e| eyre::eyre!("{e}"))?;
crate::output::print_result(&result, output);
Ok(())
}
}
}