outlayer-cli 0.2.4

CLI for deploying, running, and managing OutLayer agents
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use anyhow::{Context, Result};
use serde_json::json;

use crate::api::{ApiClient, GetPubkeyRequest};
use crate::config::{self, NetworkConfig};
use crate::crypto;
use crate::near::{ContractCaller, NearClient};

/// `outlayer keys create [--project owner/name]…` — create a new payment key,
/// optionally restricted to the projects named.
pub async fn create(network: &NetworkConfig, projects: &[String]) -> Result<()> {
    for project in projects {
        check_project_scope(project)?;
    }
    let creds = config::load_credentials(network)?;

    let near = NearClient::new(network);
    let caller = ContractCaller::from_credentials(&creds, network)?;
    let api = ApiClient::new(network);

    // Get next nonce
    let nonce = near
        .get_next_payment_key_nonce(&creds.account_id)
        .await
        .context("Failed to get next payment key nonce")?;

    eprintln!("Creating payment key (nonce: {nonce})...");

    // Generate secret
    let secret = crypto::generate_payment_key_secret();

    // Build secrets JSON.
    //
    // Every field is a STRING, and `initial_balance` especially. The worker
    // reads this blob when the contract announces the key and requires
    // `initial_balance` to be a string it can parse as a number; a JSON null
    // fails that read, the key is never registered with the coordinator, and
    // what the caller is left holding is a key that exists on chain, is
    // refused by the API, and cannot be topped up — because the top-up reads
    // the same blob. The dashboard writes the same shape, and it must stay the
    // same shape: there is one reader.
    //
    // `max_per_call` of "0" means no limit, which is what an unset limit is.
    let secrets_json = json!({
        "key": secret,
        "project_ids": projects,
        "max_per_call": "0",
        "initial_balance": "0"
    })
    .to_string();

    // Get pubkey for encryption
    let pubkey = api
        .get_secrets_pubkey(
            &GetPubkeyRequest {
                accessor: json!({ "type": "System", "PaymentKey": {} }),
                owner: creds.account_id.clone(),
                profile: Some(nonce.to_string()),
                // The keystore checks names only; a payment key's names are
                // fixed, so nothing is sent, as the dashboard does.
                secrets_json: "{}".to_string(),
            },
            // Payment keys are owner-scoped, never vault-bound — the
            // System(PaymentKey) accessor doesn't take a vault_id.
            None,
        )
        .await
        .context("Failed to get keystore pubkey")?;

    // Encrypt. A payment key is a `System` accessor — nothing to normalise, so
    // only the key itself is taken from the answer.
    let encrypted = crypto::encrypt_secrets(&pubkey.pubkey, &secrets_json)?;

    let api_key = format!("{}:{}:{}", creds.account_id, nonce, secret);

    // Written down BEFORE the transaction that creates the key.
    //
    // `secret` was generated in this process and exists nowhere else. On chain
    // it goes only as ciphertext the keystore alone can open, and nothing is
    // allowed to hand it back — that is what makes a payment key a credential
    // rather than a stored secret. So the order matters: once `store_secrets`
    // is broadcast the key exists, holds its storage deposit and can be topped
    // up, and a process that dies before it prints anything leaves a key nobody
    // can ever spend. Storing first makes the crash survivable; storing after
    // would only narrow the window.
    let stored_at = config::save_payment_key(&network.network_id, &creds.account_id, nonce, &api_key)
        .context("Refusing to create a payment key that cannot be written down")?;

    // Store on contract
    let deposit = 100_000_000_000_000_000_000_000u128; // 0.1 NEAR
    let gas = 100_000_000_000_000u64; // 100 TGas

    caller
        .call_contract(
            "store_secrets",
            json!({
                "accessor": { "System": "PaymentKey" },
                "profile": nonce.to_string(),
                "encrypted_secrets_base64": encrypted,
                "access": "AllowAll",
                // Payment keys stay on the OutLayer default master
                // (operational data, not custody).
                "vault_id": null,
            }),
            gas,
            deposit,
        )
        .await
        .context("Failed to store payment key")?;

    eprintln!("Payment key created (nonce: {nonce})");
    if projects.is_empty() {
        eprintln!("It pays for any project.");
    } else {
        eprintln!("It pays only for: {}", projects.join(", "));
    }
    println!("{api_key}");
    eprintln!(
        "\nSaved to {} (owner-only). Read it back with: outlayer keys show {nonce}",
        stored_at.display()
    );
    // A key with no balance is not yet usable: the API refuses a call it cannot
    // charge. Naming the next step here rather than leaving it to be discovered.
    eprintln!("Add a balance before using it: outlayer keys topup {nonce} --usd 1");

    Ok(())
}

