mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-25 15:10:41 +01:00
* add status to statusdialog * first commits for boltz update * formatting * add latest boltz-clien package * big refactor, depending on boltz_client package, clean up, mypy issues, not tested yet * blacking, sorting and stuff * remove unused req_wrap helper * remove api docs from frontend * bug: frontend boltz limits error * clean up buttons * update to boltz-client 0.0.8 * fix tests to poetry version 1.3.1 * update requirements * formatting * recurring swap works now, need more finetuning * add exceptions for multiple auto swaps and swapping in with active auto swap * black * auto reverse swap actually works :) * remove swap status dialogs * update to boltz_client 0.0.9 * update to boltz-client 0.1.1, and fix startup * update requirement.txt for boltz-client * fixup columns in table, remove unused payment.extra, change deezy label * remove balance check for auto swap out * update boltzc-lient to 0.1.2, fix mypy issue inside boltz package * nitpicks calle tasks.py * calle nitpicks crud * calle nitpicks crud * refactor * fix formatting * circular import * black :) Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
import asyncio
|
|
import calendar
|
|
import datetime
|
|
from typing import Awaitable
|
|
|
|
from boltz_client.boltz import BoltzClient, BoltzConfig
|
|
|
|
from lnbits.core.services import fee_reserve, get_wallet, pay_invoice
|
|
from lnbits.settings import settings
|
|
|
|
from .models import ReverseSubmarineSwap
|
|
|
|
|
|
def create_boltz_client() -> BoltzClient:
|
|
config = BoltzConfig(
|
|
network=settings.boltz_network,
|
|
api_url=settings.boltz_url,
|
|
mempool_url=f"{settings.boltz_mempool_space_url}/api",
|
|
mempool_ws_url=f"{settings.boltz_mempool_space_url_ws}/api/v1/ws",
|
|
referral_id="lnbits",
|
|
)
|
|
return BoltzClient(config)
|
|
|
|
|
|
async def check_balance(data) -> bool:
|
|
# check if we can pay the invoice before we create the actual swap on boltz
|
|
amount_msat = data.amount * 1000
|
|
fee_reserve_msat = fee_reserve(amount_msat)
|
|
wallet = await get_wallet(data.wallet)
|
|
assert wallet
|
|
if wallet.balance_msat - fee_reserve_msat < amount_msat:
|
|
return False
|
|
return True
|
|
|
|
|
|
def get_timestamp():
|
|
date = datetime.datetime.utcnow()
|
|
return calendar.timegm(date.utctimetuple())
|
|
|
|
|
|
async def execute_reverse_swap(client, swap: ReverseSubmarineSwap):
|
|
# claim_task is watching onchain address for the lockup transaction to arrive / confirm
|
|
# and if the lockup is there, claim the onchain revealing preimage for hold invoice
|
|
claim_task = asyncio.create_task(
|
|
client.claim_reverse_swap(
|
|
privkey_wif=swap.claim_privkey,
|
|
preimage_hex=swap.preimage,
|
|
lockup_address=swap.lockup_address,
|
|
receive_address=swap.onchain_address,
|
|
redeem_script_hex=swap.redeem_script,
|
|
)
|
|
)
|
|
# pay_task is paying the hold invoice which gets held until you reveal your preimage when claiming your onchain funds
|
|
pay_task = pay_invoice_and_update_status(
|
|
swap.id,
|
|
claim_task,
|
|
pay_invoice(
|
|
wallet_id=swap.wallet,
|
|
payment_request=swap.invoice,
|
|
description=f"reverse swap for {swap.onchain_amount} sats on boltz.exchange",
|
|
extra={"tag": "boltz", "swap_id": swap.id, "reverse": True},
|
|
),
|
|
)
|
|
|
|
# they need to run be concurrently, because else pay_task will lock the eventloop and claim_task will not be executed.
|
|
# the lockup transaction can only happen after you pay the invoice, which cannot be redeemed immediatly -> hold invoice
|
|
# after getting the lockup transaction, you can claim the onchain funds revealing the preimage for boltz to redeem the hold invoice
|
|
asyncio.gather(claim_task, pay_task)
|
|
|
|
|
|
def pay_invoice_and_update_status(
|
|
swap_id: str, wstask: asyncio.Task, awaitable: Awaitable
|
|
) -> asyncio.Task:
|
|
async def _pay_invoice(awaitable):
|
|
from .crud import update_swap_status
|
|
|
|
try:
|
|
awaited = await awaitable
|
|
await update_swap_status(swap_id, "complete")
|
|
return awaited
|
|
except asyncio.exceptions.CancelledError:
|
|
"""lnbits process was exited, do nothing and handle it in startup script"""
|
|
except:
|
|
wstask.cancel()
|
|
await update_swap_status(swap_id, "failed")
|
|
|
|
return asyncio.create_task(_pay_invoice(awaitable))
|