From 4c39a57ae38113070be2ee00f6f4bb319f8aa67c Mon Sep 17 00:00:00 2001 From: Cryptoforge Date: Thu, 30 Jul 2020 23:03:54 -0700 Subject: [PATCH] set fee --- lib/src/commands.rs | 14 +++++++++++--- lib/src/lightclient.rs | 4 ++-- lib/src/lightwallet.rs | 12 ++++++++---- lib/src/lightwallet/bugs.rs | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/src/commands.rs b/lib/src/commands.rs index 1da339f..8b63e87 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -481,8 +481,6 @@ impl Command for SendCommand { use std::convert::TryInto; use zcash_primitives::transaction::components::amount::DEFAULT_FEE; - let fee: u64 = DEFAULT_FEE.try_into().unwrap(); - // Check for a single argument that can be parsed as JSON let arg_list = args[0]; @@ -495,6 +493,16 @@ impl Command for SendCommand { } }; + //Check for a fee key and convert to u64 + let fee: u64 = if json_args.has_key("fee") { + match json_args["fee"].as_u64() { + Some(f) => f.clone(), + None => DEFAULT_FEE.try_into().unwrap() + } + } else { + DEFAULT_FEE.try_into().unwrap() + }; + //Check for a input key and convert to str let from = if json_args.has_key("input") { json_args["input"].as_str().unwrap().clone() @@ -541,7 +549,7 @@ impl Command for SendCommand { Ok(_) => { // Convert to the right format. String -> &str. let tos = send_args.iter().map(|(a, v, m)| (a.as_str(), *v, m.clone()) ).collect::>(); - match lightclient.do_send(from, tos) { + match lightclient.do_send(from, tos, &fee) { Ok(txid) => { object!{ "txid" => txid } }, Err(e) => { object!{ "error" => e } } }.pretty(2) diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index 088aef5..1cbb29c 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -1334,7 +1334,7 @@ impl LightClient { } } - pub fn do_send(&self, from: &str, addrs: Vec<(&str, u64, Option)>) -> Result { + pub fn do_send(&self, from: &str, addrs: Vec<(&str, u64, Option)>, fee: &u64) -> Result { if !self.wallet.read().unwrap().is_unlocked_for_spending() { error!("Wallet is locked"); return Err("Wallet is locked".to_string()); @@ -1345,7 +1345,7 @@ impl LightClient { let rawtx = self.wallet.write().unwrap().send_to_address( u32::from_str_radix(&self.config.consensus_branch_id, 16).unwrap(), &self.sapling_spend, &self.sapling_output, - from, addrs + from, addrs, fee ); info!("Transaction Complete"); diff --git a/lib/src/lightwallet.rs b/lib/src/lightwallet.rs index 853e3a7..de04a49 100644 --- a/lib/src/lightwallet.rs +++ b/lib/src/lightwallet.rs @@ -36,8 +36,8 @@ use zcash_primitives::{ consensus::BranchId, transaction::{ builder::{Builder}, - components::{Amount, OutPoint, TxOut}, components::amount::DEFAULT_FEE, - TxId, Transaction, + components::{Amount, OutPoint, TxOut}, //components::amount::DEFAULT_FEE, + TxId, Transaction, }, sapling::Node, merkle_tree::{CommitmentTree, IncrementalWitness}, @@ -1680,7 +1680,8 @@ impl LightWallet { spend_params: &[u8], output_params: &[u8], from: &str, - tos: Vec<(&str, u64, Option)> + tos: Vec<(&str, u64, Option)>, + fee: &u64 ) -> Result, String> { if !self.unlocked { return Err("Cannot spend while wallet is locked".to_string()); @@ -1728,7 +1729,7 @@ impl LightWallet { // Select notes to cover the target value println!("{}: Selecting notes", now() - start_time); - let target_value = Amount::from_u64(total_value).unwrap() + DEFAULT_FEE ; + let target_value = Amount::from_u64(total_value).unwrap() + Amount::from_u64(*fee).unwrap() ; let notes: Vec<_> = self.txs.read().unwrap().iter() .map(|(txid, tx)| tx.notes.iter().map(move |note| (*txid, note))) .flatten() @@ -1750,6 +1751,9 @@ impl LightWallet { let mut builder = Builder::new(height); + //set fre + builder.set_fee(Amount::from_u64(*fee).unwrap()); + // A note on t addresses // Funds received by t-addresses can't be explicitly spent in ZecWallet. // ZecWallet will lazily consolidate all t address funds into your shielded addresses. diff --git a/lib/src/lightwallet/bugs.rs b/lib/src/lightwallet/bugs.rs index a641b13..e81642f 100644 --- a/lib/src/lightwallet/bugs.rs +++ b/lib/src/lightwallet/bugs.rs @@ -82,7 +82,7 @@ impl BugBip39Derivation { let txid = if amount > 0 { println!("Sending funds to ourself."); let fee: u64 = DEFAULT_FEE.try_into().unwrap(); - match client.do_send(client.do_address()["z_addresses"][0].as_str().unwrap(), vec![(&zaddr, amount-fee, None)]) { + match client.do_send(client.do_address()["z_addresses"][0].as_str().unwrap(), vec![(&zaddr, amount-fee, None)], &fee) { Ok(txid) => txid, Err(e) => { let r = object!{