Skip to main content

fedimint_mintv2_client/
cli.rs

1use std::{ffi, iter};
2
3use clap::Parser;
4use fedimint_core::Amount;
5use fedimint_core::base32::{self, FEDIMINT_PREFIX};
6use serde::Serialize;
7use serde_json::Value;
8
9use crate::MintClientModule;
10
11#[derive(Parser, Serialize)]
12enum Opts {
13    /// Count the `ECash` notes in the client's database by denomination.
14    Count,
15    /// Send `ECash` for the given amount.
16    Send { amount: Amount },
17    /// Receive the `ECash` by reissuing the notes and return the amount.
18    Receive { ecash: String },
19}
20
21pub(crate) async fn handle_cli_command(
22    mint: &MintClientModule,
23    args: &[ffi::OsString],
24) -> anyhow::Result<Value> {
25    let opts = Opts::parse_from(iter::once(&ffi::OsString::from("mintv2")).chain(args.iter()));
26
27    match opts {
28        Opts::Count => Ok(json(mint.get_count_by_denomination().await)),
29        Opts::Send { amount } => {
30            let ecash = mint
31                .send(amount, Value::Null)
32                .await
33                .map(|ecash| base32::encode_prefixed(FEDIMINT_PREFIX, &ecash))?;
34
35            Ok(json(ecash))
36        }
37        Opts::Receive { ecash } => {
38            let ecash = base32::decode_prefixed(FEDIMINT_PREFIX, &ecash)?;
39
40            let operation_id = mint.receive(ecash, Value::Null).await?;
41
42            let state = mint.await_final_receive_operation_state(operation_id).await;
43
44            Ok(json(state))
45        }
46    }
47}
48
49fn json<T: Serialize>(value: T) -> Value {
50    serde_json::to_value(value).expect("JSON serialization failed")
51}