swaps: replace request_swap_for_tx with request_swap_for_amount,
as this uses less side effects (change backported from batch_payment_manager)
This commit is contained in:
@@ -560,7 +560,7 @@ class TxEditor(WindowModalDialog):
|
||||
self.error = long_warning
|
||||
else:
|
||||
messages.append(long_warning)
|
||||
if self.tx.has_dummy_output(DummyAddress.SWAP):
|
||||
if self.tx.get_dummy_output(DummyAddress.SWAP):
|
||||
messages.append(_('This transaction will send funds to a submarine swap.'))
|
||||
# warn if spending unconf
|
||||
if any((txin.block_height is not None and txin.block_height<=0) for txin in self.tx.inputs()):
|
||||
|
||||
@@ -339,18 +339,19 @@ class SendTab(QWidget, MessageBoxMixin, Logger):
|
||||
# user cancelled
|
||||
return
|
||||
|
||||
if tx.has_dummy_output(DummyAddress.SWAP):
|
||||
if swap_dummy_output := tx.get_dummy_output(DummyAddress.SWAP):
|
||||
sm = self.wallet.lnworker.swap_manager
|
||||
with self.window.create_sm_transport() as transport:
|
||||
if not self.window.initialize_swap_manager(transport):
|
||||
return
|
||||
coro = sm.request_swap_for_tx(transport, tx)
|
||||
coro = sm.request_swap_for_amount(transport, swap_dummy_output.value)
|
||||
try:
|
||||
swap, invoice, tx = self.window.run_coroutine_dialog(coro, _('Requesting swap invoice...'))
|
||||
swap, invoice = self.window.run_coroutine_dialog(coro, _('Requesting swap invoice...'))
|
||||
except SwapServerError as e:
|
||||
self.show_error(str(e))
|
||||
return
|
||||
assert not tx.has_dummy_output(DummyAddress.SWAP)
|
||||
tx.replace_output_address(DummyAddress.SWAP, swap.lockup_address)
|
||||
assert tx.get_dummy_output(DummyAddress.SWAP) is None
|
||||
tx.swap_invoice = invoice
|
||||
tx.swap_payment_hash = swap.payment_hash
|
||||
|
||||
|
||||
@@ -826,21 +826,14 @@ class SwapManager(Logger):
|
||||
return tx
|
||||
|
||||
@log_exceptions
|
||||
async def request_swap_for_tx(self, transport, tx: 'PartialTransaction') -> Optional[Tuple[SwapData, str, PartialTransaction]]:
|
||||
for o in tx.outputs():
|
||||
if o.address == self.dummy_address:
|
||||
change_amount = o.value
|
||||
break
|
||||
else:
|
||||
return
|
||||
async def request_swap_for_amount(self, transport, onchain_amount) -> Optional[Tuple[SwapData, str]]:
|
||||
await self.is_initialized.wait()
|
||||
lightning_amount_sat = self.get_recv_amount(change_amount, is_reverse=False)
|
||||
lightning_amount_sat = self.get_recv_amount(onchain_amount, is_reverse=False)
|
||||
swap, invoice = await self.request_normal_swap(
|
||||
transport,
|
||||
lightning_amount_sat = lightning_amount_sat,
|
||||
expected_onchain_amount_sat=change_amount)
|
||||
tx.replace_output_address(DummyAddress.SWAP, swap.lockup_address)
|
||||
return swap, invoice, tx
|
||||
lightning_amount_sat=lightning_amount_sat,
|
||||
expected_onchain_amount_sat=onchain_amount)
|
||||
return swap, invoice
|
||||
|
||||
@log_exceptions
|
||||
async def broadcast_funding_tx(self, swap: SwapData, tx: Transaction) -> None:
|
||||
|
||||
@@ -1247,8 +1247,13 @@ class Transaction:
|
||||
def get_change_outputs(self):
|
||||
return [o for o in self._outputs if o.is_change]
|
||||
|
||||
def has_dummy_output(self, dummy_addr: str) -> bool:
|
||||
return len(self.get_output_idxs_from_address(dummy_addr)) == 1
|
||||
def get_dummy_output(self, dummy_addr: str) -> Optional['PartialTxOutput']:
|
||||
idxs = self.get_output_idxs_from_address(dummy_addr)
|
||||
if not idxs:
|
||||
return
|
||||
assert len(idxs) == 1
|
||||
for i in idxs:
|
||||
return self.outputs()[i]
|
||||
|
||||
def output_value_for_address(self, addr):
|
||||
# assumes exactly one output has that address
|
||||
|
||||
Reference in New Issue
Block a user