quicknode-cli 0.5.0

Command-line interface for the Quicknode SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Command bodies for `qn webhook …`.

use std::path::PathBuf;

use quicknode_sdk::webhooks::{
    ActivateWebhookParams, BitcoinWalletFilterByListTemplate, BitcoinWalletFilterInput,
    BitcoinWalletFilterTemplate, CreateWebhookFromTemplateParams, EvmAbiFilterByListTemplate,
    EvmAbiFilterInput, EvmAbiFilterTemplate, EvmContractEventsByListTemplate,
    EvmContractEventsInput, EvmContractEventsTemplate, EvmWalletFilterByListTemplate,
    EvmWalletFilterInput, EvmWalletFilterTemplate, GetWebhooksParams,
    HyperliquidWalletEventsFilterByListTemplate, HyperliquidWalletEventsFilterInput,
    HyperliquidWalletEventsFilterTemplate, SolanaWalletFilterByListTemplate,
    SolanaWalletFilterInput, SolanaWalletFilterTemplate,
    StellarWalletTransactionsFilterByListTemplate, StellarWalletTransactionsFilterInput,
    StellarWalletTransactionsFilterTemplate, TemplateArgs, UpdateWebhookParams,
    UpdateWebhookTemplateParams, WebhookDestinationAttributes, XrplWalletFilterByListTemplate,
    XrplWalletFilterInput, XrplWalletFilterTemplate,
};

use super::render::{WebhookView, WebhooksListView};
use super::{ActivateArgs, CreateArgs, ListArgs, TemplateKind, UpdateArgs, UpdateTemplateArgs};
use crate::confirm::{decide_without_prompt, prompt_yes_no, ConfirmCfg, Severity};
use crate::context::Ctx;
use crate::errors::CliError;
use crate::retry::retrying;

pub(super) async fn list(a: ListArgs, ctx: Ctx) -> Result<(), CliError> {
    let params = GetWebhooksParams {
        limit: a.limit,
        offset: a.offset,
    };
    let resp = retrying(ctx.global.retries, || {
        ctx.sdk.webhooks.list_webhooks(&params)
    })
    .await?;
    crate::output::emit(&ctx.out, &WebhooksListView(resp))
}

pub(super) async fn show(id: &str, ctx: Ctx) -> Result<(), CliError> {
    let w = retrying(ctx.global.retries, || ctx.sdk.webhooks.get_webhook(id)).await?;
    crate::output::emit(&ctx.out, &WebhookView(w))
}

pub(super) async fn create(a: CreateArgs, ctx: Ctx) -> Result<(), CliError> {
    let template_args = build_template_args(TemplateInputs {
        kind: a.template,
        wallets: a.wallets,
        accounts: a.accounts,
        contracts: a.contracts,
        event_hashes: a.event_hashes,
        abi: a.abi,
        abi_file: a.abi_file,
        wallets_list_name: a.wallets_list_name,
        accounts_list_name: a.accounts_list_name,
        contracts_list_name: a.contracts_list_name,
        event_hashes_list_name: a.event_hashes_list_name,
    })?;
    let params = CreateWebhookFromTemplateParams {
        name: a.name,
        network: a.network,
        notification_email: a.notification_email,
        destination_attributes: WebhookDestinationAttributes {
            url: a.url,
            security_token: a.security_token,
            compression: a.compression,
        },
        template_args,
    };
    let w = ctx
        .sdk
        .webhooks
        .create_webhook_from_template(&params)
        .await?;
    ctx.out.note(&format!("✓ Created webhook {}", w.id));
    crate::output::emit(&ctx.out, &WebhookView(w))
}