/// `outlayer keys show` — print a payment key this machine created.
///
/// Reads what `create` wrote before it sent the transaction. Only this machine
/// has it: the chain holds ciphertext, and there is no route by which the
/// keystore hands a payment key back — a "recover my key" endpoint would let
/// anyone who can impersonate an owner walk off with a working credential.
///
/// So a key created elsewhere, or created before this stored anything, is gone
/// rather than merely absent, and the message says which of the two it is.
pub fn show(network: &NetworkConfig, nonce: u32) -> Result<()> {
    let creds = config::load_credentials(network)?;

    match config::load_payment_key(&network.network_id, &creds.account_id, nonce) {
        Some(key) => {
            println!("{key}");
            Ok(())
        }
        None => anyhow::bail!(
            "No payment key {}:{} on this machine. It is not recoverable from the chain — \
             the contract holds it encrypted for the keystore and nothing gives it back. \
             If the key still has a balance, withdraw it and create a new key.",
            creds.account_id,
            nonce
        ),
    }
}

/// `outlayer keys list` — list payment keys with balances
pub async fn list(network: &NetworkConfig) -> Result<()> {
    let creds = config::load_credentials(network)?;
    let near = NearClient::new(network);
    let api = ApiClient::new(network);

    // Get all user secrets, filter for System(PaymentKey) entries
    let secrets = near.list_user_secrets(&creds.account_id).await?;

    let payment_keys: Vec<_> = secrets
        .iter()
        .filter(|s| s.accessor.to_string().contains("System"))
        .collect();

    if payment_keys.is_empty() {
        eprintln!("No payment keys. Create one: outlayer keys create");
        return Ok(());
    }

    println!(
        "{:<8} {:>12} {:>12} {:>12}",
        "NONCE", "AVAILABLE", "SPENT", "INITIAL"
    );

    for pk in &payment_keys {
        let nonce: u32 = pk.profile.parse().unwrap_or(0);

        // Try to get balance from coordinator
        match api
            .get_payment_key_balance(&creds.account_id, nonce)
            .await
        {
            Ok(balance) => {
                println!(
                    "{:<8} {:>12} {:>12} {:>12}",
                    nonce,
                    format_usd(&balance.available),
                    format_usd(&balance.spent),
                    format_usd(&balance.initial_balance),
                );
            }
            Err(_) => {
                // Key exists on contract but not yet initialized in coordinator
                println!(
                    "{:<8} {:>12} {:>12} {:>12}",
                    nonce, "---", "---", "---"
                );
            }
        }
    }

    Ok(())
}

/// `outlayer keys balance N` — check specific key balance
pub async fn balance(network: &NetworkConfig, nonce: u32) -> Result<()> {
    let creds = config::load_credentials(network)?;
    let api = ApiClient::new(network);

    let balance = api
        .get_payment_key_balance(&creds.account_id, nonce)
        .await?;

    println!("Balance:    {}", format_usd(&balance.available));
    println!("Spent:      {}", format_usd(&balance.spent));
    println!("Reserved:   {}", format_usd(&balance.reserved));
    println!("Initial:    {}", format_usd(&balance.initial_balance));
    if let Some(last_used) = &balance.last_used_at {
        println!("Last used:  {last_used}");
    }

    Ok(())
}

