77 QModelIndex )
88from PyQt6 .QtGui import QColor
99
10- from electrum .gui .qml .qetxfinalizer import QETxFinalizer
1110from electrum .i18n import _
1211from electrum .bitcoin import DummyAddress
1312from electrum .logging import get_logger
1716from electrum .submarine_swaps import NostrTransport , SwapServerTransport , pubkey_to_rgb_color
1817from electrum .fee_policy import FeePolicy
1918
20- from electrum .gui import messages
21-
2219from .auth import AuthMixin , auth_protect
2320from .qetypes import QEAmount
2421from .qewallet import QEWallet
2522from .util import QtEventListener , qt_event_listener
23+ from .qetxfinalizer import QETxFinalizer
2624
2725if TYPE_CHECKING :
28- from electrum .submarine_swaps import SwapOffer
26+ from electrum .submarine_swaps import SwapOffer , SwapData
2927
3028
3129class InvalidSwapParameters (Exception ): pass
@@ -158,13 +156,13 @@ class State(IntEnum):
158156 def __init__ (self , parent = None ):
159157 super ().__init__ (parent )
160158
161- self ._wallet = None # type : Optional[QEWallet]
162- self ._finalizer = None # type : Optional[QETxFinalizer]
163- self ._sliderPos = 0
164- self ._rangeMin = - 1
165- self ._rangeMax = 1
166- self ._tx = None # updated on feeslider move and fee histogram updates, used for estimation
167- self ._finalized_tx = None # updated by finalizer, used for actual forward swap
159+ self ._wallet : Optional [QEWallet ] = None
160+ self ._finalizer : Optional [QETxFinalizer ] = None
161+ self ._sliderPos : float = 0
162+ self ._rangeMin : float = - 1
163+ self ._rangeMax : float = 1
164+ self ._preview_tx : Optional [ PartialTransaction ] = None # updated on feeslider move and fee histogram updates, used for estimation
165+ self ._finalized_tx : Optional [ PartialTransaction ] = None # updated by finalizer, used for actual forward swap
168166 self ._valid = False
169167 self ._state = QESwapHelper .State .Initialized
170168 self ._userinfo = QESwapHelper .MESSAGE_SWAP_HOWTO
@@ -175,17 +173,21 @@ def __init__(self, parent=None):
175173 self ._miningfee = QEAmount ()
176174 self ._isReverse = False
177175 self ._canCancel = False
178- self ._swap = None
179- self ._fut_htlc_wait = None
176+ self ._swap : Optional [ 'SwapData' ] = None
177+ self ._fut_htlc_wait : Optional [ asyncio . Task ] = None
180178
181179 self ._service_available = False
182- self ._send_amount = 0
183- self ._receive_amount = 0
180+ self ._send_amount : int = 0
181+ self ._receive_amount : int = 0
182+
183+ self ._leftVoid : float = 0
184+ self ._rightVoid : float = 0
184185
185- self ._leftVoid = 0
186- self ._rightVoid = 0
186+ self ._available_swapservers : Optional [QESwapServerNPubListModel ] = None
187187
188- self ._available_swapservers = None
188+ self .transport_task : Optional [asyncio .Task ] = None
189+ self .swap_transport : Optional [SwapServerTransport ] = None
190+ self .recent_offers : Sequence [SwapOffer ] = []
189191
190192 self .register_callbacks ()
191193 self .destroyed .connect (lambda : self .on_destroy ())
@@ -194,11 +196,7 @@ def __init__(self, parent=None):
194196 self ._fwd_swap_updatetx_timer .setSingleShot (True )
195197 self ._fwd_swap_updatetx_timer .timeout .connect (self .fwd_swap_updatetx )
196198 self .requestTxUpdate .connect (self .tx_update_pushback_timer )
197-
198199 self .offersUpdated .connect (self .on_offers_updated )
199- self .transport_task : Optional [asyncio .Task ] = None
200- self .swap_transport : Optional [SwapServerTransport ] = None
201- self .recent_offers = []
202200
203201 def on_destroy (self ):
204202 if self .transport_task is not None :
@@ -517,7 +515,7 @@ def initSwapSliderRange(self):
517515 # this is just to estimate the maximal spendable onchain amount for HTLC
518516 self .update_tx ('!' )
519517 try :
520- max_onchain_spend = self ._tx .output_value_for_address (DummyAddress .SWAP )
518+ max_onchain_spend = self ._preview_tx .output_value_for_address (DummyAddress .SWAP )
521519 except AttributeError : # happens if there are no utxos
522520 max_onchain_spend = 0
523521 reverse = int (min (lnworker .num_sats_can_send (),
@@ -558,13 +556,13 @@ def initSwapSliderRange(self):
558556 def update_tx (self , onchain_amount : Union [int , str ], fee_policy : Optional [FeePolicy ] = None ):
559557 """Updates the transaction associated with a forward swap."""
560558 if onchain_amount is None :
561- self ._tx = None
559+ self ._preview_tx = None
562560 self .valid = False
563561 return
564562 try :
565- self ._tx = self ._create_swap_tx (onchain_amount , fee_policy )
563+ self ._preview_tx = self ._create_swap_tx (onchain_amount , fee_policy )
566564 except (NotEnoughFunds , NoDynamicFeeEstimates ):
567- self ._tx = None
565+ self ._preview_tx = None
568566 self .valid = False
569567
570568 def _create_swap_tx (self , onchain_amount : int | str , fee_policy : Optional [FeePolicy ] = None ):
@@ -614,7 +612,7 @@ def swap_slider_moved(self):
614612 def tx_update_pushback_timer (self ):
615613 self ._fwd_swap_updatetx_timer .start (250 )
616614
617- def check_valid (self , send_amount , receive_amount ):
615+ def check_valid (self , send_amount : int | None , receive_amount : int | None ):
618616 if send_amount and receive_amount :
619617 self .valid = True
620618 else :
@@ -627,12 +625,11 @@ def fwd_swap_updatetx(self):
627625 return
628626 self .update_tx (self ._send_amount )
629627 # add lockup fees, but the swap amount is position
630- pay_amount = self ._send_amount + self ._tx .get_fee () if self ._tx else 0
631- self .miningfee = QEAmount (amount_sat = self ._tx .get_fee ()) if self ._tx else QEAmount ()
628+ pay_amount = self ._send_amount + self ._preview_tx .get_fee () if self ._preview_tx else 0
629+ self .miningfee = QEAmount (amount_sat = self ._preview_tx .get_fee ()) if self ._preview_tx else QEAmount ()
632630 self .check_valid (pay_amount , self ._receive_amount )
633631
634632 def do_normal_swap (self , lightning_amount , onchain_amount ):
635- assert self ._tx
636633 assert self ._finalized_tx
637634 if lightning_amount is None or onchain_amount is None :
638635 return
@@ -689,7 +686,9 @@ async def swap_task():
689686 asyncio .run_coroutine_threadsafe (swap_task (), get_asyncio_loop ())
690687
691688 @pyqtSlot ()
692- def prepNormalSwap (self ):
689+ def prepareNormalSwap (self ):
690+ """prepare for normal swap by instantiating a finalizer for the requested amount.
691+ self._finalized_tx will contain the finalized tx using the fees set by the user"""
693692 def mktx (amt , fee_policy : FeePolicy ):
694693 try :
695694 self ._finalized_tx = self ._create_swap_tx (amt , fee_policy )
0 commit comments