Sfoglia il codice sorgente

drk: display aliases in all tables

aggstam 3 anni fa
parent
commit
e052633c94
2 ha cambiato i file con 53 aggiunte e 5 eliminazioni
  1. 36 5
      bin/drk/src/main.rs
  2. 17 0
      bin/drk/src/wallet_money.rs

+ 36 - 5
bin/drk/src/main.rs

@@ -457,13 +457,23 @@ async fn main() -> Result<()> {
                 let balmap =
                     drk.money_balance().await.with_context(|| "Failed to fetch wallet balance")?;
 
+                let aliases_map = drk
+                    .get_aliases_mapped_by_token()
+                    .await
+                    .with_context(|| "Failed to fetch wallet aliases")?;
+
                 // Create a prettytable with the new data:
                 let mut table = Table::new();
                 table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
-                table.set_titles(row!["Token ID", "Balance"]);
+                table.set_titles(row!["Token ID", "Aliases", "Balance"]);
                 for (token_id, balance) in balmap.iter() {
+                    let aliases = match aliases_map.get(token_id) {
+                        Some(a) => a,
+                        None => "-",
+                    };
+
                     // FIXME: Don't hardcode to 8 decimals
-                    table.add_row(row![token_id, encode_base10(*balance, 8)]);
+                    table.add_row(row![token_id, aliases, encode_base10(*balance, 8)]);
                 }
 
                 if table.is_empty() {
@@ -545,6 +555,11 @@ async fn main() -> Result<()> {
                     .await
                     .with_context(|| "Failed to fetch coins from wallet")?;
 
+                let aliases_map = drk
+                    .get_aliases_mapped_by_token()
+                    .await
+                    .with_context(|| "Failed to fetch wallet aliases")?;
+
                 drk.rpc_client.close().await?;
 
                 if coins.is_empty() {
@@ -553,12 +568,18 @@ async fn main() -> Result<()> {
 
                 let mut table = Table::new();
                 table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
-                table.set_titles(row!["Coin", "Spent", "Token ID", "Value"]);
+                table.set_titles(row!["Coin", "Spent", "Token ID", "Aliases", "Value"]);
                 for coin in coins {
+                    let aliases = match aliases_map.get(&coin.0.note.token_id.to_string()) {
+                        Some(a) => a,
+                        None => "-",
+                    };
+
                     table.add_row(row![
                         format!("{}", bs58::encode(&serialize(&coin.0.coin.inner())).into_string()),
                         coin.1,
                         coin.0.note.token_id,
+                        aliases,
                         format!("{} ({})", coin.0.note.value, encode_base10(coin.0.note.value, 8))
                     ]);
                 }
@@ -826,13 +847,23 @@ async fn main() -> Result<()> {
                 let balmap =
                     drk.dao_balance(dao_id).await.with_context(|| "Failed to fetch DAO balance")?;
 
+                let aliases_map = drk
+                    .get_aliases_mapped_by_token()
+                    .await
+                    .with_context(|| "Failed to fetch wallet aliases")?;
+
                 // Create a prettytable with the new data:
                 let mut table = Table::new();
                 table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
-                table.set_titles(row!["Token ID", "Balance"]);
+                table.set_titles(row!["Token ID", "Aliases", "Balance"]);
                 for (token_id, balance) in balmap.iter() {
+                    let aliases = match aliases_map.get(token_id) {
+                        Some(a) => a,
+                        None => "-",
+                    };
+
                     // FIXME: Don't hardcode to 8 decimals
-                    table.add_row(row![token_id, encode_base10(*balance, 8)]);
+                    table.add_row(row![token_id, aliases, encode_base10(*balance, 8)]);
                 }
 
                 if table.is_empty() {

+ 17 - 0
bin/drk/src/wallet_money.rs

@@ -719,6 +719,23 @@ impl Drk {
         Ok(map)
     }
 
+    /// Fetch all aliases from the wallet, mapped by token id.
+    pub async fn get_aliases_mapped_by_token(&self) -> Result<HashMap<String, String>> {
+        let aliases = self.get_aliases(None, None).await?;
+        let mut map: HashMap<String, String> = HashMap::new();
+        for (alias, token_id) in aliases {
+            let aliases_string = if let Some(prev) = map.get(&token_id.to_string()) {
+                format!("{}, {}", prev, alias)
+            } else {
+                alias
+            };
+
+            map.insert(token_id.to_string(), aliases_string);
+        }
+
+        Ok(map)
+    }
+
     /// Create an alias record for provided Token ID
     pub async fn remove_alias(&self, alias: String) -> Result<()> {
         eprintln!("Removing alias: {}", alias);