pub(super) async fn update(a: UpdateArgs, ctx: Ctx) -> Result<(), CliError> {
    if a.name.is_none()
        && a.notification_email.is_none()
        && a.url.is_none()
        && a.security_token.is_none()
        && a.compression.is_none()
    {
        return Err(CliError::Arg(
            "supply at least one of --name, --notification-email, --url, --security-token, --compression".into(),
        ));
    }
    let destination = match a.url {
        Some(url) => Some(build_destination(url, a.security_token, a.compression)?),
        None if a.security_token.is_some() || a.compression.is_some() => {
            return Err(CliError::Arg(
                "to change destination fields, also supply --url".into(),
            ));
        }
        None => None,
    };
    let params = UpdateWebhookParams {
        name: a.name,
        notification_email: a.notification_email,
        destination_attributes: destination,
    };
    let w = ctx.sdk.webhooks.update_webhook(&a.id, &params).await?;
    ctx.out.note(&format!("✓ Updated webhook {}", a.id));
    crate::output::emit(&ctx.out, &WebhookView(w))
}

pub(super) async fn update_template(a: UpdateTemplateArgs, ctx: Ctx) -> Result<(), CliError> {
    let template_args = build_template_args(TemplateInputs {
        kind: a.template,
        wallets: a.wallets,
        accounts: a.accounts,
        contracts: a.contracts,
        event_hashes: a.event_hashes,
        abi: a.abi,
        abi_file: a.abi_file,
        wallets_list_name: a.wallets_list_name,
        accounts_list_name: a.accounts_list_name,
        contracts_list_name: a.contracts_list_name,
        event_hashes_list_name: a.event_hashes_list_name,
    })?;
    let destination = match a.url {
        Some(url) => Some(build_destination(url, a.security_token, a.compression)?),
        None => None,
    };
    let params = UpdateWebhookTemplateParams {
        name: a.name,
        notification_email: a.notification_email,
        destination_attributes: destination,
        template_args,
    };
    let w = ctx
        .sdk
        .webhooks
        .update_webhook_template(&a.id, &params)
        .await?;
    ctx.out
        .note(&format!("✓ Updated template on webhook {}", a.id));
    crate::output::emit(&ctx.out, &WebhookView(w))
}

pub(super) async fn delete(id: &str, ctx: Ctx) -> Result<(), CliError> {
    let cfg = ConfirmCfg::new(
        ctx.global.yes_count,
        ctx.global.no_input,
        ctx.out.stdout_is_tty,
    );
    let proceed = match decide_without_prompt(Severity::Mild, cfg)? {
        true => true,
        false => prompt_yes_no(&format!("Delete webhook {id}?"))?,
    };
    if !proceed {
        return Err(CliError::Cancelled);
    }
    ctx.sdk.webhooks.delete_webhook(id).await?;
    ctx.out.note(&format!("✓ Deleted webhook {id}"));
    Ok(())
}

// There is intentionally no `webhook delete-all` command. Account-wide wipes
// are out of scope for the CLI; use the API directly if you really need one.

pub(super) async fn activate(a: ActivateArgs, ctx: Ctx) -> Result<(), CliError> {
    let params = ActivateWebhookParams {
        start_from: a.start_from.into(),
    };
    ctx.sdk.webhooks.activate_webhook(&a.id, &params).await?;
    ctx.out.note(&format!("✓ Activated webhook {}", a.id));
    Ok(())
}

pub(super) async fn pause(id: &str, ctx: Ctx) -> Result<(), CliError> {
    ctx.sdk.webhooks.pause_webhook(id).await?;
    ctx.out.note(&format!("✓ Paused webhook {id}"));
    Ok(())
}

pub(super) async fn enabled_count(ctx: Ctx) -> Result<(), CliError> {
    let resp = retrying(ctx.global.retries, || ctx.sdk.webhooks.get_enabled_count()).await?;
    if ctx.out.format.is_structured() {
        crate::output::emit(&ctx.out, &resp)
    } else {
        println!("{}", resp.total);
        Ok(())
    }
}

/// All template-selection inputs gathered from the CLI flags. Each template
/// reads the subset it needs; the rest stay unused for that variant.
struct TemplateInputs {
    kind: TemplateKind,
    wallets: Vec<String>,
    accounts: Vec<String>,
    contracts: Vec<String>,
    event_hashes: Vec<String>,
    abi: Option<String>,
    abi_file: Option<PathBuf>,
    wallets_list_name: Option<String>,
    accounts_list_name: Option<String>,
    contracts_list_name: Option<String>,
    event_hashes_list_name: Option<String>,
}

