Boltz.exchange Extension (#922)

* initial commit and still draft, ready for review

* forgot to uncomment this line

* fee estimation and blockheight

* resolve conversation with michael, to use mempool websockets instead of boltz status event

* Update lnbits/extensions/boltz/boltz.py

Co-authored-by: michael1011 <me@michael1011.at>

* add status to swaps, add sorting and data into listing

* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits

* fix docker hosts for development

* add api endpoints to _api_docs

* add wallet name and id, to list and status information

* fix status_update for reverse_swaps

* chore: format with black

* more blackformatting and refactoring create_swap()

* fix variable bug

* check if swap is already refunded

* use create_task instead of ensure_future

* add mempool and boltz urls depending on DEBUG .env

* raise exception in mempool fails

* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand

* add status to swaps, add sorting and data into listing

* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits

* add wallet name and id, to list and status information

* fix status_update for reverse_swaps

* chore: format with black

* use create_task instead of ensure_future

* add mempool and boltz urls depending on DEBUG .env

* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand

* black formatting

* add some logging with loguru, and remove function duplication

* cleanup readme

* updates/suggestions from calle

Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>

* remove unused comments

* Update API Endpoints

Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>

* un-factor get_boltz_pairs

* added a explaination for the onchain tx

* remove unused template file

* rename api endpoints

* fix isort and prettier

* more verbose logging!!

* add boltz to mock_data.zip

* new mockdata

* remove comment

* better readme

* fix mempool urls

* change /refund /check /status to post requests

* first step in tests2

* add first tests

* change refund,check,status to post requests

* next try on tests

* overall code improvements

* just testing tests

* throw http exceptions in views_api

* require admincheck for refund,check,status and added fastapi documentation for those

* added more tests

* black

* many code improvements

* adding tests

* temp fix test

* fix race condition when pay_invoice fails

* test are working

* add boltz env variables

* add startup check, bugfixes, improvements

* improve on status checking

* remove check_invoice_status

* more fixes and tests

* testing testing testing

* make tests run again inside regtest

* fix bad error :O

* fix postgres boolean bug and add swap test

* Update README.md

Update README.md

Update README.md

Update README.md

* some mypy

* blacked

* the missing commit?

* fix api_docs readme link

* better refunding error catching

fix

* check swaps now also shows pending reverse swap, ui improvements, tooltips

* add backend check for boltz limits

fixup

* many improvements, startup check for swaps working, reverse needs more testing

* little last fixes

* remove unused logic

* fastapi documentation

fixup

* formatting and remove unused tests

* fix test

* fix swapstatus model

* Update lnbits/extensions/boltz/tasks.py

Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>

* Update lnbits/extensions/boltz/views_api.py

Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>

* balance check msg, format

* fix mypy data override

* fix swapstatus, remove can refund column

* Update lnbits/extensions/boltz/README.md

Co-authored-by: michael1011 <me@michael1011.at>

* empty lines

* fix error message when swap is not found

* remove preimage_hash from database

* fix api_docs html

fix api_docs html

* catch boltz network exceptions better

* formatting

* check for timeout on swap at get request

Co-authored-by: michael1011 <me@michael1011.at>
Co-authored-by: fusion44 <some.fusion@gmail.com>
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
dni ⚡ 2022-08-30 12:51:17 +02:00 committed by GitHub
parent 5fecb02b8d
commit 78a98ca97d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 2991 additions and 27 deletions

View file

@ -13,7 +13,7 @@ from lnbits.core.views.api import (
)
from lnbits.settings import wallet_class
from ...helpers import get_random_invoice_data
from ...helpers import get_random_invoice_data, is_regtest
# check if the client is working
@ -162,6 +162,7 @@ async def test_pay_invoice_invoicekey(client, invoice, inkey_headers_from):
# check POST /api/v1/payments: payment with admin key [should pass]
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="this only works in fakewallet")
async def test_pay_invoice_adminkey(client, invoice, adminkey_headers_from):
data = {"out": True, "bolt11": invoice["payment_request"]}
# try payment with admin key

Binary file not shown.

View file

