2022-10-20 13:40:01 +02:00
|
|
|
import os
|
|
|
|
|
import asyncio
|
|
|
|
|
from collections import defaultdict
|
2023-09-06 19:01:41 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2022-10-20 13:40:01 +02:00
|
|
|
|
2023-08-03 15:09:01 +00:00
|
|
|
from aiohttp import web
|
2022-10-20 13:40:01 +02:00
|
|
|
|
|
|
|
|
from electrum.util import log_exceptions, ignore_exceptions
|
|
|
|
|
from electrum.logging import Logger
|
2023-08-03 15:09:01 +00:00
|
|
|
from electrum.util import EventListener
|
2023-07-28 09:28:45 +02:00
|
|
|
from electrum.lnaddr import lndecode
|
2022-10-20 13:40:01 +02:00
|
|
|
|
2023-09-06 19:01:41 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from electrum.simple_config import SimpleConfig
|
|
|
|
|
from electrum.wallet import Abstract_Wallet
|
|
|
|
|
|
|
|
|
|
|
2024-10-10 12:30:27 +02:00
|
|
|
class HttpSwapServer(Logger, EventListener):
|
2022-10-20 13:40:01 +02:00
|
|
|
"""
|
|
|
|
|
public API:
|
|
|
|
|
- getpairs
|
|
|
|
|
- createswap
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
WWW_DIR = os.path.join(os.path.dirname(__file__), 'www')
|
|
|
|
|
|
2023-09-06 19:01:41 +00:00
|
|
|
def __init__(self, config: 'SimpleConfig', wallet: 'Abstract_Wallet'):
|
2022-10-20 13:40:01 +02:00
|
|
|
Logger.__init__(self)
|
|
|
|
|
self.config = config
|
|
|
|
|
self.wallet = wallet
|
2023-07-28 09:28:45 +02:00
|
|
|
self.sm = self.wallet.lnworker.swap_manager
|
2023-08-11 08:12:54 +02:00
|
|
|
self.port = self.config.SWAPSERVER_PORT
|
2022-10-20 13:40:01 +02:00
|
|
|
self.register_callbacks() # eventlistener
|
|
|
|
|
|
|
|
|
|
self.pending = defaultdict(asyncio.Event)
|
|
|
|
|
self.pending_msg = {}
|
|
|
|
|
|
|
|
|
|
@ignore_exceptions
|
|
|
|
|
@log_exceptions
|
|
|
|
|
async def run(self):
|
2024-06-08 11:10:27 +02:00
|
|
|
|
|
|
|
|
while self.wallet.has_password() and self.wallet.get_unlocked_password() is None:
|
|
|
|
|
self.logger.info("This wallet is password-protected. Please unlock it to start the swapserver plugin")
|
|
|
|
|
await asyncio.sleep(10)
|
|
|
|
|
|
2022-10-20 13:40:01 +02:00
|
|
|
app = web.Application()
|
2023-08-10 10:29:32 +02:00
|
|
|
app.add_routes([web.get('/getpairs', self.get_pairs)])
|
|
|
|
|
app.add_routes([web.post('/createswap', self.create_swap)])
|
|
|
|
|
app.add_routes([web.post('/createnormalswap', self.create_normal_swap)])
|
|
|
|
|
app.add_routes([web.post('/addswapinvoice', self.add_swap_invoice)])
|
2022-10-20 13:40:01 +02:00
|
|
|
|
|
|
|
|
runner = web.AppRunner(app)
|
|
|
|
|
await runner.setup()
|
2023-08-11 08:12:54 +02:00
|
|
|
site = web.TCPSite(runner, host='localhost', port=self.port)
|
2022-10-20 13:40:01 +02:00
|
|
|
await site.start()
|
2023-08-11 08:12:54 +02:00
|
|
|
self.logger.info(f"running and listening on port {self.port}")
|
2022-10-20 13:40:01 +02:00
|
|
|
|
|
|
|
|
async def get_pairs(self, r):
|
2023-07-28 09:28:45 +02:00
|
|
|
sm = self.sm
|
2024-10-10 12:30:27 +02:00
|
|
|
sm.server_update_pairs()
|
2022-10-20 13:40:01 +02:00
|
|
|
pairs = {
|
|
|
|
|
"info": [],
|
|
|
|
|
"warnings": [],
|
2023-08-10 17:06:31 +02:00
|
|
|
"htlcFirst": True,
|
2022-10-20 13:40:01 +02:00
|
|
|
"pairs": {
|
|
|
|
|
"BTC/BTC": {
|
|
|
|
|
"rate": 1,
|
|
|
|
|
"limits": {
|
|
|
|
|
"maximal": sm._max_amount,
|
|
|
|
|
"minimal": sm._min_amount,
|
|
|
|
|
"maximalZeroConf": {
|
|
|
|
|
"baseAsset": 0,
|
|
|
|
|
"quoteAsset": 0
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"fees": {
|
2024-10-14 13:35:02 +02:00
|
|
|
"percentage": sm.percentage,
|
2022-10-20 13:40:01 +02:00
|
|
|
"minerFees": {
|
|
|
|
|
"baseAsset": {
|
2025-02-19 17:39:56 +01:00
|
|
|
"normal": sm.mining_fee,
|
2022-10-20 13:40:01 +02:00
|
|
|
"reverse": {
|
2025-02-19 17:39:56 +01:00
|
|
|
"claim": sm.mining_fee,
|
|
|
|
|
"lockup": sm.mining_fee
|
|
|
|
|
},
|
|
|
|
|
"mining_fee": sm.mining_fee
|
2022-10-20 13:40:01 +02:00
|
|
|
},
|
|
|
|
|
"quoteAsset": {
|
2025-02-19 17:39:56 +01:00
|
|
|
"normal": sm.mining_fee,
|
2022-10-20 13:40:01 +02:00
|
|
|
"reverse": {
|
2025-02-19 17:39:56 +01:00
|
|
|
"claim": sm.mining_fee,
|
|
|
|
|
"lockup": sm.mining_fee
|
|
|
|
|
},
|
|
|
|
|
"mining_fee": sm.mining_fee
|
2022-10-20 13:40:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return web.json_response(pairs)
|
|
|
|
|
|
2023-07-28 09:28:45 +02:00
|
|
|
async def add_swap_invoice(self, r):
|
|
|
|
|
request = await r.json()
|
2024-05-07 08:46:48 +02:00
|
|
|
self.sm.server_add_swap_invoice(request)
|
2023-07-28 09:28:45 +02:00
|
|
|
return web.json_response({})
|
|
|
|
|
|
|
|
|
|
async def create_normal_swap(self, r):
|
|
|
|
|
request = await r.json()
|
2024-05-07 08:46:48 +02:00
|
|
|
response = self.sm.server_create_normal_swap(request)
|
2023-07-28 09:28:45 +02:00
|
|
|
return web.json_response(response)
|
|
|
|
|
|
2022-10-20 13:40:01 +02:00
|
|
|
async def create_swap(self, r):
|
|
|
|
|
request = await r.json()
|
2024-05-07 08:46:48 +02:00
|
|
|
response = self.sm.server_create_swap(request)
|
2022-10-20 13:40:01 +02:00
|
|
|
return web.json_response(response)
|