mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-06 13:34:38 -04:00
feat: enhance history output for cli
This commit is contained in:
parent
5d31c27890
commit
2ad3d6c4b2
3 changed files with 81 additions and 9 deletions
|
@ -8,9 +8,12 @@ use crate::protocol::bob::{BobState, Swap};
|
||||||
use crate::protocol::{bob, State};
|
use crate::protocol::{bob, State};
|
||||||
use crate::{bitcoin, cli, monero, rpc};
|
use crate::{bitcoin, cli, monero, rpc};
|
||||||
use anyhow::{bail, Context as AnyContext, Result};
|
use anyhow::{bail, Context as AnyContext, Result};
|
||||||
|
use comfy_table::Table;
|
||||||
use libp2p::core::Multiaddr;
|
use libp2p::core::Multiaddr;
|
||||||
use qrcode::render::unicode;
|
use qrcode::render::unicode;
|
||||||
use qrcode::QrCode;
|
use qrcode::QrCode;
|
||||||
|
use rust_decimal::prelude::FromPrimitive;
|
||||||
|
use rust_decimal::Decimal;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
@ -619,14 +622,83 @@ impl Request {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Method::History => {
|
Method::History => {
|
||||||
let swaps = context.db.all().await?;
|
let mut table = Table::new();
|
||||||
let mut vec: Vec<(Uuid, String)> = Vec::new();
|
table.set_header(vec![
|
||||||
for (swap_id, state) in swaps {
|
"Swap ID",
|
||||||
let state: BobState = state.try_into()?;
|
"Start Date",
|
||||||
vec.push((swap_id, state.to_string()));
|
"State",
|
||||||
|
"BTC Amount",
|
||||||
|
"XMR Amount",
|
||||||
|
"Exchange Rate",
|
||||||
|
"Trading Partner Peer ID",
|
||||||
|
]);
|
||||||
|
|
||||||
|
let all_swaps = context.db.all().await?;
|
||||||
|
let mut json_results = Vec::new();
|
||||||
|
|
||||||
|
for (swap_id, state) in all_swaps {
|
||||||
|
let result: Result<_> = async {
|
||||||
|
let latest_state: BobState = state.try_into()?;
|
||||||
|
let all_states = context.db.get_states(swap_id).await?;
|
||||||
|
let state3 = all_states
|
||||||
|
.iter()
|
||||||
|
.find_map(|s| if let State::Bob(BobState::BtcLocked { state3, .. }) = s { Some(state3) } else { None })
|
||||||
|
.context("Failed to get \"BtcLocked\" state")?;
|
||||||
|
|
||||||
|
let swap_start_date = context.db.get_swap_start_date(swap_id).await?;
|
||||||
|
let peer_id = context.db.get_peer_id(swap_id).await?;
|
||||||
|
let btc_amount = state3.tx_lock.lock_amount();
|
||||||
|
let xmr_amount = state3.xmr;
|
||||||
|
let exchange_rate = (Decimal::from_f64(btc_amount.to_btc()).unwrap() / xmr_amount.as_xmr()).round_dp(8);
|
||||||
|
let exchange_rate_str = format!("{} XMR/BTC", exchange_rate);
|
||||||
|
|
||||||
|
let swap_data = json!({
|
||||||
|
"swap_id": swap_id.to_string(),
|
||||||
|
"start_date": swap_start_date.to_string(),
|
||||||
|
"state": latest_state.to_string(),
|
||||||
|
"btc_amount": btc_amount.to_string(),
|
||||||
|
"xmr_amount": xmr_amount.to_string(),
|
||||||
|
"exchange_rate": exchange_rate_str,
|
||||||
|
"trading_partner_peer_id": peer_id.to_string()
|
||||||
|
});
|
||||||
|
|
||||||
|
if context.config.json {
|
||||||
|
tracing::info!(
|
||||||
|
swap_id = %swap_id,
|
||||||
|
swap_start_date = %swap_start_date,
|
||||||
|
latest_state = %latest_state,
|
||||||
|
btc_amount = %btc_amount,
|
||||||
|
xmr_amount = %xmr_amount,
|
||||||
|
exchange_rate = %exchange_rate_str,
|
||||||
|
trading_partner_peer_id = %peer_id,
|
||||||
|
"Found swap in database"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
table.add_row(vec![
|
||||||
|
swap_id.to_string(),
|
||||||
|
swap_start_date.to_string(),
|
||||||
|
latest_state.to_string(),
|
||||||
|
btc_amount.to_string(),
|
||||||
|
xmr_amount.to_string(),
|
||||||
|
exchange_rate_str,
|
||||||
|
peer_id.to_string(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(swap_data)
|
||||||
|
}.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(swap_data) => json_results.push(swap_data),
|
||||||
|
Err(e) => tracing::error!(swap_id = %swap_id, error = ?e, "Failed to get swap details"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(json!({ "swaps": vec }))
|
if !context.config.json {
|
||||||
|
println!("{}", table);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({"swaps": json_results}))
|
||||||
}
|
}
|
||||||
Method::GetRawStates => {
|
Method::GetRawStates => {
|
||||||
let raw_history = context.db.raw_all().await?;
|
let raw_history = context.db.raw_all().await?;
|
||||||
|
|
|
@ -230,8 +230,8 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
Command::History { only_unfinished } => {
|
Command::History { only_unfinished } => {
|
||||||
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadOnly).await?;
|
let db = open_db(config.data.dir.join("sqlite"), AccessMode::ReadOnly).await?;
|
||||||
|
|
||||||
let mut table = Table::new();
|
let mut table = Table::new();
|
||||||
|
|
||||||
table.set_header(vec![
|
table.set_header(vec![
|
||||||
"SWAP ID",
|
"SWAP ID",
|
||||||
"START DATETIME",
|
"START DATETIME",
|
||||||
|
|
|
@ -369,7 +369,7 @@ pub struct State3 {
|
||||||
S_a_monero: monero::PublicKey,
|
S_a_monero: monero::PublicKey,
|
||||||
S_a_bitcoin: bitcoin::PublicKey,
|
S_a_bitcoin: bitcoin::PublicKey,
|
||||||
v: monero::PrivateViewKey,
|
v: monero::PrivateViewKey,
|
||||||
xmr: monero::Amount,
|
pub xmr: monero::Amount,
|
||||||
pub cancel_timelock: CancelTimelock,
|
pub cancel_timelock: CancelTimelock,
|
||||||
punish_timelock: PunishTimelock,
|
punish_timelock: PunishTimelock,
|
||||||
refund_address: bitcoin::Address,
|
refund_address: bitcoin::Address,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue