added 'withdraw' option to cli/ rpc

This commit is contained in:
lunar-mining
2021-07-21 07:18:12 +02:00
parent 23dc8547cd
commit d653f5ecbd
3 changed files with 95 additions and 1 deletions

View File

@@ -90,6 +90,19 @@ impl Drk {
self.request().await
}
pub async fn withdraw(&mut self, address: String, amount: String) -> Result<()> {
let mut params = Map::new();
params.insert("amount".into(), Value::String(amount));
params.insert("address".into(), Value::String(address));
self.payload
.insert(String::from("method"), Value::String("withdraw".into()));
self.payload
.insert(String::from("params"), Value::Object(params));
self.request().await
}
async fn request(&self) -> Result<()> {
let payload = surf::Body::from_json(&self.payload)?;
let payload = payload.into_string().await?;
@@ -137,6 +150,10 @@ async fn start(config: &DrkConfig, options: DrkCli) -> Result<()> {
client.deposit().await?;
}
if let Some(withdraw) = options.withdraw {
client.withdraw(withdraw.pub_key, withdraw.amount).await?;
}
if options.stop {
client.stop().await?;
}

View File

@@ -40,6 +40,33 @@ impl Deposit {
}
}
}
pub struct Withdraw {
pub pub_key: String,
pub amount: String,
}
impl Withdraw {
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())))
}
}
}
pub struct DrkCli {
//pub change_config: bool,
pub verbose: bool,
@@ -51,6 +78,7 @@ pub struct DrkCli {
pub stop: bool,
pub transfer: Option<Transfer>,
pub deposit: Option<Deposit>,
pub withdraw: Option<Withdraw>,
}
impl DrkCli {
@@ -112,7 +140,7 @@ impl DrkCli {
.about("Transfer DBTC between users")
.arg(
Arg::new("address")
.value_name("RECIPIENT_ADDRESS")
.value_name("RECEIVE_ADDRESS")
.takes_value(true)
.index(1)
.help_heading(Some("Address of recipient"))
@@ -128,6 +156,27 @@ impl DrkCli {
),
)
.subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
.subcommand(
App::new("withdraw")
.about("Withdraw BTC for dBTC")
.arg(
Arg::new("address")
.value_name("RECEIVE_ADDRESS")
.takes_value(true)
.index(1)
.help_heading(Some("Address of recipient"))
.required(true),
)
.arg(
Arg::new("amount")
.value_name("AMOUNT")
.takes_value(true)
.index(2)
.help_heading(Some("Amount to send, in BTC"))
.required(true),
),
)
.subcommand(App::new("deposit").about("Deposit BTC for dBTC"))
//.subcommand(
// App::new("config")
// .about("Configuration settings")
@@ -186,6 +235,21 @@ impl DrkCli {
None => {}
}
let mut withdraw = None;
match app.subcommand_matches("withdraw") {
Some(withdraw_sub) => {
let mut wdraw = Withdraw::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();
}
withdraw = Some(wdraw);
}
None => {}
}
//match app.subcommand_matches("config") {
// Some(config_sub) => match config_sub.subcommand() {
// Some(c) => match c {
@@ -223,6 +287,7 @@ impl DrkCli {
stop,
deposit,
transfer,
withdraw,
})
}
}

View File

@@ -19,6 +19,12 @@ pub struct TransferParams {
amount: String,
}
#[derive(Deserialize, Debug)]
pub struct WithdrawParams {
address: String,
amount: String,
}
/// Listens for incoming connections and serves them.
pub async fn listen(
executor: Arc<Executor<'_>>,
@@ -252,6 +258,12 @@ impl RpcInterface {
Ok(jsonrpc_core::Value::String("Transfer To... ".into()))
});
io.add_method("withdraw", |params: jsonrpc_core::Params| async move {
let parsed: WithdrawParams = params.parse().unwrap();
println!("test withdraw params: {:?}", parsed);
Ok(jsonrpc_core::Value::String("Transfer To... ".into()))
});
debug!(target: "rpc", "JsonRpcInterface::handle_input() [END]");
Ok(io)
}