mod actions;
mod render;
use std::path::PathBuf;
use clap::{Args as ClapArgs, Subcommand, ValueEnum};
use quicknode_sdk::webhooks::WebhookStartFrom;
use crate::context::Ctx;
use crate::errors::CliError;
#[derive(Debug, ClapArgs)]
#[command(after_help = "Examples:\n \
qn webhook list\n \
qn webhook create --name alerts --network ethereum-mainnet \\\n \
--url https://hook.example.com --template evmWalletFilter --wallet 0xabc\n \
qn webhook pause wh-1234")]
pub struct Args {
#[command(subcommand)]
pub cmd: WebhookCmd,
}
#[derive(Debug, Subcommand)]
pub enum WebhookCmd {
#[command(visible_alias = "ls")]
List(ListArgs),
Show {
#[arg(value_name = "WEBHOOK_ID")]
id: String,
},
Create(Box<CreateArgs>),
Update(UpdateArgs),
UpdateTemplate(Box<UpdateTemplateArgs>),
Delete {
#[arg(value_name = "WEBHOOK_ID")]
id: String,
},
Activate(ActivateArgs),
Pause {
#[arg(value_name = "WEBHOOK_ID")]
id: String,
},
EnabledCount,
}
#[derive(Debug, ClapArgs)]
pub struct ListArgs {
#[arg(long)]
pub limit: Option<i64>,
#[arg(long)]
pub offset: Option<i64>,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum TemplateKind {
EvmWallet,
EvmContractEvents,
EvmAbi,
SolanaWallet,
BitcoinWallet,
XrplWallet,
HyperliquidWalletEvents,
StellarWalletTransactions,
}
#[derive(Debug, ClapArgs)]
pub struct CreateArgs {
#[arg(long)]
pub name: String,
#[arg(long)]
pub network: String,
#[arg(long)]
pub url: String,
#[arg(long)]
pub security_token: Option<String>,
#[arg(long)]
pub compression: String,
#[arg(long)]
pub notification_email: Option<String>,
#[arg(long, value_enum)]
pub template: TemplateKind,
#[arg(long = "wallet")]
pub wallets: Vec<String>,
#[arg(long = "account")]
pub accounts: Vec<String>,
#[arg(long = "contract")]
pub contracts: Vec<String>,
#[arg(long = "event-hash")]
pub event_hashes: Vec<String>,
#[arg(long, conflicts_with = "abi_file")]
pub abi: Option<String>,
#[arg(long)]
pub abi_file: Option<PathBuf>,
#[arg(long)]
pub wallets_list_name: Option<String>,
#[arg(long)]
pub accounts_list_name: Option<String>,
#[arg(long)]
pub contracts_list_name: Option<String>,
#[arg(long)]
pub event_hashes_list_name: Option<String>,
}
#[derive(Debug, ClapArgs)]
pub struct UpdateArgs {
#[arg(value_name = "WEBHOOK_ID")]
pub id: String,
#[arg(long)]
pub name: Option<String>,
#[arg(long)]
pub notification_email: Option<String>,
#[arg(long)]
pub url: Option<String>,
#[arg(long)]
pub security_token: Option<String>,
#[arg(long)]
pub compression: Option<String>,
}
#[derive(Debug, ClapArgs)]
pub struct UpdateTemplateArgs {
#[arg(value_name = "WEBHOOK_ID")]
pub id: String,
#[arg(long, value_enum)]
pub template: TemplateKind,
#[arg(long = "wallet")]
pub wallets: Vec<String>,
#[arg(long = "account")]
pub accounts: Vec<String>,
#[arg(long = "contract")]
pub contracts: Vec<String>,
#[arg(long = "event-hash")]
pub event_hashes: Vec<String>,
#[arg(long, conflicts_with = "abi_file")]
pub abi: Option<String>,
#[arg(long)]
pub abi_file: Option<PathBuf>,
#[arg(long)]
pub wallets_list_name: Option<String>,
#[arg(long)]
pub accounts_list_name: Option<String>,
#[arg(long)]
pub contracts_list_name: Option<String>,
#[arg(long)]
pub event_hashes_list_name: Option<String>,
#[arg(long)]
pub name: Option<String>,
#[arg(long)]
pub notification_email: Option<String>,
#[arg(long)]
pub url: Option<String>,
#[arg(long)]
pub security_token: Option<String>,
#[arg(long)]
pub compression: Option<String>,
}
#[derive(Debug, ClapArgs)]
pub struct ActivateArgs {
#[arg(value_name = "WEBHOOK_ID")]
pub id: String,
#[arg(long, value_enum, default_value = "latest")]
pub start_from: StartFromArg,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum StartFromArg {
Last,
Latest,
}
impl From<StartFromArg> for WebhookStartFrom {
fn from(s: StartFromArg) -> Self {
match s {
StartFromArg::Last => WebhookStartFrom::Last,
StartFromArg::Latest => WebhookStartFrom::Latest,
}
}
}
pub async fn run(args: Args, ctx: Ctx) -> Result<(), CliError> {
match args.cmd {
WebhookCmd::List(a) => actions::list(a, ctx).await,
WebhookCmd::Show { id } => actions::show(&id, ctx).await,
WebhookCmd::Create(a) => actions::create(*a, ctx).await,
WebhookCmd::Update(a) => actions::update(a, ctx).await,
WebhookCmd::UpdateTemplate(a) => actions::update_template(*a, ctx).await,
WebhookCmd::Delete { id } => actions::delete(&id, ctx).await,
WebhookCmd::Activate(a) => actions::activate(a, ctx).await,
WebhookCmd::Pause { id } => actions::pause(&id, ctx).await,
WebhookCmd::EnabledCount => actions::enabled_count(ctx).await,
}
}