Merge multiple outputs to same address
This commit is contained in:
@@ -613,6 +613,15 @@ def check_scriptpubkey_template_and_dust(scriptpubkey, amount: Optional[int]):
|
||||
if amount < dust_limit:
|
||||
raise Exception(f'amount ({amount}) is below dust limit for scriptpubkey type ({dust_limit})')
|
||||
|
||||
def merge_tx_outputs(outputs):
|
||||
output_dict = {}
|
||||
for output in outputs:
|
||||
if output.scriptpubkey in output_dict:
|
||||
output_dict[output.scriptpubkey].value += output.value
|
||||
else:
|
||||
output_dict[output.scriptpubkey] = copy.copy(output)
|
||||
|
||||
return list(output_dict.values())
|
||||
|
||||
def match_script_against_template(script, template, debug=False) -> bool:
|
||||
"""Returns whether 'script' matches 'template'."""
|
||||
@@ -2042,6 +2051,7 @@ class PartialTransaction(Transaction):
|
||||
|
||||
def add_outputs(self, outputs: List[PartialTxOutput]) -> None:
|
||||
self._outputs.extend(outputs)
|
||||
self._outputs = merge_tx_outputs(self._outputs)
|
||||
self.BIP69_sort(inputs=False)
|
||||
self.invalidate_ser_cache()
|
||||
|
||||
|
||||
@@ -1792,19 +1792,20 @@ class Abstract_Wallet(ABC, Logger, EventListener):
|
||||
lower_bound = max(lower_bound_feerate, lower_bound_relayfee)
|
||||
return max(lower_bound, original_fee_estimator(size))
|
||||
txi = base_tx.inputs()
|
||||
txo = list(filter(lambda o: not self.is_change(o.address), base_tx.outputs()))
|
||||
txo = list(filter(lambda o: not self.is_change(o.address), base_tx.outputs())) + list(outputs)
|
||||
txo = transaction.merge_tx_outputs(txo)
|
||||
old_change_addrs = [o.address for o in base_tx.outputs() if self.is_change(o.address)]
|
||||
rbf_merge_txid = base_tx.txid()
|
||||
else:
|
||||
txi = []
|
||||
txo = []
|
||||
txo = list(outputs)
|
||||
old_change_addrs = []
|
||||
# change address. if empty, coin_chooser will set it
|
||||
change_addrs = self.get_change_addresses_for_new_transaction(change_addr or old_change_addrs)
|
||||
tx = coin_chooser.make_tx(
|
||||
coins=coins,
|
||||
inputs=txi,
|
||||
outputs=list(outputs) + txo,
|
||||
outputs=txo,
|
||||
change_addrs=change_addrs,
|
||||
fee_estimator_vb=fee_estimator,
|
||||
dust_threshold=self.dust_threshold())
|
||||
|
||||
Reference in New Issue
Block a user