From 08e3d81f6515dfffbf6bc37ab54a4ef350aadb7f Mon Sep 17 00:00:00 2001 From: parazyd Date: Sat, 30 Apr 2022 21:15:56 +0200 Subject: [PATCH] drk: WIP transfers. --- bin/drk/src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++- src/node/client.rs | 2 -- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/bin/drk/src/main.rs b/bin/drk/src/main.rs index 5b5b716f5..54564933c 100644 --- a/bin/drk/src/main.rs +++ b/bin/drk/src/main.rs @@ -13,7 +13,7 @@ use darkfi::{ jsonrpc, jsonrpc::{JsonRequest, JsonResult}, }, - util::cli::log_config, + util::{cli::log_config, NetworkName}, Error::JsonRpcError, Result, }; @@ -72,6 +72,24 @@ enum DrkSubcommand { /// Get all addresses in the wallet all_addresses: bool, }, + + /// Transfer of value + Transfer { + /// Recipient address + #[clap(parse(try_from_str))] + recipient: Address, + + /// Amount to transfer + amount: f64, + + /// Coin network + #[clap(short, long, default_value = "darkfi", parse(try_from_str))] + network: NetworkName, + + /// Token ID + #[clap(short, long)] + token_id: String, + }, } struct Drk { @@ -212,6 +230,31 @@ impl Drk { println!("Wallet addresses:\n{:#?}", rep); Ok(()) } + + async fn tx_transfer( + &self, + network: NetworkName, + token_id: String, + recipient: Address, + amount: f64, + ) -> Result<()> { + println!("Attempting to transfer {} tokens to {}", amount, recipient); + + let req = jsonrpc::request( + json!("tx.transfer"), + json!([network.to_string(), token_id, recipient.to_string(), amount]), + ); + let rep = match self.request(req, None).await { + Ok(v) => v, + Err(e) => { + error!("Error building and sending transaction: {}", e); + return Err(e) + } + }; + + println!("Success! Transaction ID: {}", rep); + Ok(()) + } } #[async_std::main] @@ -250,5 +293,9 @@ async fn main() -> Result<()> { eprintln!("Run 'drk wallet -h' to see the subcommand usage."); exit(2); } + + DrkSubcommand::Transfer { recipient, amount, network, token_id } => { + drk.tx_transfer(network, token_id, recipient, amount).await + } } } diff --git a/src/node/client.rs b/src/node/client.rs index 4cb955b4c..df8940eea 100644 --- a/src/node/client.rs +++ b/src/node/client.rs @@ -142,8 +142,6 @@ impl Client { debug!("build_slab_from_tx(): Successful state transition"); debug!("build_slab_from_tx(): Broadcasting transaction"); - // TODO: Send to some channel, let's not p2p here - //self.p2p.broadcast(Tx(Transaction)).await?; debug!("build_slab_from_tx(): Broadcasted successfully"); Ok((tx, coins))