quicknode-cli 0.6.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! MPP payment-channel lifecycle. Channel state is cached by payer, pay network,
//! and asset. Lifecycle operations are keyless and single-attempt.

use clap::{Args as ClapArgs, Subcommand};
use quicknode_sdk::ChannelState;

use crate::config::{self, PaymentSection};
use crate::context::{Ctx, GlobalArgs};
use crate::errors::CliError;
use crate::output::{style, Style};

use super::payment::{resolve_payment_params, PaymentParams};

#[derive(Debug, ClapArgs)]
#[command(subcommand_required = true, arg_required_else_help = true)]
#[command(after_help = "Examples:\n  \
    qn rpc mpp open --deposit 1000000 \\\n      \
    --payment-wallet payer --payment-network tempo-testnet \\\n      \
    --payment-asset pathUSD --max-amount 1000000\n  \
    qn rpc call eth_blockNumber --network ethereum-mainnet --mpp-session \\\n      \
    --payment-wallet payer --payment-network tempo-testnet \\\n      \
    --payment-asset pathUSD --max-amount 1000000\n  \
    qn rpc mpp status \\\n      \
    --payment-wallet payer --payment-network tempo-testnet \\\n      \
    --payment-asset pathUSD --max-amount 1000000\n  \
    qn rpc mpp top-up --deposit 500000 \\\n      \
    --payment-wallet payer --payment-network tempo-testnet \\\n      \
    --payment-asset pathUSD --max-amount 500000\n  \
    qn rpc mpp close \\\n      \
    --payment-wallet payer --payment-network tempo-testnet \\\n      \
    --payment-asset pathUSD --max-amount 1000000")]
pub struct Args {
    #[command(subcommand)]
    pub cmd: MppCmd,
}

#[derive(Debug, Subcommand)]
pub enum MppCmd {
    /// Open a payment channel by depositing into the escrow. Gated: names the
    /// deposit before signing.
    #[command(after_help = "Examples:\n  \
        qn rpc mpp open --deposit 1000000 \\\n      \
        --payment-wallet payer --payment-network tempo-testnet --payment-asset USDC \\\n      \
        --max-amount 1000000")]
    Open(OpenArgs),

    /// Add deposit to the open channel. Gated.
    #[command(name = "top-up")]
    #[command(after_help = "Examples:\n  \
        qn rpc mpp top-up --deposit 500000 \\\n      \
        --payment-wallet payer --payment-network tempo-testnet \\\n      \
        --payment-asset pathUSD --max-amount 500000")]
    TopUp(TopUpArgs),

    /// Cooperatively close the channel (settle on-chain + refund unused
    /// deposit). Gated.
    #[command(after_help = "Examples:\n  \
        qn rpc mpp close \\\n      \
        --payment-wallet payer --payment-network tempo-testnet \\\n      \
        --payment-asset pathUSD --max-amount 1000000")]
    Close(ChannelArgs),

    /// Show the channel's deposit and remaining balance from the local record.
    /// Pass --verify to ask the gateway instead (spends one request unit).
    #[command(after_help = "Examples:\n  \
        qn rpc mpp status \\\n      \
        --payment-wallet payer --payment-network tempo-testnet \\\n      \
        --payment-asset pathUSD --max-amount 1000000\n  \
        qn rpc mpp status --verify \\\n      \
        --payment-wallet payer --payment-network tempo-testnet \\\n      \
        --payment-asset pathUSD --max-amount 1000000")]
    Status(StatusArgs),

    /// List the networks you can make MPP-paid RPC calls to. Each slug is a
    /// valid --network for a paid call. No API key required.
    #[command(visible_alias = "networks")]
    #[command(after_help = "Examples:\n  \
        qn rpc mpp networks\n  \
        qn rpc mpp supported-networks --format json")]
    SupportedNetworks,

    /// List the payment options the MPP gateway accepts: the network you pay
    /// on, the token, and its contract address — ready --payment-network and
    /// --payment-asset values. No API key required.
    #[command(visible_alias = "payments")]
    #[command(after_help = "Examples:\n  \
        qn rpc mpp payments\n  \
        qn rpc mpp supported-payments --format json")]
    SupportedPayments,
}

/// The payment parameter stack shared by the MPP verbs. There is no query
/// network here: the channel is scoped by --payment-network and
/// --payment-asset, and one open channel funds paid calls to every supported
/// network, so the lifecycle verbs never name a chain to query.
#[derive(Debug, ClapArgs)]
pub struct PaymentArgs {
    /// File containing the raw Tempo payment key (hex); `-` reads stdin.
    /// Precedence: this > --payment-wallet > `key_file` > `wallet` in config.
    #[arg(long, value_name = "PATH", conflicts_with = "payment_wallet")]
    pub payment_key_file: Option<std::path::PathBuf>,

