create_transaction deep refactoring
This commit is contained in:
parent
a433400dd4
commit
db3db3a497
@ -27,52 +27,12 @@ impl TsUnsignedTransaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_transaction(
|
pub fn create_transaction(
|
||||||
mandatory_inputs: Vec<OutPoint>,
|
available_outpoints: Vec<(OutPoint, OwnedOutput)>,
|
||||||
mut available_outpoints: HashMap<OutPoint, OwnedOutput>,
|
|
||||||
freezed_utxos: &HashSet<OutPoint>,
|
|
||||||
sp_client: &SpClient,
|
sp_client: &SpClient,
|
||||||
mut recipients: Vec<Recipient>,
|
mut recipients: Vec<Recipient>,
|
||||||
payload: Option<Vec<u8>>,
|
payload: Option<Vec<u8>>,
|
||||||
fee_rate: Amount,
|
fee_rate: FeeRate,
|
||||||
) -> Result<SilentPaymentUnsignedTransaction> {
|
) -> Result<SilentPaymentUnsignedTransaction> {
|
||||||
let sum_outputs = recipients
|
|
||||||
.iter()
|
|
||||||
.fold(Amount::from_sat(0), |acc, x| acc + x.amount);
|
|
||||||
|
|
||||||
let zero_value_recipient = recipients
|
|
||||||
.iter_mut()
|
|
||||||
.find(|r| r.amount == Amount::from_sat(0));
|
|
||||||
|
|
||||||
let mut inputs: HashMap<OutPoint, OwnedOutput> = HashMap::new();
|
|
||||||
let mut total_available = Amount::from_sat(0);
|
|
||||||
for outpoint in mandatory_inputs {
|
|
||||||
let (must_outpoint, must_output) = available_outpoints
|
|
||||||
.remove_entry(&outpoint)
|
|
||||||
.ok_or_else(|| Error::msg(format!("Mandatory outpoint unknown: {}", outpoint)))?;
|
|
||||||
total_available += must_output.amount;
|
|
||||||
inputs.insert(must_outpoint, must_output);
|
|
||||||
}
|
|
||||||
|
|
||||||
let fee_provision = Amount::from_sat(1000);
|
|
||||||
|
|
||||||
for (outpoint, output) in &available_outpoints {
|
|
||||||
if freezed_utxos.contains(outpoint) { continue }
|
|
||||||
if total_available > sum_outputs.checked_add(fee_provision).unwrap() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
total_available += output.amount;
|
|
||||||
inputs.insert(*outpoint, output.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
if total_available <= sum_outputs.checked_add(fee_provision).unwrap() {
|
|
||||||
return Err(Error::msg("Not enough available funds"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(recipient) = zero_value_recipient {
|
|
||||||
// update the amount for the recipient
|
|
||||||
recipient.amount = total_available;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut commitment = [0u8; 32];
|
let mut commitment = [0u8; 32];
|
||||||
if let Some(ref p) = payload {
|
if let Some(ref p) = payload {
|
||||||
commitment.copy_from_slice(&p);
|
commitment.copy_from_slice(&p);
|
||||||
@ -80,10 +40,15 @@ pub fn create_transaction(
|
|||||||
thread_rng().fill(&mut commitment);
|
thread_rng().fill(&mut commitment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recipients.push(Recipient {
|
||||||
|
address: sp_client::RecipientAddress::Data(commitment.to_vec()),
|
||||||
|
amount: Amount::ZERO
|
||||||
|
});
|
||||||
|
|
||||||
let new_transaction = sp_client.create_new_transaction(
|
let new_transaction = sp_client.create_new_transaction(
|
||||||
available_outpoints.into_iter().collect(),
|
available_outpoints,
|
||||||
recipients,
|
recipients,
|
||||||
fee_rate.to_btc() as f32,
|
fee_rate,
|
||||||
sp_client.get_network()
|
sp_client.get_network()
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user