/// The stablecoin the contract itself settles in.
///
/// Asked of the contract rather than configured here: the two must agree, and
/// only one of them is authoritative. A key topped up in the wrong token would
/// be a transfer the contract never credits.
async fn stablecoin_contract(near: &NearClient) -> Result<String> {
    let token: Option<String> = near
        .view_call("get_payment_token_contract", json!({}))
        .await
        .context("Failed to ask the contract which token it settles in")?;
    token.ok_or_else(|| {
        anyhow::anyhow!("This contract has no payment token set, so a key cannot hold a balance")
    })
}

/// `outlayer keys topup N --usd X` — top up with the stablecoin.
///
/// The second half of creating a usable key. The transfer carries
/// `top_up_payment_key` in its `msg`, the contract credits the key, and the
/// worker re-encrypts the blob with the new balance — so the balance appears a
/// moment later, not instantly.
pub async fn topup_usd(network: &NetworkConfig, nonce: u32, amount_usd: f64) -> Result<()> {
    let creds = config::load_credentials(network)?;

    if !(amount_usd > 0.0) {
        anyhow::bail!("Top-up amount must be greater than zero");
    }
    // 6 decimals, the stablecoin's own unit. Rounded rather than truncated so
    // "1.23" is not quietly 1.229999.
    let minimal = (amount_usd * 1_000_000.0).round() as u128;

    let near = NearClient::new(network);
    let token = stablecoin_contract(&near).await?;

    let caller = ContractCaller::from_credentials(&creds, network)?;
    eprintln!("Topping up key nonce {nonce} with ${amount_usd} ({token})...");

    caller
        .call_contract_at(
            &token,
            "ft_transfer_call",
            json!({
                "receiver_id": network.contract_id,
                "amount": minimal.to_string(),
                "msg": json!({ "action": "top_up_payment_key", "nonce": nonce }).to_string(),
            }),
            100_000_000_000_000u64, // 100 TGas
            1,                      // 1 yoctoNEAR, as NEP-141 requires
        )
        .await
        .context("Top-up failed")?;

    eprintln!("Top-up sent. The balance appears once the worker has re-encrypted the key.");
    eprintln!("Check balance: outlayer keys balance {nonce}");

    Ok(())
}

/// `outlayer keys delete N` — delete payment key
pub async fn delete(network: &NetworkConfig, nonce: u32) -> Result<()> {
    let creds = config::load_credentials(network)?;

    let caller = ContractCaller::from_credentials(&creds, network)?;
    let gas = 100_000_000_000_000u64; // 100 TGas

    eprintln!("Deleting payment key nonce {nonce}...");

    caller
        .call_contract(
            "delete_payment_key",
            json!({ "nonce": nonce }),
            gas,
            1, // 1 yoctoNEAR
        )
        .await
        .context("Failed to delete payment key")?;

    eprintln!("Payment key deleted. Storage deposit refunded; the remaining balance is forfeited (`outlayer keys close` would have refunded it).");
    Ok(())
}

/// The chain's ledger of one key's refund, as `get_payment_key_refund_ledger`
/// answers it. `None` for a key created before the ledger that was never
/// topped up or closed since.
#[derive(Debug, serde::Deserialize)]
struct RefundLedgerView {
    user_funded: String,
    refunded: String,
    refundable_cap: String,
    closing_since: Option<u64>,
    claimable_at: Option<u64>,
}

async fn refund_ledger(near: &NearClient, owner: &str, nonce: u32) -> Result<Option<RefundLedgerView>> {
    near.view_call(
        "get_payment_key_refund_ledger",
        json!({ "owner": owner, "nonce": nonce }),
    )
    .await
    .context("Failed to read the key's refund ledger from the contract")
}