    /// Name of a stored wallet (from `qn wallet generate`) to pay with.
    #[arg(long, value_name = "NAME")]
    pub payment_wallet: Option<String>,

    /// Spend ceiling for a single signed action (deposit/top-up), in integer
    /// base units. Flag > `max_amount` in [rpc.payment].
    #[arg(long, value_name = "BASE_UNITS")]
    pub max_amount: Option<String>,

    /// Chain the channel settles on: a network name or CAIP-2 id. Falls back to
    /// `payment_network` in [rpc.payment].
    #[arg(long, value_name = "NETWORK")]
    pub payment_network: Option<String>,

    /// Token the channel is denominated in: an address or a symbol like USDC.
    /// Falls back to `payment_asset` in [rpc.payment].
    #[arg(long, value_name = "ADDRESS")]
    pub payment_asset: Option<String>,

    /// Explicit Solana RPC URL (unused for Tempo channels; accepted for a
    /// uniform flag stack). Falls back to `svm_rpc_url` in [rpc.payment].
    #[arg(long, value_name = "URL")]
    pub svm_rpc_url: Option<String>,
}

impl PaymentArgs {
    fn params(&self) -> PaymentParams<'_> {
        PaymentParams {
            key_file: self.payment_key_file.as_deref(),
            wallet: self.payment_wallet.as_deref(),
            max_amount: self.max_amount.as_deref(),
            payment_network: self.payment_network.as_deref(),
            payment_asset: self.payment_asset.as_deref(),
            svm_rpc_url: self.svm_rpc_url.as_deref(),
        }
    }
}

#[derive(Debug, ClapArgs)]
pub struct OpenArgs {
    #[command(flatten)]
    pub payment: PaymentArgs,

    /// Amount to deposit into the escrow, in integer base units of the asset.
    #[arg(long, value_name = "BASE_UNITS")]
    pub deposit: String,
}

#[derive(Debug, ClapArgs)]
pub struct TopUpArgs {
    #[command(flatten)]
    pub payment: PaymentArgs,

    /// Additional amount to deposit into the open channel, in base units.
    #[arg(long, value_name = "BASE_UNITS")]
    pub deposit: String,
}

/// Verbs that operate on the already-open channel (no new deposit amount).
#[derive(Debug, ClapArgs)]
pub struct ChannelArgs {
    #[command(flatten)]
    pub payment: PaymentArgs,
}

/// `status` reads the local channel record by default. `--verify` asks the
/// gateway instead, which costs one request unit (see `run_status`).
#[derive(Debug, ClapArgs)]
pub struct StatusArgs {
    #[command(flatten)]
    pub payment: PaymentArgs,

    /// Ask the gateway for its view instead of reading the local record. Spends
    /// one request unit from the channel deposit.
    #[arg(long)]
    pub verify: bool,
}

pub async fn run(args: Args, global: GlobalArgs) -> Result<(), CliError> {
    match args.cmd {
        MppCmd::Open(a) => run_open(a, global).await,
        MppCmd::TopUp(a) => run_top_up(a, global).await,
        MppCmd::Close(a) => run_close(a.payment, global).await,
        MppCmd::Status(a) => run_status(a, global).await,
        MppCmd::SupportedNetworks => {
            super::supported_networks::run_networks(super::supported_networks::Scheme::Mpp, global)
                .await
        }
        MppCmd::SupportedPayments => {
            super::supported_networks::run_payments(super::supported_networks::Scheme::Mpp, global)
                .await
        }
    }
}

// Resolve payment config and the channel's pay scope before network I/O.
fn setup(args: &PaymentArgs, global: GlobalArgs) -> Result<(Ctx, PayScope), CliError> {
    let section = load_payment_section(&global)?;
    let wallets_dir = config::wallets_dir(global.resolve_config_path().as_deref());
    let (payment, key_file_warning) = resolve_payment_params(
        "mpp",
        &args.params(),
        &section,
        wallets_dir.as_deref(),
        global.base_url.clone(),
    )?;
    let scope = PayScope::from_config(&payment);
    let ctx = Ctx::from_global_keyless_payment(global, payment)?;
    if let Some(w) = key_file_warning {
        ctx.out.warn(&w);
    }
    Ok((ctx, scope))
}

// Pay scope carried to channel-cache keying; the payer address is added later.
pub(super) struct PayScope {
    pay_network: String,
    pay_asset: String,
}

