66from PyQt6 .QtCore import (pyqtProperty , pyqtSignal , pyqtSlot , QObject , QTimer , pyqtEnum , QAbstractListModel , Qt ,
77 QModelIndex )
88
9- from electrum .gui .qml .qetxfinalizer import QETxFinalizer
109from electrum .i18n import _
1110from electrum .bitcoin import DummyAddress
1211from electrum .logging import get_logger
1615from electrum .submarine_swaps import NostrTransport , SwapServerTransport
1716from electrum .fee_policy import FeePolicy
1817
19- from electrum .gui import messages
20-
2118from .auth import AuthMixin , auth_protect
2219from .qetypes import QEAmount
2320from .qewallet import QEWallet
2421from .util import QtEventListener , qt_event_listener
22+ from .qetxfinalizer import QETxFinalizer
2523
2624if TYPE_CHECKING :
27- from electrum .submarine_swaps import SwapOffer
25+ from electrum .submarine_swaps import SwapOffer , SwapData
2826
2927
3028class InvalidSwapParameters (Exception ): pass
@@ -156,13 +154,13 @@ class State(IntEnum):
156154 def __init__ (self , parent = None ):
157155 super ().__init__ (parent )
158156
159- self ._wallet = None # type : Optional[QEWallet]
160- self ._finalizer = None # type : Optional[QETxFinalizer]
161- self ._sliderPos = 0
162- self ._rangeMin = - 1
163- self ._rangeMax = 1
164- self ._tx = None # updated on feeslider move and fee histogram updates, used for estimation
165- self ._finalized_tx = None # updated by finalizer, used for actual forward swap
157+ self ._wallet : Optional [QEWallet ] = None
158+ self ._finalizer : Optional [QETxFinalizer ] = None
159+ self ._sliderPos : float = 0
160+ self ._rangeMin : float = - 1
161+ self ._rangeMax : float = 1
162+ self ._preview_tx : Optional [ PartialTransaction ] = None # updated on feeslider move and fee histogram updates, used for estimation
163+ self ._finalized_tx : Optional [ PartialTransaction ] = None # updated by finalizer, used for actual forward swap
166164 self ._valid = False
167165 self ._state = QESwapHelper .State .Initialized
168166 self ._userinfo = QESwapHelper .MESSAGE_SWAP_HOWTO
@@ -173,17 +171,21 @@ def __init__(self, parent=None):
173171 self ._miningfee = QEAmount ()
174172 self ._isReverse = False
175173 self ._canCancel = False
176- self ._swap = None
177- self ._fut_htlc_wait = None
174+ self ._swap : Optional [ 'SwapData' ] = None
175+ self ._fut_htlc_wait : Optional [ asyncio . Task ] = None
178176
179177 self ._service_available = False
180- self ._send_amount = 0
181- self ._receive_amount = 0
178+ self ._send_amount : int = 0
179+ self ._receive_amount : int = 0
180+
181+ self ._leftVoid : float = 0
182+ self ._rightVoid : float = 0
182183
183- self ._leftVoid = 0
184- self ._rightVoid = 0
184+ self ._available_swapservers : Optional [QESwapServerNPubListModel ] = None
185185
186- self ._available_swapservers = None
186+ self .transport_task : Optional [asyncio .Task ] = None
187+ self .swap_transport : Optional [SwapServerTransport ] = None
188+ self .recent_offers : Sequence [SwapOffer ] = []
187189
188190 self .register_callbacks ()
189191 self .destroyed .connect (lambda : self .on_destroy ())
@@ -192,11 +194,7 @@ def __init__(self, parent=None):
192194 self ._fwd_swap_updatetx_timer .setSingleShot (True )
193195 self ._fwd_swap_updatetx_timer .timeout .connect (self .fwd_swap_updatetx )
194196 self .requestTxUpdate .connect (self .tx_update_pushback_timer )
195-
196197 self .offersUpdated .connect (self .on_offers_updated )
197- self .transport_task : Optional [asyncio .Task ] = None
198- self .swap_transport : Optional [SwapServerTransport ] = None
199- self .recent_offers = []
200198
201199 def on_destroy (self ):
202200 if self .transport_task is not None :
@@ -515,7 +513,7 @@ def initSwapSliderRange(self):
515513 # this is just to estimate the maximal spendable onchain amount for HTLC
516514 self .update_tx ('!' )
517515 try :
518- max_onchain_spend = self ._tx .output_value_for_address (DummyAddress .SWAP )
516+ max_onchain_spend = self ._preview_tx .output_value_for_address (DummyAddress .SWAP )
519517 except AttributeError : # happens if there are no utxos
520518 max_onchain_spend = 0
521519 reverse = int (min (lnworker .num_sats_can_send (),
@@ -556,13 +554,13 @@ def initSwapSliderRange(self):
556554 def update_tx (self , onchain_amount : Union [int , str ], fee_policy : Optional [FeePolicy ] = None ):
557555 """Updates the transaction associated with a forward swap."""
558556 if onchain_amount is None :
559- self ._tx = None
557+ self ._preview_tx = None
560558 self .valid = False
561559 return
562560 try :
563- self ._tx = self ._create_swap_tx (onchain_amount , fee_policy )
561+ self ._preview_tx = self ._create_swap_tx (onchain_amount , fee_policy )
564562 except (NotEnoughFunds , NoDynamicFeeEstimates ):
565- self ._tx = None
563+ self ._preview_tx = None
566564 self .valid = False
567565
568566 def _create_swap_tx (self , onchain_amount : int | str , fee_policy : Optional [FeePolicy ] = None ):
@@ -612,7 +610,7 @@ def swap_slider_moved(self):
612610 def tx_update_pushback_timer (self ):
613611 self ._fwd_swap_updatetx_timer .start (250 )
614612
615- def check_valid (self , send_amount , receive_amount ):
613+ def check_valid (self , send_amount : int | None , receive_amount : int | None ):
616614 if send_amount and receive_amount :
617615 self .valid = True
618616 else :
@@ -625,12 +623,11 @@ def fwd_swap_updatetx(self):
625623 return
626624 self .update_tx (self ._send_amount )
627625 # add lockup fees, but the swap amount is position
628- pay_amount = self ._send_amount + self ._tx .get_fee () if self ._tx else 0
629- self .miningfee = QEAmount (amount_sat = self ._tx .get_fee ()) if self ._tx else QEAmount ()
626+ pay_amount = self ._send_amount + self ._preview_tx .get_fee () if self ._preview_tx else 0
627+ self .miningfee = QEAmount (amount_sat = self ._preview_tx .get_fee ()) if self ._preview_tx else QEAmount ()
630628 self .check_valid (pay_amount , self ._receive_amount )
631629
632630 def do_normal_swap (self , lightning_amount , onchain_amount ):
633- assert self ._tx
634631 assert self ._finalized_tx
635632 if lightning_amount is None or onchain_amount is None :
636633 return
@@ -687,7 +684,9 @@ async def swap_task():
687684 asyncio .run_coroutine_threadsafe (swap_task (), get_asyncio_loop ())
688685
689686 @pyqtSlot ()
690- def prepNormalSwap (self ):
687+ def prepareNormalSwap (self ):
688+ """prepare for normal swap by instantiating a finalizer for the requested amount.
689+ self._finalized_tx will contain the finalized tx using the fees set by the user"""
691690 def mktx (amt , fee_policy : FeePolicy ):
692691 try :
693692 self ._finalized_tx = self ._create_swap_tx (amt , fee_policy )
0 commit comments