@ -12,7 +12,7 @@ from lnbits.extensions.bleskomat.helpers import (
from lnbits.settings import HOST, PORT
from tests.conftest import client
from tests.extensions.bleskomat.conftest import bleskomat, lnurl
from tests.helpers import credit_wallet
from tests.helpers import credit_wallet, is_regtest
from tests.mocks import WALLET
@ -97,6 +97,7 @@ async def test_bleskomat_lnurl_api_valid_signature(client, bleskomat):
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="this test is only passes in fakewallet")
async def test_bleskomat_lnurl_api_action_insufficient_balance(client, lnurl):
bleskomat = lnurl["bleskomat"]
secret = lnurl["secret"]
@ -116,6 +117,7 @@ async def test_bleskomat_lnurl_api_action_insufficient_balance(client, lnurl):
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="this test is only passes in fakewallet")
async def test_bleskomat_lnurl_api_action_success(client, lnurl):
bleskomat = lnurl["bleskomat"]
secret = lnurl["secret"]

View file

View file

@ -0,0 +1,25 @@
import asyncio
import json
import secrets
import pytest
import pytest_asyncio
from lnbits.core.crud import create_account, create_wallet, get_wallet
from lnbits.extensions.boltz.boltz import create_reverse_swap, create_swap
from lnbits.extensions.boltz.models import (
CreateReverseSubmarineSwap,
CreateSubmarineSwap,
)
from tests.mocks import WALLET
@pytest_asyncio.fixture(scope="session")
async def reverse_swap(from_wallet):
data = CreateReverseSubmarineSwap(
wallet=from_wallet.id,
instant_settlement=True,
onchain_address="bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43",
amount=20_000,
)
return await create_reverse_swap(data)

View file

@ -0,0 +1,146 @@
import pytest
import pytest_asyncio
from tests.helpers import is_fake, is_regtest
@pytest.mark.asyncio
async def test_mempool_url(client):
response = await client.get("/boltz/api/v1/swap/mempool")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_boltz_config(client):
response = await client.get("/boltz/api/v1/swap/boltz")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_endpoints_unauthenticated(client):
response = await client.get("/boltz/api/v1/swap?all_wallets=true")
assert response.status_code == 401
response = await client.get("/boltz/api/v1/swap/reverse?all_wallets=true")
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap")
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/reverse")
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/status")
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/check")
assert response.status_code == 401
@pytest.mark.asyncio
async def test_endpoints_inkey(client, inkey_headers_to):
response = await client.get(
"/boltz/api/v1/swap?all_wallets=true", headers=inkey_headers_to
)
assert response.status_code == 200
response = await client.get(
"/boltz/api/v1/swap/reverse?all_wallets=true", headers=inkey_headers_to
)
assert response.status_code == 200
response = await client.post("/boltz/api/v1/swap", headers=inkey_headers_to)
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/reverse", headers=inkey_headers_to)
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/refund", headers=inkey_headers_to)
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/status", headers=inkey_headers_to)
assert response.status_code == 401
response = await client.post("/boltz/api/v1/swap/check", headers=inkey_headers_to)
assert response.status_code == 401
@pytest.mark.asyncio
async def test_endpoints_adminkey_nocontent(client, adminkey_headers_to):
response = await client.post("/boltz/api/v1/swap", headers=adminkey_headers_to)
assert response.status_code == 204
response = await client.post(
"/boltz/api/v1/swap/reverse", headers=adminkey_headers_to
)
assert response.status_code == 204
response = await client.post(
"/boltz/api/v1/swap/refund", headers=adminkey_headers_to
)
assert response.status_code == 204
response = await client.post(
"/boltz/api/v1/swap/status", headers=adminkey_headers_to
)
assert response.status_code == 204
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="this test is only passes with fakewallet")
async def test_endpoints_adminkey_fakewallet(client, from_wallet, adminkey_headers_to):
response = await client.post(
"/boltz/api/v1/swap/check", headers=adminkey_headers_to
)
assert response.status_code == 200
swap = {
"wallet": from_wallet.id,
"refund_address": "bcrt1q3cwq33y435h52gq3qqsdtczh38ltlnf69zvypm",
"amount": 50_000,
}
response = await client.post(
"/boltz/api/v1/swap", json=swap, headers=adminkey_headers_to
)
assert response.status_code == 405
reverse_swap = {
"wallet": from_wallet.id,
"instant_settlement": True,
"onchain_address": "bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43",
"amount": 50_000,
}
response = await client.post(
"/boltz/api/v1/swap/reverse", json=reverse_swap, headers=adminkey_headers_to
)
assert response.status_code == 201
reverse_swap = response.json()
assert reverse_swap["id"] is not None
response = await client.post(
"/boltz/api/v1/swap/status",
params={"swap_id": reverse_swap["id"]},
headers=adminkey_headers_to,
)
assert response.status_code == 200
response = await client.post(
"/boltz/api/v1/swap/status",
params={"swap_id": "wrong"},
headers=adminkey_headers_to,
)
assert response.status_code == 404
response = await client.post(
"/boltz/api/v1/swap/refund",
params={"swap_id": "wrong"},
headers=adminkey_headers_to,
)
assert response.status_code == 404
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="this test is only passes with regtest")
async def test_endpoints_adminkey_regtest(client, from_wallet, adminkey_headers_to):
swap = {
"wallet": from_wallet.id,
"refund_address": "bcrt1q3cwq33y435h52gq3qqsdtczh38ltlnf69zvypm",
"amount": 50_000,
}
response = await client.post(
"/boltz/api/v1/swap", json=swap, headers=adminkey_headers_to
)
assert response.status_code == 201
reverse_swap = {
"wallet": from_wallet.id,
"instant_settlement": True,
"onchain_address": "bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43",
"amount": 50_000,
}
response = await client.post(
"/boltz/api/v1/swap/reverse", json=reverse_swap, headers=adminkey_headers_to
)
assert response.status_code == 201

