Browse Source

submarine_swaps: move http calls to method

master
ThomasV 2 years ago
parent
commit
01ca1b0382
  1. 36
      electrum/submarine_swaps.py

36
electrum/submarine_swaps.py

@ -579,6 +579,14 @@ class SwapManager(Logger):
assert swap.spending_txid is None assert swap.spending_txid is None
self.invoices_to_pay[key] = 0 self.invoices_to_pay[key] = 0
async def send_request_to_server(self, method, request_data):
response = await self.network.async_send_http_on_proxy(
'post' if request_data else 'get',
self.api_url + '/' + method,
json=request_data,
timeout=30)
return json.loads(response)
async def normal_swap( async def normal_swap(
self, self,
*, *,
@ -632,12 +640,7 @@ class SwapManager(Logger):
"invoiceAmount": lightning_amount_sat, "invoiceAmount": lightning_amount_sat,
"refundPublicKey": refund_pubkey.hex() "refundPublicKey": refund_pubkey.hex()
} }
response = await self.network.async_send_http_on_proxy( data = await self.send_request_to_server('createnormalswap', request_data)
'post',
self.api_url + '/createnormalswap',
json=request_data,
timeout=30)
data = json.loads(response)
payment_hash = bytes.fromhex(data["preimageHash"]) payment_hash = bytes.fromhex(data["preimageHash"])
zeroconf = data["acceptZeroConf"] zeroconf = data["acceptZeroConf"]
@ -701,12 +704,7 @@ class SwapManager(Logger):
"invoice": invoice, "invoice": invoice,
"refundPublicKey": refund_pubkey.hex(), "refundPublicKey": refund_pubkey.hex(),
} }
response = await self.network.async_send_http_on_proxy( data = await self.send_request_to_server('addswapinvoice', request_data)
'post',
self.api_url + '/addswapinvoice',
json=request_data,
timeout=30)
data = json.loads(response)
# wait for funding tx # wait for funding tx
lnaddr = lndecode(invoice) lnaddr = lndecode(invoice)
while swap.funding_txid is None and not lnaddr.is_expired(): while swap.funding_txid is None and not lnaddr.is_expired():
@ -795,12 +793,7 @@ class SwapManager(Logger):
"preimageHash": payment_hash.hex(), "preimageHash": payment_hash.hex(),
"claimPublicKey": our_pubkey.hex() "claimPublicKey": our_pubkey.hex()
} }
response = await self.network.async_send_http_on_proxy( data = await self.send_request_to_server('createswap', request_data)
'post',
self.api_url + '/createswap',
json=request_data,
timeout=30)
data = json.loads(response)
invoice = data['invoice'] invoice = data['invoice']
fee_invoice = data.get('minerFeeInvoice') fee_invoice = data.get('minerFeeInvoice')
lockup_address = data['lockupAddress'] lockup_address = data['lockupAddress']
@ -881,15 +874,10 @@ class SwapManager(Logger):
"""Might raise SwapServerError.""" """Might raise SwapServerError."""
from .network import Network from .network import Network
try: try:
response = await Network.async_send_http_on_proxy( pairs = await self.send_request_to_server('getpairs', None)
'get',
self.api_url + '/getpairs',
timeout=30)
except aiohttp.ClientError as e: except aiohttp.ClientError as e:
self.logger.error(f"Swap server errored: {e!r}") self.logger.error(f"Swap server errored: {e!r}")
raise SwapServerError() from e raise SwapServerError() from e
# we assume server response is well-formed; otherwise let an exception propagate to the crash reporter
pairs = json.loads(response)
# cache data to disk # cache data to disk
with open(self.pairs_filename(), 'w', encoding='utf-8') as f: with open(self.pairs_filename(), 'w', encoding='utf-8') as f:
f.write(json.dumps(pairs)) f.write(json.dumps(pairs))

Loading…
Cancel
Save