1
0

qt gui: handle swap server unreachable

note: testnet swap server is offline atm
closes https://github.com/spesmilo/electrum/issues/8107
This commit is contained in:
SomberNight
2023-01-01 23:43:25 +00:00
parent c51b6dd75a
commit e3485de496
2 changed files with 23 additions and 5 deletions

View File

@@ -74,6 +74,7 @@ from electrum.simple_config import SimpleConfig
from electrum.logging import Logger
from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError
from electrum.lnaddr import lndecode
from electrum.submarine_swaps import SwapServerError
from .exception_window import Exception_Hook
from .amountedit import BTCAmountEdit
@@ -1131,7 +1132,11 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener):
return
def get_pairs_thread():
self.network.run_from_another_thread(self.wallet.lnworker.swap_manager.get_pairs())
BlockingWaitingDialog(self, _('Please wait...'), get_pairs_thread)
try:
BlockingWaitingDialog(self, _('Please wait...'), get_pairs_thread)
except SwapServerError as e:
self.show_error(str(e))
return
d = SwapDialog(self, is_reverse=is_reverse, recv_amount_sat=recv_amount_sat, channels=channels)
return d.run()

View File

@@ -6,6 +6,7 @@ from decimal import Decimal
import math
import attr
import aiohttp
from .crypto import sha256, hash_160
from .ecc import ECPrivkey
@@ -21,6 +22,7 @@ from .lnutil import hex_to_bytes
from .json_db import StoredObject
from . import constants
from .address_synchronizer import TX_HEIGHT_LOCAL
from .i18n import _
if TYPE_CHECKING:
from .network import Network
@@ -79,6 +81,11 @@ WITNESS_TEMPLATE_REVERSE_SWAP = [
]
class SwapServerError(Exception):
def __str__(self):
return _("The swap server errored or is unreachable.")
@attr.s
class SwapData(StoredObject):
is_reverse = attr.ib(type=bool)
@@ -472,11 +479,17 @@ class SwapManager(Logger):
self._swaps_by_lockup_address[swap.lockup_address] = swap
async def get_pairs(self) -> None:
"""Might raise SwapServerError."""
from .network import Network
response = await Network.async_send_http_on_proxy(
'get',
self.api_url + '/getpairs',
timeout=30)
try:
response = await Network.async_send_http_on_proxy(
'get',
self.api_url + '/getpairs',
timeout=30)
except aiohttp.ClientError as e:
self.logger.error(f"Swap server errored: {e!r}")
raise SwapServerError() from e
# we assume server response is well-formed; otherwise let an exception propagate to the crash reporter
pairs = json.loads(response)
fees = pairs['pairs']['BTC/BTC']['fees']
self.percentage = fees['percentage']