diff --git a/src/bin/drk.rs b/src/bin/drk.rs index 626c959f0..8dc4686d4 100644 --- a/src/bin/drk.rs +++ b/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?) } } diff --git a/src/cli/drk_cli.rs b/src/cli/drk_cli.rs index 300d222cc..51771ecc3 100644 --- a/src/cli/drk_cli.rs +++ b/src/cli/drk_cli.rs @@ -2,29 +2,27 @@ use crate::Result; use clap::{App, Arg}; +use serde::Deserialize; -pub struct Transfer { - pub pub_key: String, - pub amount: String, +fn is_u64<'a>(v: &'a str) -> std::result::Result<(), String> { + if v.parse::().is_ok() { + Ok(()) + } else { + Err(String::from("The value is not an integer of type u64")) + } } -impl Transfer { +#[derive(Deserialize, Debug)] +pub struct TransferParams { + pub pub_key: String, + pub amount: u64, +} + +impl TransferParams { pub fn new() -> Self { Self { pub_key: String::new(), - amount: String::new(), - } - } - - pub fn verify_amount(amount: &str) -> Result<()> { - if amount.parse::().is_ok() || amount.parse::().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::().is_ok() || amount.parse::().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, + pub transfer: Option, pub deposit: Option, - pub withdraw: Option, + pub withdraw: Option, } 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); } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index f019efcd1..63b98e3ba 100644 --- a/src/cli/mod.rs +++ b/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; diff --git a/src/rpc/adapter.rs b/src/rpc/adapter.rs index 7006fa8c0..07b537e10 100644 --- a/src/rpc/adapter.rs +++ b/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::*; diff --git a/src/rpc/jsonserver.rs b/src/rpc/jsonserver.rs index e5a184759..95fb34f54 100644 --- a/src/rpc/jsonserver.rs +++ b/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))) } diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index a6a9d1577..3a4aafa5a 100644 --- a/src/rpc/mod.rs +++ b/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};