Browse Source

Merge pull request #8942 from SomberNight/202403_get_swap_by_funding_tx

swaps: fix get_swap_by_funding_tx, and types/type-hints
master
ghost43 2 years ago committed by GitHub
parent
commit
745f9184e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 19
      electrum/submarine_swaps.py
  2. 9
      electrum/wallet.py

19
electrum/submarine_swaps.py

@ -1,7 +1,7 @@
import asyncio
import json
import os
from typing import TYPE_CHECKING, Optional, Dict, Union, Sequence, Tuple
from typing import TYPE_CHECKING, Optional, Dict, Union, Sequence, Tuple, Iterable
from decimal import Decimal
import math
import time
@ -294,6 +294,7 @@ class SwapManager(Logger):
if txin:
# the swap is funded
# note: swap.funding_txid can change due to RBF, it will get updated here:
swap.funding_txid = txin.prevout.txid.hex()
swap._funding_prevout = txin.prevout
self._add_or_reindex_swap(swap) # to update _swaps_by_funding_outpoint
@ -1016,13 +1017,19 @@ class SwapManager(Logger):
f"recv_amount={recv_amount} -> send_amount={send_amount} -> inverted_recv_amount={inverted_recv_amount}")
return send_amount
def get_swap_by_funding_tx(self, tx: Transaction) -> Optional[SwapData]:
if len(tx.outputs()) != 1:
return False
prevout = TxOutpoint(txid=bytes.fromhex(tx.txid()), out_idx=0)
return self._swaps_by_funding_outpoint.get(prevout)
def get_swaps_by_funding_tx(self, tx: Transaction) -> Iterable[SwapData]:
swaps = []
for txout_idx, _txo in enumerate(tx.outputs()):
prevout = TxOutpoint(txid=bytes.fromhex(tx.txid()), out_idx=txout_idx)
if swap := self._swaps_by_funding_outpoint.get(prevout):
swaps.append(swap)
return swaps
def get_swap_by_claim_tx(self, tx: Transaction) -> Optional[SwapData]:
# note: we don't batch claim txs atm (batch_rbf cannot combine them
# as the inputs do not belong to the wallet)
if not (len(tx.inputs()) == 1 and len(tx.outputs()) == 1):
return None
txin = tx.inputs()[0]
return self.get_swap_by_claim_txin(txin)

9
electrum/wallet.py

@ -91,6 +91,7 @@ from .descriptor import Descriptor
if TYPE_CHECKING:
from .network import Network
from .exchange_rate import FxThread
from .submarine_swaps import SwapData
_logger = get_logger(__name__)
@ -838,11 +839,11 @@ class Abstract_Wallet(ABC, Logger, EventListener):
return True
return False
def get_swap_by_claim_tx(self, tx: Transaction) -> bool:
def get_swap_by_claim_tx(self, tx: Transaction) -> Optional['SwapData']:
return self.lnworker.swap_manager.get_swap_by_claim_tx(tx) if self.lnworker else None
def get_swap_by_funding_tx(self, tx: Transaction) -> bool:
return bool(self.lnworker.swap_manager.get_swap_by_funding_tx(tx)) if self.lnworker else None
def get_swaps_by_funding_tx(self, tx: Transaction) -> Iterable['SwapData']:
return self.lnworker.swap_manager.get_swaps_by_funding_tx(tx) if self.lnworker else []
def get_wallet_delta(self, tx: Transaction) -> TxWalletDelta:
"""Return the effect a transaction has on the wallet.
@ -2049,7 +2050,7 @@ class Abstract_Wallet(ABC, Logger, EventListener):
if invoices[0].outputs[0].value == '!':
return all_strats, all_strats.index(BumpFeeStrategy.DECREASE_PAYMENT)
# do not decrease payment if it is a swap
if self.get_swap_by_funding_tx(tx):
if self.get_swaps_by_funding_tx(tx):
return [BumpFeeStrategy.PRESERVE_PAYMENT], 0
# default
return all_strats, all_strats.index(BumpFeeStrategy.PRESERVE_PAYMENT)

Loading…
Cancel
Save