change the type of amount in TransferParams and WithdrawParams from

String to u64
This commit is contained in:
ghassmo
2021-08-04 02:05:10 +03:00
parent 7a4b4cf9c4
commit 23bb4cf04e
6 changed files with 36 additions and 67 deletions

View File

@@ -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?)
}
}

View File

@@ -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::<u64>().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::<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);
}

View File

@@ -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;

View File

@@ -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::*;

View File

@@ -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)))
}

View File

@@ -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};