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
|
self.error = long_warning
|
||||||
else:
|
else:
|
||||||
messages.append(long_warning)
|
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.'))
|
messages.append(_('This transaction will send funds to a submarine swap.'))
|
||||||
# warn if spending unconf
|
# warn if spending unconf
|
||||||
if any((txin.block_height is not None and txin.block_height<=0) for txin in self.tx.inputs()):
|
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
|
# user cancelled
|
||||||
return
|
return
|
||||||
|
|
||||||
if tx.has_dummy_output(DummyAddress.SWAP):
|
if swap_dummy_output := tx.get_dummy_output(DummyAddress.SWAP):
|
||||||
sm = self.wallet.lnworker.swap_manager
|
sm = self.wallet.lnworker.swap_manager
|
||||||
with self.window.create_sm_transport() as transport:
|
with self.window.create_sm_transport() as transport:
|
||||||
if not self.window.initialize_swap_manager(transport):
|
if not self.window.initialize_swap_manager(transport):
|
||||||
return
|
return
|
||||||
coro = sm.request_swap_for_tx(transport, tx)
|
coro = sm.request_swap_for_amount(transport, swap_dummy_output.value)
|
||||||
try:
|
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:
|
except SwapServerError as e:
|
||||||
self.show_error(str(e))
|
self.show_error(str(e))
|
||||||
return
|
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_invoice = invoice
|
||||||
tx.swap_payment_hash = swap.payment_hash
|
tx.swap_payment_hash = swap.payment_hash
|
||||||
|
|
||||||
|
|||||||
@@ -826,21 +826,14 @@ class SwapManager(Logger):
|
|||||||
return tx
|
return tx
|
||||||
|
|
||||||
@log_exceptions
|
@log_exceptions
|
||||||
async def request_swap_for_tx(self, transport, tx: 'PartialTransaction') -> Optional[Tuple[SwapData, str, PartialTransaction]]:
|
async def request_swap_for_amount(self, transport, onchain_amount) -> Optional[Tuple[SwapData, str]]:
|
||||||
for o in tx.outputs():
|
|
||||||
if o.address == self.dummy_address:
|
|
||||||
change_amount = o.value
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
await self.is_initialized.wait()
|
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(
|
swap, invoice = await self.request_normal_swap(
|
||||||
transport,
|
transport,
|
||||||
lightning_amount_sat = lightning_amount_sat,
|
lightning_amount_sat=lightning_amount_sat,
|
||||||
expected_onchain_amount_sat=change_amount)
|
expected_onchain_amount_sat=onchain_amount)
|
||||||
tx.replace_output_address(DummyAddress.SWAP, swap.lockup_address)
|
return swap, invoice
|
||||||
return swap, invoice, tx
|
|
||||||
|
|
||||||
@log_exceptions
|
@log_exceptions
|
||||||
async def broadcast_funding_tx(self, swap: SwapData, tx: Transaction) -> None:
|
async def broadcast_funding_tx(self, swap: SwapData, tx: Transaction) -> None:
|
||||||
|
|||||||
@@ -1247,8 +1247,13 @@ class Transaction:
|
|||||||
def get_change_outputs(self):
|
def get_change_outputs(self):
|
||||||
return [o for o in self._outputs if o.is_change]
|
return [o for o in self._outputs if o.is_change]
|
||||||
|
|
||||||
def has_dummy_output(self, dummy_addr: str) -> bool:
|
def get_dummy_output(self, dummy_addr: str) -> Optional['PartialTxOutput']:
|
||||||
return len(self.get_output_idxs_from_address(dummy_addr)) == 1
|
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):
|
def output_value_for_address(self, addr):
|
||||||
# assumes exactly one output has that address
|
# assumes exactly one output has that address
|
||||||
|
|||||||
Reference in New Issue
Block a user