add custom fee

This commit is contained in:
lucretius
2024-01-23 16:59:54 +01:00
parent 4ba8b962f2
commit 3634a6bcdd
5 changed files with 113 additions and 81 deletions

View File

@@ -39,7 +39,7 @@ use zcash_primitives::{
serialize::{Vector},
transaction::{
builder::{Builder},
components::{Amount, OutPoint, TxOut}, components::amount::DEFAULT_FEE,
components::{Amount, OutPoint, TxOut},
TxId, Transaction,
},
sapling::Node,
@@ -2194,6 +2194,7 @@ pub fn scan_full_mempool_tx(&self, tx: &Transaction, height: i32, _datetime: u64
output_params: &[u8],
_transparent_only: bool,
tos: Vec<(&str, u64, Option<String>)>,
fee: &u64,
broadcast_fn: F
) -> Result<(String, Vec<u8>), String>
where F: Fn(Box<[u8]>) -> Result<String, String>
@@ -2224,24 +2225,31 @@ pub fn scan_full_mempool_tx(&self, tx: &Transaction, height: i32, _datetime: u64
total_value, tos.len()
);
// Convert address (str) to RecepientAddress and value to Amount
let recepients = tos.iter().map(|to| {
let ra = match address::RecipientAddress::from_str(to.0,
self.config.hrp_sapling_address(),
self.config.base58_pubkey_address(),
self.config.base58_script_address()) {
Some(to) => to,
None => {
let e = format!("Invalid recipient address: '{}'", to.0);
error!("{}", e);
return Err(e);
}
};
// Convert address (str) to RecipientAddress and value to Amount
let value = Amount::from_u64(to.1).unwrap();
let recepients: Result<Vec<(address::RecipientAddress, Amount, Option<String>)>, String> = tos.iter().map(|to| {
// Convert string to RecipientAddress
let ra = match address::RecipientAddress::from_str(
to.0,
self.config.hrp_sapling_address(),
self.config.base58_pubkey_address(),
self.config.base58_script_address()
) {
Some(addr) => addr,
None => {
let e = format!("Invalid recipient address: '{}'", to.0);
error!("{}", e);
return Err(e);
}
};
Ok((ra, value, to.2.clone()))
}).collect::<Result<Vec<(address::RecipientAddress, Amount, Option<String>)>, String>>()?;
// Convert the second tuple element to Amount
let value = Amount::from_u64(to.1).expect("Invalid amount value");
Ok((ra, value, to.2.clone()))
}).collect();
let recepients = recepients?;
// Target the next block, assuming we are up-to-date.
let (height, anchor_offset) = match self.get_target_height_and_anchor_offset() {
@@ -2254,8 +2262,9 @@ pub fn scan_full_mempool_tx(&self, tx: &Transaction, height: i32, _datetime: u64
};
// Select notes to cover the target value
println!("{}: Selecting notes", now() - start_time);
let target_value = Amount::from_u64(total_value).unwrap() + DEFAULT_FEE ;
// Select notes to cover the target value
println!("{}: Selecting notes", now() - start_time);
let target_value = Amount::from_u64(total_value).unwrap() + Amount::from_u64(*fee).unwrap();
// Select the candidate notes that are eligible to be spent
let notes: Vec<_> = self.txs.read().unwrap().iter()
.map(|(txid, tx)| tx.notes.iter().map(move |note| (*txid, note)))
@@ -2340,8 +2349,11 @@ pub fn scan_full_mempool_tx(&self, tx: &Transaction, height: i32, _datetime: u64
return Err(e);
}
let fee_amount = Amount::from_u64(*fee).expect("Invalid fee amount");
builder.set_fee(fee_amount);
// Create the transaction
println!("{}: Adding {} notes and {} utxos", now() - start_time, notes.len(), tinputs.len());
println!("{}: Adding {} notes and {} utxos and fee {:?}", now() - start_time, notes.len(), tinputs.len(), fee_amount);
for selected in notes.iter() {
if let Err(e) = builder.add_sapling_spend(