Просмотр исходного кода

bin/dao: balance() replace token name with token_id

Dastan-glitch 3 лет назад
Родитель
Сommit
7a7c5070f2
2 измененных файлов с 12 добавлено и 21 удалено
  1. 8 19
      bin/dao/daod/src/main.rs
  2. 4 2
      bin/dao/daod/src/rpc.rs

+ 8 - 19
bin/dao/daod/src/main.rs

@@ -1,7 +1,7 @@
 use std::{sync::Arc, time::Instant};
 
-use darkfi_sdk::crypto::MerkleNode;
 use fxhash::FxHashMap;
+use group::ff::PrimeField;
 use incrementalmerkletree::{Position, Tree};
 use log::debug;
 use pasta_curves::{
@@ -25,6 +25,7 @@ use darkfi::{
     zkas::ZkBinary,
     Error, Result,
 };
+use darkfi_sdk::crypto::MerkleNode;
 
 mod contract;
 mod error;
@@ -315,7 +316,6 @@ impl Client {
         Ok(())
     }
 
-    // TODO: Change these into errors instead of expects.
     fn validate(&mut self, tx: &Transaction) -> DaoResult<()> {
         debug!(target: "dao_demo::client::validate()", "commencing validate sequence");
         let mut updates = vec![];
@@ -624,18 +624,13 @@ impl DaoWallet {
         Ok(())
     }
 
-    // TODO: Make this a HashMap<TokenID, u64>
-    // only parse it to a String in the cli.
     fn balances(&self) -> Result<FxHashMap<String, u64>> {
         let mut ret: FxHashMap<String, u64> = FxHashMap::default();
         for (coin, is_spent) in &self.own_coins {
             if *is_spent {}
-            if coin.note.token_id == *DRK_ID {
-                let id = "DRK".to_owned();
-                ret.insert(id, coin.note.value);
-            } else if coin.note.token_id == *GOV_ID {
-                let id = "GOV".to_owned();
-                ret.insert(id, coin.note.value);
+            if coin.note.token_id == *DRK_ID || coin.note.token_id == *GOV_ID {
+                let token_id = bs58::encode(coin.note.token_id.to_repr()).into_string();
+                ret.insert(token_id, coin.note.value);
             }
         }
         Ok(ret)
@@ -690,7 +685,6 @@ impl DaoWallet {
         &self.vote_notes
     }
 
-    // TODO: Explicit error handling.
     fn get_treasury_path(
         &self,
         own_coin: &OwnCoin,
@@ -889,18 +883,13 @@ impl MoneyWallet {
         Ok(())
     }
 
-    // TODO: Make this a HashMap<TokenID, u64>
-    // only parse it to a String in the cli.
     fn balances(&self) -> Result<FxHashMap<String, u64>> {
         let mut ret: FxHashMap<String, u64> = FxHashMap::default();
         for (coin, is_spent) in &self.own_coins {
             if *is_spent {}
-            if coin.note.token_id == *DRK_ID {
-                let id = "DRK".to_owned();
-                ret.insert(id, coin.note.value);
-            } else if coin.note.token_id == *GOV_ID {
-                let id = "GOV".to_owned();
-                ret.insert(id, coin.note.value);
+            if coin.note.token_id == *DRK_ID || coin.note.token_id == *GOV_ID {
+                let token_id = bs58::encode(coin.note.token_id.to_repr()).into_string();
+                ret.insert(token_id, coin.note.value);
             }
         }
         Ok(ret)

+ 4 - 2
bin/dao/daod/src/rpc.rs

@@ -352,8 +352,9 @@ impl JsonRpcInterface {
             Ok(key) => match client.money_wallets.get(&key) {
                 Some(wallet) => {
                     let balance = wallet.balances().unwrap();
-                    if balance.get("GOV").is_some() {
-                        *balance.get("GOV").unwrap()
+                    let token_id = bs58::encode((*GOV_ID).to_repr()).into_string();
+                    if balance.get(&token_id).is_some() {
+                        *balance.get(&token_id).unwrap()
                     } else {
                         error!("Balance is empty");
                         0
@@ -418,6 +419,7 @@ impl JsonRpcInterface {
             Ok(bulla) => match client.exec_proposal(bulla) {
                 Ok(_) => JsonResponse::new(json!("Proposal executed successfully."), id).into(),
                 Err(e) => {
+                    // Reject proposal instead of returning error?
                     error!("Failed executing proposal: {}", e);
                     return server_error(RpcError::Exec, id)
                 }