/// Resolved value source for a single template field: either inline values
/// supplied directly, or the name of a saved list to reference.
enum FilterSource {
    Inline(Vec<String>),
    ByList(String),
}

fn build_template_args(t: TemplateInputs) -> Result<TemplateArgs, CliError> {
    match t.kind {
        TemplateKind::EvmWallet => Ok(match wallets_source(t.wallets, t.wallets_list_name)? {
            FilterSource::Inline(wallets) => TemplateArgs::EvmWalletFilter(
                EvmWalletFilterInput::Inline(EvmWalletFilterTemplate { wallets }),
            ),
            FilterSource::ByList(wallets_list_name) => TemplateArgs::EvmWalletFilter(
                EvmWalletFilterInput::ByList(EvmWalletFilterByListTemplate { wallets_list_name }),
            ),
        }),
        TemplateKind::BitcoinWallet => Ok(match wallets_source(t.wallets, t.wallets_list_name)? {
            FilterSource::Inline(wallets) => TemplateArgs::BitcoinWalletFilter(
                BitcoinWalletFilterInput::Inline(BitcoinWalletFilterTemplate { wallets }),
            ),
            FilterSource::ByList(wallets_list_name) => {
                TemplateArgs::BitcoinWalletFilter(BitcoinWalletFilterInput::ByList(
                    BitcoinWalletFilterByListTemplate { wallets_list_name },
                ))
            }
        }),
        TemplateKind::XrplWallet => Ok(match wallets_source(t.wallets, t.wallets_list_name)? {
            FilterSource::Inline(wallets) => TemplateArgs::XrplWalletFilter(
                XrplWalletFilterInput::Inline(XrplWalletFilterTemplate { wallets }),
            ),
            FilterSource::ByList(wallets_list_name) => TemplateArgs::XrplWalletFilter(
                XrplWalletFilterInput::ByList(XrplWalletFilterByListTemplate { wallets_list_name }),
            ),
        }),
        TemplateKind::HyperliquidWalletEvents => {
            Ok(match wallets_source(t.wallets, t.wallets_list_name)? {
                FilterSource::Inline(wallets) => TemplateArgs::HyperliquidWalletEventsFilter(
                    HyperliquidWalletEventsFilterInput::Inline(
                        HyperliquidWalletEventsFilterTemplate { wallets },
                    ),
                ),
                FilterSource::ByList(wallets_list_name) => {
                    TemplateArgs::HyperliquidWalletEventsFilter(
                        HyperliquidWalletEventsFilterInput::ByList(
                            HyperliquidWalletEventsFilterByListTemplate { wallets_list_name },
                        ),
                    )
                }
            })
        }
        TemplateKind::StellarWalletTransactions => {
            Ok(match wallets_source(t.wallets, t.wallets_list_name)? {
                FilterSource::Inline(wallets) => {
                    TemplateArgs::StellarWalletTransactionsSourceAccountFilter(
                        StellarWalletTransactionsFilterInput::Inline(
                            StellarWalletTransactionsFilterTemplate { wallets },
                        ),
                    )
                }
                FilterSource::ByList(wallets_list_name) => {
                    TemplateArgs::StellarWalletTransactionsSourceAccountFilter(
                        StellarWalletTransactionsFilterInput::ByList(
                            StellarWalletTransactionsFilterByListTemplate { wallets_list_name },
                        ),
                    )
                }
            })
        }
        TemplateKind::SolanaWallet => {
            let source = filter_source(
                t.accounts,
                t.accounts_list_name,
                "--account",
                "--accounts-list-name",
            )?;
            Ok(match source {
                FilterSource::Inline(accounts) => TemplateArgs::SolanaWalletFilter(
                    SolanaWalletFilterInput::Inline(SolanaWalletFilterTemplate { accounts }),
                ),
                FilterSource::ByList(accounts_list_name) => {
                    TemplateArgs::SolanaWalletFilter(SolanaWalletFilterInput::ByList(
                        SolanaWalletFilterByListTemplate { accounts_list_name },
                    ))
                }
            })
        }
        TemplateKind::EvmContractEvents => {
            let source = filter_source(
                t.contracts,
                t.contracts_list_name,
                "--contract",
                "--contracts-list-name",
            )?;
            Ok(match source {
                FilterSource::Inline(contracts) => TemplateArgs::EvmContractEvents(
                    EvmContractEventsInput::Inline(EvmContractEventsTemplate {
                        contracts,
                        event_hashes: t.event_hashes,
                    }),
                ),
                FilterSource::ByList(contracts_list_name) => TemplateArgs::EvmContractEvents(
                    EvmContractEventsInput::ByList(EvmContractEventsByListTemplate {
                        contracts_list_name,
                        event_hashes_list_name: t.event_hashes_list_name,
                    }),
                ),
            })
        }
        TemplateKind::EvmAbi => {
            let abi_text = read_abi(t.abi, t.abi_file)?;
            // The ABI is always inline content; only the contracts can come
            // from a saved list, which selects the ByList variant.
            Ok(match t.contracts_list_name {
                Some(contracts_list_name) => {
                    if !t.contracts.is_empty() {
                        return Err(CliError::Arg(
                            "supply either --contract or --contracts-list-name, not both".into(),
                        ));
                    }
                    TemplateArgs::EvmAbiFilter(EvmAbiFilterInput::ByList(
                        EvmAbiFilterByListTemplate {
                            abi_json: abi_text,
                            contracts_list_name: Some(contracts_list_name),
                        },
                    ))
                }
                None => {
                    if t.contracts.is_empty() {
                        return Err(CliError::Arg(
                            "supply at least one --contract (or --contracts-list-name)".into(),
                        ));
                    }
                    TemplateArgs::EvmAbiFilter(EvmAbiFilterInput::Inline(EvmAbiFilterTemplate {
                        abi: abi_text,
                        contracts: t.contracts,
                    }))
                }
            })
        }
    }
}

