Skip to content

Commit

Permalink
implement Show for customer #133
Browse files Browse the repository at this point in the history
  • Loading branch information
marsella committed Jan 31, 2022
1 parent 021cc68 commit 2538fd6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bin/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn main_with_cli(cli: Cli) -> Result<(), anyhow::Error> {
tokio::task::spawn_blocking(|| Ok(edit::edit_file(config_path)?)).await?
}
List(list) => list.run(rng, config.await?).await,
// Show(show) => show.run(rng, config.await?).await,
Show(show) => show.run(rng, config.await?).await,
Rename(rename) => rename.run(rng, config.await?).await,
Establish(establish) => establish.run(rng, config.await?).await,
Pay(pay) => pay.run(rng, config.await?).await,
Expand Down
6 changes: 3 additions & 3 deletions src/cli/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Cli {
#[derive(Debug, StructOpt)]
pub enum Customer {
List(List),
// Show(Show),
Show(Show),
Configure(Configure),
Rename(Rename),
Establish(Establish),
Expand All @@ -49,8 +49,8 @@ pub struct List {
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Show {
/// The channel ID, or a unique prefix of it.
pub prefix: String,
/// The channel label.
pub label: ChannelName,

/// Get json output.
#[structopt(long)]
Expand Down
2 changes: 2 additions & 0 deletions src/zkchannels/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod manage;
mod pay;
mod watch;

pub use manage::PublicChannelDetails;

/// A single customer-side command, parameterized by the currently loaded configuration.
///
/// All subcommands of [`cli::Customer`] should implement this, except [`Configure`], which does not need
Expand Down
64 changes: 59 additions & 5 deletions src/zkchannels/customer/manage.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
use {
anyhow::Context,
async_trait::async_trait,
comfy_table::{Cell, Table},
rand::rngs::StdRng,
serde::{Deserialize, Serialize},
serde_json::json,
serde_with::{serde_as, DisplayFromStr},
tracing::info,
zkabacus_crypto::{ChannelId, CustomerBalance, MerchantBalance},
};

use crate::{
amount::Amount,
customer::{
cli::{List, Rename},
Config,
cli::{List, Rename, Show},
ChannelName, Config,
},
database::customer::{ChannelDetails, StateName},
escrow::types::ContractId,
};

use super::{database, Command};
use anyhow::Context;
use serde_json::json;

/// The contents of a row of the database for a particular channel that are suitable to share with
/// the user.
///
/// This should be a subset of the [`ChannelDetails`].
#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
pub struct PublicChannelDetails {
label: ChannelName,
state: StateName,
customer_balance: CustomerBalance,
merchant_balance: MerchantBalance,
#[serde_as(as = "DisplayFromStr")]
channel_id: ChannelId,
contract_id: Option<ContractId>,
}

impl From<ChannelDetails> for PublicChannelDetails {
fn from(details: ChannelDetails) -> Self {
PublicChannelDetails {
label: details.label,
state: details.state.state_name(),
customer_balance: details.state.customer_balance(),
merchant_balance: details.state.merchant_balance(),
channel_id: *details.state.channel_id(),
contract_id: details.contract_details.contract_id,
}
}
}

#[async_trait]
impl Command for Show {
async fn run(self, _rng: StdRng, config: self::Config) -> Result<(), anyhow::Error> {
let database = database(&config)
.await
.context("Failed to connect to local database")?;
let channel_details = database.get_channel(&self.label).await?;

if self.json {
let j = serde_json::to_string(&PublicChannelDetails::from(channel_details))?;
info!("{}", j);
println!("{}", j);
} else {
todo!("non-JSON show is not yet implemented")
}
Ok(())
}
}

#[async_trait]
impl Command for List {
Expand All @@ -33,7 +87,7 @@ impl Command for List {
"balance": format!("{}", Amount::from(details.state.customer_balance())),
"max_refund": format!("{}", Amount::from(details.state.merchant_balance())),
"channel_id": format!("{}", details.state.channel_id()),
"contract_id": details.contract_details.contract_id.map_or_else(|| "N/A".to_string(), |contract_id| format!("{}", contract_id))
"contract_id": details.contract_details.contract_id.map_or_else(|| "N/A".to_string(), |contract_id| format!("{}", contract_id)),
}));
}
println!("{}", json!(output).to_string());
Expand Down

0 comments on commit 2538fd6

Please sign in to comment.