瀏覽代碼

darkfid: check the network when receive get_token_id request

ghassmo 4 年之前
父節點
當前提交
164757f17b
共有 1 個文件被更改,包括 32 次插入14 次删除
  1. 32 14
      src/bin/darkfid.rs

+ 32 - 14
src/bin/darkfid.rs

@@ -1,11 +1,3 @@
-use async_trait::async_trait;
-use clap::clap_app;
-use log::debug;
-use serde_json::{json, Value};
-
-use async_std::sync::{Arc, Mutex};
-use std::path::PathBuf;
-
 use drk::{
 use drk::{
     blockchain::Rocks,
     blockchain::Rocks,
     cli::{Config, DarkfidConfig},
     cli::{Config, DarkfidConfig},
@@ -18,12 +10,21 @@ use drk::{
     serial::{deserialize, serialize},
     serial::{deserialize, serialize},
     util::{
     util::{
         assign_id, decimals, decode_base10, expand_path, join_config_path, DrkTokenList,
         assign_id, decimals, decode_base10, expand_path, join_config_path, DrkTokenList,
-        SolTokenList,
+        NetworkName, SolTokenList,
     },
     },
     wallet::WalletDb,
     wallet::WalletDb,
-    Result,
+    Error, Result,
 };
 };
 
 
+use async_trait::async_trait;
+use clap::clap_app;
+use log::debug;
+use serde_json::{json, Value};
+
+use async_std::sync::{Arc, Mutex};
+use std::path::PathBuf;
+use std::str::FromStr;
+
 struct Darkfid {
 struct Darkfid {
     config: DarkfidConfig,
     config: DarkfidConfig,
     client: Arc<Mutex<Client>>,
     client: Arc<Mutex<Client>>,
@@ -131,7 +132,7 @@ impl Darkfid {
         return JsonResult::Resp(jsonresp(json!(b58), id));
         return JsonResult::Resp(jsonresp(json!(b58), id));
     }
     }
 
 
-    // --> {"method": "get_token_id", "params": [token]}
+    // --> {"method": "get_token_id", "params": [network, token]}
     // <-- {"result": "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"}
     // <-- {"result": "Ht5G1RhkcKnpLVLMhqJc5aqZ4wYUEbxbtZwGCVbgU7DL"}
     async fn get_token_id(&self, id: Value, params: Value) -> JsonResult {
     async fn get_token_id(&self, id: Value, params: Value) -> JsonResult {
         let args = params.as_array();
         let args = params.as_array();
@@ -141,7 +142,13 @@ impl Darkfid {
         }
         }
 
 
         let args = args.unwrap();
         let args = args.unwrap();
-        let symbol = args[0].as_str();
+
+        let network = args[0].as_str();
+        let symbol = args[1].as_str();
+
+        if network.is_none() {
+            return JsonResult::Err(jsonerr(InvalidNetworkParam, None, id));
+        }
 
 
         if symbol.is_none() {
         if symbol.is_none() {
             return JsonResult::Err(jsonerr(InvalidSymbolParam, None, id));
             return JsonResult::Err(jsonerr(InvalidSymbolParam, None, id));
@@ -149,8 +156,19 @@ impl Darkfid {
         let symbol = symbol.unwrap();
         let symbol = symbol.unwrap();
 
 
         let result: Result<Value> = async {
         let result: Result<Value> = async {
-            let token_id = self.sol_tokenlist.clone().search_id(symbol)?;
-            Ok(json!(token_id))
+            let network = NetworkName::from_str(&network.unwrap())?;
+            match network {
+                #[cfg(feature = "sol")]
+                NetworkName::Solana => {
+                    let token_id = self.sol_tokenlist.clone().search_id(symbol)?;
+                    Ok(json!(token_id))
+                }
+                #[cfg(feature = "btc")]
+                NetworkName::Bitcoin => {
+                    return Err(Error::NotSupportedToken);
+                }
+                _ => Err(Error::NotSupportedNetwork),
+            }
         }
         }
         .await;
         .await;