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

change the type of amount in TransferParams and WithdrawParams from
String to u64

ghassmo 5 лет назад
Родитель
Сommit
23bb4cf04e
6 измененных файлов с 35 добавлено и 66 удалено
  1. 4 6
      src/bin/drk.rs
  2. 27 40
      src/cli/drk_cli.rs
  3. 1 1
      src/cli/mod.rs
  4. 1 2
      src/rpc/adapter.rs
  5. 2 3
      src/rpc/jsonserver.rs
  6. 0 14
      src/rpc/mod.rs

+ 4 - 6
src/bin/drk.rs

@@ -77,15 +77,13 @@ impl Drk {
         Ok(self.request(r).await?)
     }
 
-    // TODO: Should amount be an integer? Also, keep same order in array.
-    pub async fn transfer(&self, address: String, amount: String) -> Result<()> {
-        let r = jsonrpc::request(json!("transfer"), json!([amount, address]));
+    pub async fn transfer(&self, address: String, amount: u64) -> Result<()> {
+        let r = jsonrpc::request(json!("transfer"), json!([address, amount]));
         Ok(self.request(r).await?)
     }
 
-    // TODO: Should amount be an integer? Also, keep same order in array.
-    pub async fn withdraw(&self, address: String, amount: String) -> Result<()> {
-        let r = jsonrpc::request(json!("withdraw"), json!([amount, address]));
+    pub async fn withdraw(&self, address: String, amount: u64) -> Result<()> {
+        let r = jsonrpc::request(json!("withdraw"), json!([address, amount]));
         Ok(self.request(r).await?)
     }
 }

+ 27 - 40
src/cli/drk_cli.rs

@@ -2,29 +2,27 @@
 use crate::Result;
 
 use clap::{App, Arg};
+use serde::Deserialize;
 
-pub struct Transfer {
+fn is_u64<'a>(v: &'a str) -> std::result::Result<(), String> {
+    if v.parse::<u64>().is_ok() {
+        Ok(())
+    } else {
+        Err(String::from("The value is not an integer of type u64"))
+    }
+}
+
+#[derive(Deserialize, Debug)]
+pub struct TransferParams {
     pub pub_key: String,
-    pub amount: String,
+    pub amount: u64,
 }
 
-impl Transfer {
+impl TransferParams {
     pub fn new() -> Self {
         Self {
             pub_key: String::new(),
-            amount: String::new(),
-        }
-    }
-
-    pub fn verify_amount(amount: &str) -> Result<()> {
-        if amount.parse::<u64>().is_ok() || amount.parse::<f64>().is_ok() {
-            Ok(())
-        } else {
-            let err = format!(
-                "Unable to parse input amount as integer or float: {}",
-                amount
-            );
-            Err(crate::Error::ParseFailed(Box::leak(err.into_boxed_str())))
+            amount: 0,
         }
     }
 }
@@ -41,28 +39,17 @@ impl Deposit {
     }
 }
 
-pub struct Withdraw {
+#[derive(Deserialize, Debug)]
+pub struct WithdrawParams {
     pub pub_key: String,
-    pub amount: String,
+    pub amount: u64,
 }
 
-impl Withdraw {
+impl WithdrawParams {
     pub fn new() -> Self {
         Self {
             pub_key: String::new(),
-            amount: String::new(),
-        }
-    }
-
-    pub fn verify_amount(amount: &str) -> Result<()> {
-        if amount.parse::<u64>().is_ok() || amount.parse::<f64>().is_ok() {
-            Ok(())
-        } else {
-            let err = format!(
-                "Unable to parse input amount as integer or float: {}",
-                amount
-            );
-            Err(crate::Error::ParseFailed(Box::leak(err.into_boxed_str())))
+            amount: 0,
         }
     }
 }
@@ -76,9 +63,9 @@ pub struct DrkCli {
     pub info: bool,
     pub hello: bool,
     pub stop: bool,
-    pub transfer: Option<Transfer>,
+    pub transfer: Option<TransferParams>,
     pub deposit: Option<Deposit>,
-    pub withdraw: Option<Withdraw>,
+    pub withdraw: Option<WithdrawParams>,
 }
 
 impl DrkCli {
@@ -151,6 +138,7 @@ impl DrkCli {
                             .value_name("AMOUNT")
                             .takes_value(true)
                             .index(2)
+                            .validator(is_u64)
                             .help_heading(Some("Amount to send, in DBTC"))
                             .required(true),
                     ),
@@ -172,6 +160,7 @@ impl DrkCli {
                             .value_name("AMOUNT")
                             .takes_value(true)
                             .index(2)
+                            .validator(is_u64)
                             .help_heading(Some("Amount to send, in BTC"))
                             .required(true),
                     ),
@@ -222,13 +211,12 @@ impl DrkCli {
         let mut transfer = None;
         match app.subcommand_matches("transfer") {
             Some(transfer_sub) => {
-                let mut trn = Transfer::new();
+                let mut trn = TransferParams::new();
                 if let Some(address) = transfer_sub.value_of("address") {
                     trn.pub_key = address.to_string();
                 }
                 if let Some(amount) = transfer_sub.value_of("amount") {
-                    Transfer::verify_amount(amount)?;
-                    trn.amount = amount.to_string();
+                    trn.amount = amount.parse()?;
                 }
                 transfer = Some(trn);
             }
@@ -238,13 +226,12 @@ impl DrkCli {
         let mut withdraw = None;
         match app.subcommand_matches("withdraw") {
             Some(withdraw_sub) => {
-                let mut wdraw = Withdraw::new();
+                let mut wdraw = WithdrawParams::new();
                 if let Some(address) = withdraw_sub.value_of("address") {
                     wdraw.pub_key = address.to_string();
                 }
                 if let Some(amount) = withdraw_sub.value_of("amount") {
-                    Transfer::verify_amount(amount)?;
-                    wdraw.amount = amount.to_string();
+                    wdraw.amount = amount.parse()?;
                 }
                 withdraw = Some(wdraw);
             }

+ 1 - 1
src/cli/mod.rs

@@ -8,5 +8,5 @@ pub use cashierd_cli::CashierdCli;
 pub use cli_config::{CashierdConfig, DarkfidConfig, DrkConfig, GatewaydConfig, Config};
 pub use darkfid_cli::DarkfidCli;
 pub use drk_cli::DrkCli;
-pub use drk_cli::Transfer;
+pub use drk_cli::{TransferParams, WithdrawParams};
 pub use gatewayd_cli::GatewaydCli;

+ 1 - 2
src/rpc/adapter.rs

@@ -2,8 +2,7 @@ use crate::service::btc::PubAddress;
 use crate::service::cashier::CashierClient;
 use crate::wallet::WalletDb;
 use crate::{Error, Result};
-
-use super::TransferParams;
+use crate::cli::TransferParams;
 
 use log::*;
 

+ 2 - 3
src/rpc/jsonserver.rs

@@ -1,8 +1,7 @@
 use crate::cli::DarkfidConfig;
 use crate::rpc::adapter::RpcAdapter;
 use crate::{Error, Result};
-
-use super::{TransferParams, WithdrawParams};
+use crate::cli::{TransferParams, WithdrawParams};
 
 use async_executor::Executor;
 use async_native_tls::TlsAcceptor;
@@ -250,7 +249,7 @@ impl RpcInterface {
             let self2 = self1.clone();
             async move {
                 let parsed: TransferParams = params.parse().unwrap();
-                let address = parsed.address.clone();
+                let address = parsed.pub_key.clone();
                 self2.adapter.transfer(parsed).await?;
                 Ok(jsonrpc_core::Value::String(format!("Transfer To: {}", address)))
             }

+ 0 - 14
src/rpc/mod.rs

@@ -3,18 +3,4 @@ pub mod jsonrpc;
 pub mod jsonserver;
 pub mod test;
 
-use serde::Deserialize;
-
-#[derive(Deserialize, Debug)]
-pub struct TransferParams {
-    pub address: String,
-    pub amount: String,
-}
-
-#[derive(Deserialize, Debug)]
-pub struct WithdrawParams {
-    address: String,
-    amount: String,
-}
-
 pub use adapter::{AdapterPtr, RpcAdapter};