View file

@ -0,0 +1,31 @@
import asyncio
import pytest
import pytest_asyncio
from lnbits.extensions.boltz.boltz import create_reverse_swap, create_swap
from lnbits.extensions.boltz.crud import (
create_reverse_submarine_swap,
create_submarine_swap,
get_reverse_submarine_swap,
get_submarine_swap,
)
from tests.extensions.boltz.conftest import reverse_swap
from tests.helpers import is_fake, is_regtest
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="this test is only passes in regtest")
async def test_create_reverse_swap(client, reverse_swap):
swap, wait_for_onchain = reverse_swap
assert swap.status == "pending"
assert swap.id is not None
assert swap.boltz_id is not None
assert swap.claim_privkey is not None
assert swap.onchain_address is not None
assert swap.lockup_address is not None
newswap = await create_reverse_submarine_swap(swap)
await wait_for_onchain
newswap = await get_reverse_submarine_swap(swap.id)
assert newswap is not None
assert newswap.status == "complete"

View file

@ -4,6 +4,7 @@ import secrets
import string
from lnbits.core.crud import create_payment
from lnbits.settings import wallet_class
async def credit_wallet(wallet_id: str, amount: int):
@ -32,3 +33,7 @@ def get_random_string(N=10):
async def get_random_invoice_data():
return {"out": False, "amount": 10, "memo": f"test_memo_{get_random_string(10)}"}
is_fake: bool = wallet_class.__name__ == "FakeWallet"
is_regtest: bool = not is_fake

View file

@ -5,7 +5,7 @@ from lnbits.settings import WALLET
from lnbits.wallets.base import PaymentResponse, PaymentStatus, StatusResponse
from lnbits.wallets.fake import FakeWallet
from .helpers import get_random_string
from .helpers import get_random_string, is_fake
# generates an invoice with FakeWallet
@ -16,12 +16,13 @@ async def generate_mock_invoice(**x):
return invoice
WALLET.status = AsyncMock(
return_value=StatusResponse(
"", # no error
1000000, # msats
if is_fake:
WALLET.status = AsyncMock(
return_value=StatusResponse(
"", # no error
1000000, # msats
)
)
)
# Note: if this line is uncommented, invoices will always be generated by FakeWallet
# WALLET.create_invoice = generate_mock_invoice
@ -51,26 +52,27 @@ WALLET.status = AsyncMock(
# )
def pay_invoice_side_effect(
payment_request: str, fee_limit_msat: int
) -> PaymentResponse:
invoice = bolt11.decode(payment_request)
return PaymentResponse(
True, # ok
invoice.payment_hash, # checking_id (i.e. payment_hash)
0, # fee_msat
"", # no error
)
if is_fake:
def pay_invoice_side_effect(
payment_request: str, fee_limit_msat: int
) -> PaymentResponse:
invoice = bolt11.decode(payment_request)
return PaymentResponse(
True, # ok
invoice.payment_hash, # checking_id (i.e. payment_hash)
0, # fee_msat
"", # no error
)
WALLET.pay_invoice = AsyncMock(side_effect=pay_invoice_side_effect)
WALLET.get_invoice_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
WALLET.pay_invoice = AsyncMock(side_effect=pay_invoice_side_effect)
WALLET.get_invoice_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
)
)
)
WALLET.get_payment_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
WALLET.get_payment_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
)
)
)