use crate::commands::devnet::DevnetContext;
use crate::error::{HylixError, HylixResult};
use crate::logging::{log_info, log_success, log_warning, ProgressExecutor};
use client_sdk::rest_client::NodeApiClient;
use std::time::Duration;
#[derive(Debug, Clone, clap::Subcommand)]
pub enum ContractAction {
#[command(alias = "d")]
Delete {
name: String,
},
}
pub async fn execute(action: ContractAction) -> HylixResult<()> {
match action {
ContractAction::Delete { name } => {
delete_contract(&name).await?;
}
}
Ok(())
}
async fn delete_contract(contract_name: &str) -> HylixResult<()> {
let config = crate::config::HylixConfig::load()?;
let context = DevnetContext::new(config)?;
if !is_devnet_running(&context).await? {
return Err(HylixError::devnet(
"Devnet is not running. Please start the devnet first with 'hy devnet start'."
.to_string(),
));
}
log_info(&format!("Deleting contract '{contract_name}'..."));
let executor = ProgressExecutor::new();
let success = executor
.execute_command(
format!("Delete contract {contract_name}"),
"npx",
&[
"--yes",
"hyli-wallet-cli",
"delete_contract",
contract_name,
crate::constants::passwords::DEFAULT,
],
None,
)
.await?;
executor.clear()?;
if !success {
log_warning(&format!(
"Contract '{contract_name}' deletion completed with warnings"
));
log_warning(&format!(
"Command was: {}",
console::style(format!(
"npx hyli-wallet-cli delete_contract {contract_name} {}",
crate::constants::passwords::DEFAULT
))
.green()
));
} else {
log_success(&format!("Contract '{contract_name}' deleted successfully!"));
}
Ok(())
}
async fn is_devnet_running(context: &DevnetContext) -> HylixResult<bool> {
match tokio::time::timeout(Duration::from_secs(5), context.client.get_block_height()).await {
Ok(result) => match result {
Ok(_) => Ok(true),
Err(_) => Ok(false),
},
Err(_) => Ok(false),
}
}