From fb317864e6bdf6a791cb6fc0e1539f57db15c0dd Mon Sep 17 00:00:00 2001 From: cyberhumble Date: Fri, 7 Feb 2025 22:56:17 +0300 Subject: [PATCH] feature#389: [apps] Pretty print the fidelity bonds #389 --- src/wallet/fidelity.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/wallet/fidelity.rs b/src/wallet/fidelity.rs index 18d55e06..75ef1b4c 100644 --- a/src/wallet/fidelity.rs +++ b/src/wallet/fidelity.rs @@ -167,6 +167,37 @@ impl Wallet { &self.store.fidelity_bond } + pub fn display_fidelity_bonds(&self) -> Result<(), WalletError> { + let blockchain_info = self.rpc.get_blockchain_info()?; + let mtp = blockchain_info.median_time as u32; + + let serialized: Vec = self + .get_fidelity_bonds() + .iter() + .map(|(index, (bond, _, _))| { + let expires_in = match bond.lock_time { + LockTime::Blocks(height) => height.to_consensus_u32() - bond.conf_height, + LockTime::Seconds(time) => (time.to_consensus_u32() + 1_u32 - mtp) / 600, + }; + self.calculate_bond_value(index.to_owned()) + .map(|bond_value| { + serde_json::json!({ + "outpoint": bond.outpoint.to_string(), + "amount": bond.amount.to_sat(), + "bond-value:": bond_value, + "expires-in": expires_in, + }) + }) + }) + .collect::, WalletError>>()?; + + let pretty_json = serde_json::to_string_pretty(&serialized) + .map_err(|e| WalletError::General(e.to_string()))?; + + println!("{}", pretty_json); + Ok(()) + } + /// Get the highest value fidelity bond. Returns None, if no bond exists. pub fn get_highest_fidelity_index(&self) -> Result, WalletError> { Ok(self