@@ -142,7 +142,7 @@ def _select_utxos(
142
142
return utxo_ids
143
143
144
144
145
- def _balance_txouts ( # noqa: C901
145
+ def _balance_txouts (
146
146
change_address : str ,
147
147
txouts : structs .OptionalTxOuts ,
148
148
txins_db : tp .Dict [str , tp .List [structs .UTXOData ]],
@@ -155,39 +155,40 @@ def _balance_txouts( # noqa: C901
155
155
skip_asset_balancing : bool = False ,
156
156
) -> tp .List [structs .TxOut ]:
157
157
"""Balance the transaction by adding change output for each coin."""
158
- # records for burning tokens, i.e. records with negative amount, are not allowed in `txouts`
158
+ # Records for burning tokens, i.e. records with negative amount, are not allowed in `txouts`
159
159
burning_txouts = [r for r in txouts if r .amount < 0 and r .coin != consts .DEFAULT_COIN ]
160
160
if burning_txouts :
161
161
msg = f"Token burning is not allowed in txouts: { burning_txouts } "
162
162
raise AssertionError (msg )
163
163
164
- txouts_result : tp .List [structs .TxOut ] = list (txouts )
164
+ # Filter out negative amounts (-1 "max" amounts)
165
+ txouts_result = [r for r in txouts if r .amount > 0 ]
165
166
166
- # iterate over coins both in txins and txouts
167
+ if skip_asset_balancing :
168
+ # Balancing is done elsewhere (by the `transaction build` command)
169
+ return txouts_result
170
+
171
+ # Iterate over coins both in txins and txouts
167
172
for coin in set (txins_db ).union (txouts_passed_db ).union (txouts_mint_db ):
168
173
max_address = None
169
174
change = 0
170
175
171
176
coin_txins = txins_db .get (coin ) or []
172
177
coin_txouts = txouts_passed_db .get (coin ) or []
173
178
179
+ total_input_amount = functools .reduce (lambda x , y : x + y .amount , coin_txins , 0 )
180
+
174
181
if coin == consts .DEFAULT_COIN :
175
- # the value "-1" means all available funds
182
+ # The value "-1" means all available funds
176
183
max_index = [idx for idx , val in enumerate (coin_txouts ) if val .amount == - 1 ]
177
184
if len (max_index ) > 1 :
178
185
msg = "Cannot send all remaining funds to more than one address."
179
186
raise AssertionError (msg )
180
187
if max_index :
181
- # remove the "-1" record and get its address
188
+ # Remove the "-1" record and get its address
182
189
max_address = coin_txouts .pop (max_index [0 ]).address
183
190
184
- total_input_amount = functools .reduce (lambda x , y : x + y .amount , coin_txins , 0 )
185
- total_output_amount = functools .reduce (lambda x , y : x + y .amount , coin_txouts , 0 )
186
-
187
- if skip_asset_balancing :
188
- # balancing is done elsewhere (by the `transaction build` command)
189
- pass
190
- elif coin == consts .DEFAULT_COIN :
191
+ total_output_amount = functools .reduce (lambda x , y : x + y .amount , coin_txouts , 0 )
191
192
tx_fee = fee if fee > 0 else 0
192
193
total_withdrawals_amount = functools .reduce (lambda x , y : x + y .amount , withdrawals , 0 )
193
194
funds_available = total_input_amount + total_withdrawals_amount
@@ -200,6 +201,7 @@ def _balance_txouts( # noqa: C901
200
201
)
201
202
else :
202
203
coin_txouts_minted = txouts_mint_db .get (coin ) or []
204
+ total_output_amount = functools .reduce (lambda x , y : x + y .amount , coin_txouts , 0 )
203
205
total_minted_amount = functools .reduce (lambda x , y : x + y .amount , coin_txouts_minted , 0 )
204
206
funds_available = total_input_amount + total_minted_amount
205
207
change = funds_available - total_output_amount
@@ -214,9 +216,6 @@ def _balance_txouts( # noqa: C901
214
216
structs .TxOut (address = (max_address or change_address ), amount = change , coin = coin )
215
217
)
216
218
217
- # filter out negative amounts (-1 "max" amounts)
218
- txouts_result = [r for r in txouts_result if r .amount > 0 ]
219
-
220
219
return txouts_result
221
220
222
221
0 commit comments