Wallet: Lightning interface (#318)

* mint does not start yet

* fix import

* revert mint db migrations

* handle zero fee case

* cli: adjust fee message

* wallet: replace requests with httpx

* clean up

* rename http client decorator

* fix pending check in main, todo: TEST PROXIES WITH HTTPX

* fix up

* use httpx for nostr as well

* update packages to same versions as https://github.com/lnbits/lnbits/pull/1609/files

* fix proof deserialization

* check for string

* tests passing

* adjust wallet api tests

* lockfile

* add correct responses to Lightning interface and delete melt_id for proofs for which the payent has failed

* fix create_invoice checking_id response

* migrations atomic

* proofs are stored automatically when created

* make format

* use bolt11 lib

* stricter type checking

* add fee response to payments

* assert fees in test_melt

* test that mint_id and melt_id is stored correctly in proofs and proofs_used

* remove traces

* refactor: Lightning interface into own file and LedgerCrud with typing

* fix tests

* fix payment response

* rename variable
This commit is contained in:
callebtc
2023-10-21 14:38:16 +02:00
committed by GitHub
parent 8a4813aee6
commit 0490f20932
41 changed files with 1899 additions and 1664 deletions

View File

@@ -1,8 +1,10 @@
import asyncio
import pytest
import pytest_asyncio
from fastapi.testclient import TestClient
from cashu.core.settings import settings
from cashu.lightning.base import InvoiceResponse, PaymentStatus
from cashu.wallet.api.app import app
from cashu.wallet.wallet import Wallet
from tests.conftest import SERVER_ENDPOINT
@@ -23,25 +25,19 @@ async def wallet(mint):
@pytest.mark.asyncio
async def test_invoice(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/invoice?amount=100")
response = client.post("/lightning/create_invoice?amount=100")
assert response.status_code == 200
if settings.lightning:
assert response.json()["invoice"]
else:
assert response.json()["amount"]
@pytest.mark.asyncio
async def test_invoice_with_split(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/invoice?amount=10&split=1")
assert response.status_code == 200
if settings.lightning:
assert response.json()["invoice"]
else:
assert response.json()["amount"]
# await wallet.load_proofs(reload=True)
# assert wallet.proof_amounts.count(1) >= 10
invoice_response = InvoiceResponse.parse_obj(response.json())
state = PaymentStatus(paid=False)
while not state.paid:
print("checking invoice state")
response2 = client.get(
f"/lightning/invoice_state?payment_hash={invoice_response.checking_id}"
)
state = PaymentStatus.parse_obj(response2.json())
await asyncio.sleep(0.1)
print("state:", state)
print("paid")
@pytest.mark.asyncio
@@ -49,7 +45,7 @@ async def test_balance():
with TestClient(app) as client:
response = client.get("/balance")
assert response.status_code == 200
assert response.json()["balance"]
assert "balance" in response.json()
assert response.json()["keysets"]
assert response.json()["mints"]
@@ -89,7 +85,7 @@ async def test_receive_all(wallet: Wallet):
with TestClient(app) as client:
response = client.post("/receive?all=true")
assert response.status_code == 200
assert response.json()["initial_balance"] == 0
assert response.json()["initial_balance"]
assert response.json()["balance"]
@@ -100,24 +96,21 @@ async def test_burn_all(wallet: Wallet):
assert response.status_code == 200
response = client.post("/burn?all=true")
assert response.status_code == 200
assert response.json()["balance"] == 0
assert response.json()["balance"]
@pytest.mark.asyncio
async def test_pay():
with TestClient(app) as client:
invoice = (
"lnbc100n1pjzp22cpp58xvjxvagzywky9xz3vurue822aaax"
"735hzc5pj5fg307y58v5znqdq4vdshx6r4ypjx2ur0wd5hgl"
"h6ahauv24wdmac4zk478pmwfzd7sdvm8tje3dmfue3lc2g4l"
"9g40a073h39748uez9p8mxws5vqwjmkqr4wl5l7n4dlhj6z6"
"va963cqvufrs4"
"lnbc100n1pjjcqzfdq4gdshx6r4ypjx2ur0wd5hgpp58xvj8yn00d5"
"7uhshwzcwgy9uj3vwf5y2lr5fjf78s4w9l4vhr6xssp5stezsyty9r"
"hv3lat69g4mhqxqun56jyehhkq3y8zufh83xyfkmmq4usaqwrt5q4f"
"adm44g6crckp0hzvuyv9sja7t65hxj0ucf9y46qstkay7gfnwhuxgr"
"krf7djs38rml39l8wpn5ug9shp3n55quxhdecqfwxg23"
)
response = client.post(f"/pay?invoice={invoice}")
if not settings.lightning:
assert response.status_code == 400
else:
assert response.status_code == 200
response = client.post(f"/lightning/pay_invoice?bolt11={invoice}")
assert response.status_code == 200
@pytest.mark.asyncio
@@ -159,21 +152,31 @@ async def test_info():
@pytest.mark.asyncio
async def test_flow(wallet: Wallet):
with TestClient(app) as client:
if not settings.lightning:
response = client.get("/balance")
initial_balance = response.json()["balance"]
response = client.post("/invoice?amount=100")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 100
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 50
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance
response = client.get("/pending")
token = response.json()["pending_token"]["0"]["token"]
amount = response.json()["pending_token"]["0"]["amount"]
response = client.post(f"/receive?token={token}")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + amount
response = client.get("/balance")
initial_balance = response.json()["balance"]
response = client.post("/lightning/create_invoice?amount=100")
invoice_response = InvoiceResponse.parse_obj(response.json())
state = PaymentStatus(paid=False)
while not state.paid:
print("checking invoice state")
response2 = client.get(
f"/lightning/invoice_state?payment_hash={invoice_response.checking_id}"
)
state = PaymentStatus.parse_obj(response2.json())
await asyncio.sleep(0.1)
print("state:", state)
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 100
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + 50
response = client.post("/send?amount=50")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance
response = client.get("/pending")
token = response.json()["pending_token"]["0"]["token"]
amount = response.json()["pending_token"]["0"]["amount"]
response = client.post(f"/receive?token={token}")
response = client.get("/balance")
assert response.json()["balance"] == initial_balance + amount