@@ -188,6 +188,46 @@ def send_to(self, *, from_node, scriptPubKey, amount, fee=1000):
188188 txid = self .sendrawtransaction (from_node = from_node , tx_hex = tx .serialize ().hex ())
189189 return txid , 1
190190
191+ def send_self_transfer_multi (self , ** kwargs ):
192+ """
193+ Create and send a transaction that spends the given UTXOs and creates a
194+ certain number of outputs with equal amounts.
195+
196+ Returns a dictionary with
197+ - txid
198+ - serialized transaction in hex format
199+ - transaction as CTransaction instance
200+ - list of newly created UTXOs, ordered by vout index
201+ """
202+ tx = self .create_self_transfer_multi (** kwargs )
203+ txid = self .sendrawtransaction (from_node = kwargs ['from_node' ], tx_hex = tx .serialize ().hex ())
204+ return {'new_utxos' : [self .get_utxo (txid = txid , vout = vout ) for vout in range (len (tx .vout ))],
205+ 'txid' : txid , 'hex' : tx .serialize ().hex (), 'tx' : tx }
206+
207+ def create_self_transfer_multi (self , * , from_node , utxos_to_spend , num_outputs = 1 , fee_per_output = 1000 ):
208+ """
209+ Create and return a transaction that spends the given UTXOs and creates a
210+ certain number of outputs with equal amounts.
211+ """
212+ # create simple tx template (1 input, 1 output)
213+ tx = self .create_self_transfer (fee_rate = 0 , from_node = from_node , utxo_to_spend = utxos_to_spend [0 ], mempool_valid = False )['tx' ]
214+
215+ # duplicate inputs, witnesses and outputs
216+ tx .vin = [deepcopy (tx .vin [0 ]) for _ in range (len (utxos_to_spend ))]
217+ tx .wit .vtxinwit = [deepcopy (tx .wit .vtxinwit [0 ]) for _ in range (len (utxos_to_spend ))]
218+ tx .vout = [deepcopy (tx .vout [0 ]) for _ in range (num_outputs )]
219+
220+ # adapt input prevouts
221+ for i , utxo in enumerate (utxos_to_spend ):
222+ tx .vin [i ] = CTxIn (COutPoint (int (utxo ['txid' ], 16 ), utxo ['vout' ]))
223+
224+ # adapt output amounts (use fixed fee per output)
225+ inputs_value_total = sum ([int (COIN * utxo ['value' ]) for utxo in utxos_to_spend ])
226+ outputs_value_total = inputs_value_total - fee_per_output * num_outputs
227+ for i in range (num_outputs ):
228+ tx .vout [i ].nValue = outputs_value_total // num_outputs
229+ return tx
230+
191231 def create_self_transfer (self , * , fee_rate = Decimal ("0.003" ), from_node = None , utxo_to_spend = None , mempool_valid = True , locktime = 0 , sequence = 0 ):
192232 """Create and return a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
193233 from_node = from_node or self ._test_node
0 commit comments