/// The refund ledger as the transaction `tx_hash` of `owner` left it.
///
/// A contract call answers once its transaction has executed, two or three
/// blocks before its block is final, and the ledger is read at final finality:
/// read at once, it shows the key as it was before the call. The read waits
/// until the block is final.
async fn refund_ledger_once_final(
    near: &NearClient,
    owner: &str,
    nonce: u32,
    method: &str,
    tx_hash: Option<&str>,
) -> Result<Option<RefundLedgerView>> {
    let tx_hash = tx_hash
        .with_context(|| format!("{method} went through, and no transaction hash came back to wait on"))?;
    near.wait_until_final(tx_hash, owner)
        .await
        .with_context(|| format!("{method} executed in {tx_hash}"))?;
    refund_ledger(near, owner, nonce)
        .await
        .with_context(|| format!("{method} executed in {tx_hash}, and is final"))
}

fn now_ns() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0)
}

/// "in 23h 59m" / "now", for a block timestamp in nanoseconds.
fn until(ns: u64) -> String {
    let now = now_ns();
    if ns <= now {
        return "now".to_string();
    }
    let secs = (ns - now) / 1_000_000_000;
    format!("in {}h {:02}m", secs / 3600, (secs % 3600) / 60)
}

/// `outlayer keys close N` — stop a key and start its refund.
///
/// The key takes no more calls or top-ups from here; what it has left is
/// claimed a day later with `keys claim`, once every call in flight on it has
/// settled. The chain pays at most what THIS account deposited on the key
/// after the refund ledger was released, less what it already refunded — the
/// figure printed before the transaction is sent.
pub async fn close(network: &NetworkConfig, nonce: u32) -> Result<()> {
    let creds = config::load_credentials(network)?;
    let near = NearClient::new(network);
    let caller = ContractCaller::from_credentials(&creds, network)?;

    let ledger = refund_ledger(&near, &creds.account_id, nonce).await?;
    if let Some(l) = &ledger {
        if let (Some(_), Some(at)) = (l.closing_since, l.claimable_at) {
            anyhow::bail!(
                "Payment key {nonce} is already closing. Claim its refund with \
                 `outlayer keys claim {nonce}` ({}).",
                until(at)
            );
        }
    }
    let cap = ledger.as_ref().map(|l| l.refundable_cap.as_str()).unwrap_or("0");

    eprintln!("Closing payment key {nonce}.");
    eprintln!("  From now: no new calls, no top-ups. Calls already running settle normally.");
    if cap == "0" {
        eprintln!("  Nothing on this key is refundable on chain: it was funded before refunds existed, or");
        eprintln!("  by a grant. Closing it deletes it; whatever it can still spend is spent through the key.");
    } else {
        eprintln!("  Refundable on chain: {} — what this account deposited on the key since refunds exist, less refunds.", format_usd(cap));
    }
    eprintln!("  Grants, sponsor credit and trial allowance are never refunded.");
    eprintln!("  Do not close while a NEAR top-up of this key is in progress: the swapped stablecoin");
    eprintln!("  would go back to the swap account, not to you.");
    eprintln!("  In 24 hours: `outlayer keys claim {nonce}` pays it out and deletes the key (one claim a day).");

    let closed = caller
        .call_contract(
            "close_payment_key",
            json!({ "nonce": nonce }),
            100_000_000_000_000u64, // 100 TGas: the call yields for the coordinator
            1,                      // 1 yoctoNEAR
        )
        .await
        .context("Failed to close the payment key")?;

    let after = refund_ledger_once_final(&near, &creds.account_id, nonce, "close_payment_key", closed.tx_hash.as_deref())
        .await
        .with_context(|| {
            format!("Run `outlayer keys close {nonce}` again in a moment: it says whether the key is closing")
        })?;
    match after {
        Some(l) if l.closing_since.is_some() => {
            eprintln!(
                "Payment key {nonce} is closing. Claim its refund {}: outlayer keys claim {nonce}",
                l.claimable_at.map(until).unwrap_or_else(|| "in 24 hours".to_string())
            );
        }
        _ => {
            eprintln!(
                "The transaction went through but the key is not marked closing: the coordinator \
                 did not confirm the close in time. The key is still live; run this again."
            );
        }
    }
    Ok(())
}