/// Wallet-style templates all key off the same `--wallet` / `--wallets-list-name` pair.
fn wallets_source(
    wallets: Vec<String>,
    wallets_list_name: Option<String>,
) -> Result<FilterSource, CliError> {
    filter_source(
        wallets,
        wallets_list_name,
        "--wallet",
        "--wallets-list-name",
    )
}

/// Resolve an inline-values-vs-saved-list choice, rejecting "both" and "neither".
fn filter_source(
    inline: Vec<String>,
    list_name: Option<String>,
    inline_flag: &str,
    list_flag: &str,
) -> Result<FilterSource, CliError> {
    match list_name {
        Some(name) => {
            if !inline.is_empty() {
                return Err(CliError::Arg(format!(
                    "supply either {inline_flag} or {list_flag}, not both"
                )));
            }
            Ok(FilterSource::ByList(name))
        }
        None => {
            if inline.is_empty() {
                return Err(CliError::Arg(format!(
                    "supply at least one {inline_flag} (or {list_flag})"
                )));
            }
            Ok(FilterSource::Inline(inline))
        }
    }
}

fn read_abi(abi: Option<String>, abi_file: Option<PathBuf>) -> Result<String, CliError> {
    match (abi, abi_file) {
        (Some(s), None) => Ok(s),
        (None, Some(p)) => Ok(std::fs::read_to_string(&p)?),
        (None, None) => Err(CliError::Arg("supply --abi or --abi-file".into())),
        (Some(_), Some(_)) => Err(CliError::Arg(
            "supply only one of --abi or --abi-file".into(),
        )),
    }
}

/// Build a destination from an explicit URL, requiring compression (the API
/// needs a non-optional value whenever a destination is sent).
fn build_destination(
    url: String,
    security_token: Option<String>,
    compression: Option<String>,
) -> Result<WebhookDestinationAttributes, CliError> {
    let compression = compression
        .ok_or_else(|| CliError::Arg("--url requires --compression (`gzip` or `none`)".into()))?;
    Ok(WebhookDestinationAttributes {
        url,
        security_token,
        compression,
    })
}