Skip to content

Commit 05adb94

Browse files
committed
Add more inputs after random improvement when needed
1 parent ab4ad39 commit 05adb94

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

rust/src/tx_builder.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ impl TransactionBuilder {
256256
}
257257
}
258258
}
259+
// Phase 3: add extra inputs needed for fees
260+
// We do this at the end because this new inputs won't be associated with
261+
// a specific output, so the improvement algorithm we do above does not apply here.
262+
if input_total.partial_cmp(&output_total).unwrap().eq(&Ordering::Less) {
263+
let mut added = Value::new(&Coin::zero());
264+
let mut remaining_amount = output_total.checked_sub(&input_total)?;
265+
while added < remaining_amount {
266+
if available_inputs.is_empty() {
267+
return Err(JsError::from_str("UTxO Balance Insufficient"));
268+
}
269+
let (new_input_total, new_output_total, new_added, new_remaining_amount, _) = add_random_input(self, &mut rng, &mut available_inputs, &input_total, &output_total, &added, &remaining_amount)?;
270+
input_total = new_input_total;
271+
output_total = new_output_total;
272+
added = new_added;
273+
remaining_amount = new_remaining_amount;
274+
}
275+
}
259276
},
260277
}
261278

0 commit comments

Comments
 (0)