impl PayScope {
    pub(super) fn from_config(payment: &quicknode_sdk::PaymentConfig) -> Self {
        PayScope {
            pay_network: payment.pay_network.clone(),
            pay_asset: payment.asset.clone(),
        }
    }

    pub(super) fn with_address(&self, address: String) -> config::ChannelScope {
        config::ChannelScope {
            address,
            pay_network: self.pay_network.clone(),
            pay_asset: self.pay_asset.clone(),
        }
    }

    pub(super) fn describe(&self) -> String {
        format!("{} on {}", self.pay_asset, self.pay_network)
    }
}

fn load_payment_section(global: &GlobalArgs) -> Result<PaymentSection, CliError> {
    let Some(path) = global.resolve_config_path() else {
        return Ok(PaymentSection::default());
    };
    Ok(config::load_from(&path)?
        .map(|cfg| cfg.rpc.payment)
        .unwrap_or_default())
}

fn parse_base_units(s: &str, flag: &str) -> Result<u128, CliError> {
    s.parse::<u128>().map_err(|_| {
        CliError::Arg(format!(
            "--{flag} must be a non-negative integer in base units, got '{s}'"
        ))
    })
}

// Print a ready-to-run next-command hint using explicit payment flags.
fn note_next(ctx: &Ctx, base: &str, payment: &PaymentArgs) {
    let mut flags: Vec<String> = Vec::new();
    if let Some(f) = &payment.payment_key_file {
        flags.push(format!("--payment-key-file {}", f.display()));
    }
    if let Some(w) = &payment.payment_wallet {
        flags.push(format!("--payment-wallet {w}"));
    }
    if let Some(n) = &payment.payment_network {
        flags.push(format!("--payment-network {n}"));
    }
    if let Some(a) = &payment.payment_asset {
        flags.push(format!("--payment-asset {a}"));
    }
    if let Some(m) = &payment.max_amount {
        flags.push(format!("--max-amount {m}"));
    }

    let mut lines = vec![format!("  {base}")];
    for chunk in flags.chunks(2) {
        lines.push(format!("    {}", chunk.join(" ")));
    }
    let cmd = lines.join(" \\\n");
    ctx.out.note(&format!(
        "\n{}\n\n{}\n",
        style("Example", Style::Bold, ctx.out.color),
        style(&cmd, Style::Bold, ctx.out.color),
    ));
}

async fn run_open(args: OpenArgs, global: GlobalArgs) -> Result<(), CliError> {
    let deposit = parse_base_units(&args.deposit, "deposit")?;
    let (ctx, scope) = setup(&args.payment, global.clone())?;

    crate::confirm::confirm_mild(
        &ctx,
        &format!(
            "Open an MPP channel with a {deposit} base-unit deposit of {}? \
             This moves real funds on-chain.",
            scope.describe()
        ),
    )?;

    let channel = ctx
        .sdk
        .rpc
        .mpp_open(deposit)
        .await
        .map_err(super::payment::map_paid_error)?;
    persist_channel(&ctx, &global, &scope, &channel);

    ctx.out.note(&format!(
        "✓ Opened channel {} (deposit: {})",
        channel.channel_id, channel.deposit
    ));
    note_next(
        &ctx,
        "qn rpc call eth_blockNumber --network ethereum-mainnet --mpp-session",
        &args.payment,
    );
    emit_channel(&ctx, &channel)
}

async fn run_top_up(args: TopUpArgs, global: GlobalArgs) -> Result<(), CliError> {
    let additional = parse_base_units(&args.deposit, "deposit")?;
    let (ctx, scope) = setup(&args.payment, global.clone())?;
    let channel = require_channel(&ctx, &global, &scope)?;

    crate::confirm::confirm_mild(
        &ctx,
        &format!(
            "Top up channel {} with {additional} more base units of {}? \
             This moves real funds on-chain.",
            channel.channel_id,
            scope.describe()
        ),
    )?;

    let updated = ctx
        .sdk
        .rpc
        .mpp_top_up(&channel, additional)
        .await
        .map_err(super::payment::map_paid_error)?;
    persist_channel(&ctx, &global, &scope, &updated);

    ctx.out.note(&format!(
        "✓ Topped up channel {} (deposit: {})",
        updated.channel_id, updated.deposit
    ));
    note_next(&ctx, "qn rpc mpp status", &args.payment);
    emit_channel(&ctx, &updated)
}

