drk: WIP transfers.

This commit is contained in:
parazyd
2022-04-30 21:15:56 +02:00
parent 250cdeeae8
commit 08e3d81f65
2 changed files with 48 additions and 3 deletions

View File

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

View File

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