2222from electrum .network import TxBroadcastError , BestEffortRequestFailed
2323from electrum .payment_identifier import (PaymentIdentifierType , PaymentIdentifier , invoice_from_payment_identifier ,
2424 payment_identifier_from_invoice )
25- from electrum .submarine_swaps import SwapServerError
25+ from electrum .submarine_swaps import SwapServerError , SwapServerTransport
2626from electrum .fee_policy import FeePolicy , FixedFeePolicy
2727
2828from .amountedit import AmountEdit , BTCAmountEdit , SizedFreezableLineEdit
@@ -291,17 +291,31 @@ def spend_max(self):
291291 msg += "\n " + _ ("Some coins are frozen: {} (can be unfrozen in the Addresses or in the Coins tab)" ).format (frozen_bal )
292292 QToolTip .showText (self .max_button .mapToGlobal (QPoint (0 , 0 )), msg )
293293
294+ @staticmethod
295+ def maybe_pass_swap_transport (func ):
296+ def wrapper (self , * args , ** kwargs ):
297+ assert isinstance (self , SendTab )
298+ if self .config .WALLET_SEND_CHANGE_TO_LIGHTNING and not kwargs .get ('swap_transport' ):
299+ with self .window .create_sm_transport () as transport :
300+ if self .window .initialize_swap_manager (transport ):
301+ kwargs ['swap_transport' ] = transport
302+ return func (self , * args , ** kwargs )
303+ return func (self , * args , ** kwargs )
304+ return wrapper
305+
294306 # TODO: instead of passing outputs, use an invoice instead (like pay_lightning_invoice)
295307 # so we have more context (we cannot rely on send_tab field contents or payment identifier
296308 # as this method is called from other places as well).
309+ @maybe_pass_swap_transport
297310 def pay_onchain_dialog (
298311 self ,
299312 outputs : List [PartialTxOutput ],
300313 * ,
301314 nonlocal_only = False ,
302315 external_keypairs : Mapping [bytes , bytes ] = None ,
303316 get_coins : Callable [..., Sequence [PartialTxInput ]] = None ,
304- invoice : Optional [Invoice ] = None
317+ invoice : Optional [Invoice ] = None ,
318+ swap_transport : Optional ['SwapServerTransport' ] = None ,
305319 ) -> None :
306320 # trustedcoin requires this
307321 if run_hook ('abort_send' , self ):
@@ -320,7 +334,7 @@ def make_tx(fee_policy, *, confirmed_only=False, base_tx=None):
320334 outputs = outputs ,
321335 base_tx = base_tx ,
322336 is_sweep = is_sweep ,
323- send_change_to_lightning = self . config . WALLET_SEND_CHANGE_TO_LIGHTNING ,
337+ send_change_to_lightning = bool ( swap_transport ) ,
324338 merge_duplicate_outputs = self .config .WALLET_MERGE_DUPLICATE_OUTPUTS ,
325339 )
326340 output_values = [x .value for x in outputs ]
@@ -341,22 +355,20 @@ def make_tx(fee_policy, *, confirmed_only=False, base_tx=None):
341355 return
342356
343357 if swap_dummy_output := tx .get_dummy_output (DummyAddress .SWAP ):
344- sm = self .wallet .lnworker .swap_manager
345- with self .window .create_sm_transport () as transport :
346- if not self .window .initialize_swap_manager (transport ):
347- return
348- coro = sm .request_swap_for_amount (transport = transport , onchain_amount = swap_dummy_output .value )
349- try :
350- swap , swap_invoice = self .window .run_coroutine_dialog (coro , _ ('Requesting swap invoice...' ))
351- except (SwapServerError , UserFacingException ) as e :
352- self .show_error (str (e ))
353- return
354- except UserCancelled :
355- return
356- tx .replace_output_address (DummyAddress .SWAP , swap .lockup_address )
357- assert tx .get_dummy_output (DummyAddress .SWAP ) is None
358- tx .swap_invoice = swap_invoice
359- tx .swap_payment_hash = swap .payment_hash
358+ assert swap_transport and swap_transport .sm .is_initialized .is_set ()
359+ sm = swap_transport .sm
360+ coro = sm .request_forward_swap_for_amount (transport = swap_transport , onchain_amount = swap_dummy_output .value )
361+ try :
362+ swap , swap_invoice = self .window .run_coroutine_dialog (coro , _ ('Requesting swap invoice...' ))
363+ except (SwapServerError , UserFacingException ) as e :
364+ self .show_error (str (e ))
365+ return
366+ except UserCancelled :
367+ return
368+ tx .replace_output_address (DummyAddress .SWAP , swap .lockup_address )
369+ assert tx .get_dummy_output (DummyAddress .SWAP ) is None
370+ tx .swap_invoice = swap_invoice
371+ tx .swap_payment_hash = swap .payment_hash
360372
361373 if is_preview :
362374 self .window .show_transaction (tx , external_keypairs = external_keypairs , invoice = invoice )
0 commit comments