async fn run_close(args: PaymentArgs, global: GlobalArgs) -> Result<(), CliError> {
    let (ctx, scope) = setup(&args, global.clone())?;
    let channel = require_channel(&ctx, &global, &scope)?;

    crate::confirm::confirm_mild(
        &ctx,
        &format!(
            "Close channel {} ({})? It settles on-chain and refunds the \
             unused deposit; further --mpp-session calls fail until you open a \
             new channel.",
            channel.channel_id,
            scope.describe()
        ),
    )?;

    ctx.sdk
        .rpc
        .mpp_close(&channel)
        .await
        .map_err(super::payment::map_paid_error)?;
    // Do not reuse a channel after settlement.
    if let Some(address) = wallet_address(&ctx) {
        if let Some(path) = config::channels_cache_path(global.resolve_config_path().as_deref()) {
            let _ = config::delete_channel(&path, &scope.with_address(address));
        }
    }

    ctx.out
        .note(&format!("✓ Closed channel {}", channel.channel_id));
    note_next(&ctx, "qn rpc mpp open --deposit <BASE_UNITS>", &args);
    Ok(())
}

// Read local state by default; verification spends one request unit and syncs it.
async fn run_status(args: StatusArgs, global: GlobalArgs) -> Result<(), CliError> {
    let (ctx, scope) = setup(&args.payment, global.clone())?;
    let channel = require_channel(&ctx, &global, &scope)?;

    if !args.verify {
        return emit_status(&ctx, &channel, channel.cumulative_spent, None);
    }

    let status = ctx.sdk.rpc.mpp_status(&channel).await?;

    // Persist the gateway's accepted high-water mark before rendering.
    let mut synced = channel.clone();
    synced.cumulative_spent = synced
        .cumulative_spent
        .saturating_add(synced.per_call)
        .max(status.accepted_cumulative);
    persist_channel(&ctx, &global, &scope, &synced);

    emit_status(
        &ctx,
        &synced,
        status.accepted_cumulative,
        Some(status.spent),
    )
}

fn emit_status(
    ctx: &Ctx,
    channel: &ChannelState,
    accepted: u128,
    spent: Option<u128>,
) -> Result<(), CliError> {
    let deposit = channel.deposit;
    let remaining = deposit.saturating_sub(accepted);
    if matches!(ctx.global.format, Some(f) if f.is_structured()) {
        let mut v = serde_json::json!({
            "channel_id": channel.channel_id,
            "deposit": deposit,
            "accepted_cumulative": accepted,
            "remaining": remaining,
            "verified": spent.is_some(),
        });
        if let Some(spent) = spent {
            v["spent"] = serde_json::json!(spent);
        }
        return super::emit_result(ctx, &v);
    }
    ctx.out.note(&format!(
        "channel {}: deposit {}, accepted {}, remaining {}{}",
        channel.channel_id,
        deposit,
        accepted,
        remaining,
        if spent.is_some() {
            ""
        } else {
            " (local record; --verify asks the gateway)"
        },
    ));
    Ok(())
}

fn wallet_address(ctx: &Ctx) -> Option<String> {
    ctx.sdk.rpc.payment_address().ok()
}

fn persist_channel(ctx: &Ctx, global: &GlobalArgs, scope: &PayScope, channel: &ChannelState) {
    if let Some(address) = wallet_address(ctx) {
        if let Some(path) = config::channels_cache_path(global.resolve_config_path().as_deref()) {
            let _ = config::save_channel(&path, &scope.with_address(address), channel);
        }
    }
}

fn require_channel(
    ctx: &Ctx,
    global: &GlobalArgs,
    scope: &PayScope,
) -> Result<ChannelState, CliError> {
    let address = wallet_address(ctx)
        .ok_or_else(|| CliError::Arg("could not derive the payment wallet address".to_string()))?;
    let path = config::channels_cache_path(global.resolve_config_path().as_deref());
    let channel = path
        .as_deref()
        .and_then(|p| config::load_channel(p, &scope.with_address(address)));
    channel.ok_or_else(|| {
        CliError::Arg(format!(
            "no open MPP channel for this wallet paying {}. Open one with \
             'qn rpc mpp open --deposit <BASE_UNITS>'.",
            scope.describe()
        ))
    })
}

fn emit_channel(ctx: &Ctx, channel: &ChannelState) -> Result<(), CliError> {
    if matches!(ctx.global.format, Some(f) if f.is_structured()) {
        let v = serde_json::json!({
            "channel_id": channel.channel_id,
            "deposit": channel.deposit,
            "cumulative_spent": channel.cumulative_spent,
        });
        return super::emit_result(ctx, &v);
    }
    println!("{}", channel.channel_id);
    Ok(())
}