/// `outlayer keys claim N` — claim a closed key's refund.
///
/// Accepted a day after the close. The worker asks the coordinator what the
/// key has left; the chain pays at most its own cap of it, deletes the key
/// and returns its storage deposit. A coordinator that still has a call of
/// the key in flight refuses, and the key stays closing: claim again later.
pub async fn claim(network: &NetworkConfig, nonce: u32) -> Result<()> {
    let creds = config::load_credentials(network)?;
    let near = NearClient::new(network);
    let caller = ContractCaller::from_credentials(&creds, network)?;

    let before = refund_ledger(&near, &creds.account_id, nonce).await?;
    let Some(ledger) = before.as_ref().filter(|l| l.closing_since.is_some()) else {
        anyhow::bail!(
            "Payment key {nonce} is not closing. Close it first: outlayer keys close {nonce}"
        );
    };
    if let Some(at) = ledger.claimable_at {
        if at > now_ns() {
            anyhow::bail!(
                "Payment key {nonce} closed too recently: its refund can be claimed {}.",
                until(at)
            );
        }
    }

    eprintln!("Claiming the refund of payment key {nonce}.");
    eprintln!(
        "  Refundable on chain: at most {} (deposited {}, refunded {}).",
        format_usd(&ledger.refundable_cap),
        format_usd(&ledger.user_funded),
        format_usd(&ledger.refunded)
    );
    eprintln!("  The key is deleted and its storage deposit returned; its nonce is not reused.");

    let result = caller
        .call_contract(
            "claim_payment_key_refund",
            json!({ "nonce": nonce }),
            100_000_000_000_000u64, // 100 TGas: the call yields for the coordinator
            1,                      // 1 yoctoNEAR
        )
        .await
        .context("Failed to claim the refund")?;

    let paid = result
        .value
        .as_ref()
        .and_then(|v| v.as_str())
        .unwrap_or("0")
        .to_string();

    if paid != "0" {
        eprintln!("Refunded {} to {}. Payment key {nonce} is deleted.", format_usd(&paid), creds.account_id);
        return Ok(());
    }
    let after = refund_ledger_once_final(&near, &creds.account_id, nonce, "claim_payment_key_refund", result.tx_hash.as_deref())
        .await
        .with_context(|| {
            format!("Nothing was paid. `outlayer keys list` no longer lists key {nonce} once it is deleted")
        })?;
    match after {
        Some(l) if l.closing_since.is_some() => {
            eprintln!(
                "Nothing was paid and the key stays closing: the coordinator still has money of this \
                 key in flight, or did not answer in time. Claim again tomorrow: outlayer keys claim {nonce}"
            );
        }
        _ => {
            eprintln!(
                "Payment key {nonce} is deleted; nothing was refundable on chain (deposits made before \
                 refunds existed, grants and trial credit are spent through the key, not returned)."
            );
        }
    }
    Ok(())
}

fn format_usd(minimal_units: &str) -> String {
    let units: u64 = minimal_units.parse().unwrap_or(0);
    let dollars = units as f64 / 1_000_000.0;
    format!("${:.2}", dollars)
}

/// A scope the coordinator matches: `owner/name` exactly, or `owner/*` for
/// every project of one owner. Anything else would be stored and then match
/// nothing, leaving a key that pays for no project at all.
fn check_project_scope(project: &str) -> Result<()> {
    let (owner, name) = project
        .split_once('/')
        .ok_or_else(|| anyhow::anyhow!("--project {project:?}: expected owner/name, for example alice.near/my-app"))?;
    if owner.is_empty() || name.is_empty() || name.contains('/') || owner.contains(char::is_whitespace) || name.contains(char::is_whitespace) {
        anyhow::bail!("--project {project:?}: expected owner/name, or owner/* for every project of one owner");
    }
    if name.contains('*') && name != "*" {
        anyhow::bail!("--project {project:?}: `*` stands for a whole project name only (owner/*), not a prefix");
    }
    Ok(())
}

