Sfoglia il codice sorgente

drk: WIP transfers.

parazyd 4 anni fa
parent
commit
08e3d81f65
2 ha cambiato i file con 48 aggiunte e 3 eliminazioni
  1. 48 1
      bin/drk/src/main.rs
  2. 0 2
      src/node/client.rs

+ 48 - 1
bin/drk/src/main.rs

@@ -13,7 +13,7 @@ use darkfi::{
         jsonrpc,
         jsonrpc,
         jsonrpc::{JsonRequest, JsonResult},
         jsonrpc::{JsonRequest, JsonResult},
     },
     },
-    util::cli::log_config,
+    util::{cli::log_config, NetworkName},
     Error::JsonRpcError,
     Error::JsonRpcError,
     Result,
     Result,
 };
 };
@@ -72,6 +72,24 @@ enum DrkSubcommand {
         /// Get all addresses in the wallet
         /// Get all addresses in the wallet
         all_addresses: bool,
         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 {
 struct Drk {
@@ -212,6 +230,31 @@ impl Drk {
         println!("Wallet addresses:\n{:#?}", rep);
         println!("Wallet addresses:\n{:#?}", rep);
         Ok(())
         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]
 #[async_std::main]
@@ -250,5 +293,9 @@ async fn main() -> Result<()> {
             eprintln!("Run 'drk wallet -h' to see the subcommand usage.");
             eprintln!("Run 'drk wallet -h' to see the subcommand usage.");
             exit(2);
             exit(2);
         }
         }
+
+        DrkSubcommand::Transfer { recipient, amount, network, token_id } => {
+            drk.tx_transfer(network, token_id, recipient, amount).await
+        }
     }
     }
 }
 }

+ 0 - 2
src/node/client.rs

@@ -142,8 +142,6 @@ impl Client {
         debug!("build_slab_from_tx(): Successful state transition");
         debug!("build_slab_from_tx(): Successful state transition");
 
 
         debug!("build_slab_from_tx(): Broadcasting transaction");
         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");
         debug!("build_slab_from_tx(): Broadcasted successfully");
 
 
         Ok((tx, coins))
         Ok((tx, coins))