#[cfg(test)]
mod project_scope_tests {
    use super::check_project_scope;

    #[test]
    fn a_scope_is_owner_slash_name_or_owner_slash_star() {
        assert!(check_project_scope("zavodil2.testnet/compliance-gateway").is_ok());
        assert!(check_project_scope("alice.near/*").is_ok());
        for bad in ["compliance-gateway", "/app", "alice.near/", "a/b/c", "alice.near/app*", "alice near/app"] {
            assert!(check_project_scope(bad).is_err(), "{bad} must be refused");
        }
    }
}

#[cfg(test)]
mod read_back_tests {
    use super::*;
    use crate::near::fake_rpc::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    const HASH: &str = "11111111111111111111111111111111";

    /// A chain where a call has executed and its block is not final yet: a
    /// view at final shows `before` until a `tx` request has been answered
    /// FINAL (the first one is answered "unknown"), and `after` from then on.
    fn chain(before: serde_json::Value, after: serde_json::Value) -> NearClient {
        let final_seen = Arc::new(AtomicBool::new(false));
        let tx_asked = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let url = serve(move |request| match request["method"].as_str() {
            Some("tx") => {
                if tx_asked.fetch_add(1, Ordering::SeqCst) == 0 {
                    tx_unknown()
                } else {
                    final_seen.store(true, Ordering::SeqCst);
                    tx_final()
                }
            }
            Some("query") => {
                assert_eq!(request["params"]["finality"], "final", "{request}");
                assert_eq!(request["params"]["method_name"], "get_payment_key_refund_ledger", "{request}");
                view_result(if final_seen.load(Ordering::SeqCst) { &after } else { &before })
            }
            other => panic!("unexpected method {other:?}"),
        });
        NearClient::new(&network(&url))
    }

    fn ledger(closing: bool) -> serde_json::Value {
        json!({
            "user_funded": "5000000",
            "refunded": "0",
            "refundable_cap": "5000000",
            "closing_since": if closing { json!(1_700_000_000_000_000_000u64) } else { json!(null) },
            "claimable_at": if closing { json!(1_700_086_400_000_000_000u64) } else { json!(null) },
        })
    }

    /// `keys close` reads the key as closing: the close marks it in its last
    /// receipt, which a read at final sees only once the block is final.
    #[tokio::test]
    async fn a_close_is_read_back_once_its_block_is_final() {
        let near = chain(ledger(false), ledger(true));
        let read = refund_ledger_once_final(&near, "alice.testnet", 7, "close_payment_key", Some(HASH))
            .await
            .expect("read");
        assert!(read.is_some_and(|l| l.closing_since.is_some()), "the key reads as closing");
    }

    /// `keys claim` that paid nothing reads the key as deleted, not as still
    /// closing.
    #[tokio::test]
    async fn a_claim_is_read_back_once_its_block_is_final() {
        let near = chain(ledger(true), json!(null));
        let read = refund_ledger_once_final(&near, "alice.testnet", 7, "claim_payment_key_refund", Some(HASH))
            .await
            .expect("read");
        assert!(read.is_none(), "the key reads as deleted");
    }

    /// A call that came back with no transaction hash is not read as if
    /// nothing had happened.
    #[tokio::test]
    async fn no_hash_is_an_error_that_says_the_call_went_through() {
        let near = chain(ledger(false), ledger(true));
        let e = refund_ledger_once_final(&near, "alice.testnet", 7, "close_payment_key", None)
            .await
            .expect_err("nothing to wait on");
        assert!(format!("{e:#}").contains("close_payment_key went through"), "{e:#}");
    }
}