track fiat value of payments (#1789)
* save fiat_amounts on payment creation * show fiat amount in frontend * add lnbits_default_accounting_currency * extract fiat calculation logic into service * move all currency conversions to calc_fiat_amounts move all conversion logic into create_invoice so extensions can benefit aswell * show user-defined and wallet-defined currency in frontend * remove sat from fiat units * make bundle * improve tests * debug log
This commit is contained in:
parent
7a37e72915
commit
2623e9247a
14 changed files with 233 additions and 42 deletions
|
|
@ -10,6 +10,7 @@ from lnbits.core.models import Payment
|
|||
from lnbits.core.views.admin_api import api_auditor
|
||||
from lnbits.core.views.api import api_payment
|
||||
from lnbits.db import DB_TYPE, SQLITE
|
||||
from lnbits.settings import settings
|
||||
from lnbits.wallets import get_wallet_class
|
||||
from tests.conftest import CreateInvoice, api_payments_create_invoice
|
||||
|
||||
|
|
@ -96,6 +97,13 @@ async def test_create_invoice_fiat_amount(client, inkey_headers_to):
|
|||
decode = bolt11.decode(invoice["payment_request"])
|
||||
assert decode.amount_msat != data["amount"] * 1000
|
||||
|
||||
response = await client.get("/api/v1/payments?limit=1", headers=inkey_headers_to)
|
||||
assert response.is_success
|
||||
extra = response.json()[0]["extra"]
|
||||
assert extra["fiat_amount"] == data["amount"]
|
||||
assert extra["fiat_currency"] == data["unit"]
|
||||
assert extra["fiat_rate"]
|
||||
|
||||
|
||||
# check POST /api/v1/payments: invoice creation for internal payments only
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -357,6 +365,65 @@ async def test_create_invoice_with_unhashed_description(client, inkey_headers_to
|
|||
return invoice
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_wallet(client, adminkey_headers_from):
|
||||
name = "new name"
|
||||
currency = "EUR"
|
||||
|
||||
response = await client.patch(
|
||||
"/api/v1/wallet", json={"name": name}, headers=adminkey_headers_from
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["name"] == name
|
||||
|
||||
response = await client.patch(
|
||||
"/api/v1/wallet", json={"currency": currency}, headers=adminkey_headers_from
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["currency"] == currency
|
||||
# name is not changed because updates are partial
|
||||
assert response.json()["name"] == name
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fiat_tracking(client, adminkey_headers_from):
|
||||
async def create_invoice():
|
||||
data = await get_random_invoice_data()
|
||||
response = await client.post(
|
||||
"/api/v1/payments", json=data, headers=adminkey_headers_from
|
||||
)
|
||||
assert response.is_success
|
||||
|
||||
response = await client.get(
|
||||
f"/api/v1/payments/{response.json()['payment_hash']}",
|
||||
headers=adminkey_headers_from,
|
||||
)
|
||||
assert response.is_success
|
||||
return response.json()["details"]
|
||||
|
||||
async def update_currency(currency):
|
||||
response = await client.patch(
|
||||
"/api/v1/wallet", json={"currency": currency}, headers=adminkey_headers_from
|
||||
)
|
||||
assert response.is_success
|
||||
assert response.json()["currency"] == currency
|
||||
|
||||
await update_currency("")
|
||||
|
||||
settings.lnbits_default_accounting_currency = "USD"
|
||||
payment = await create_invoice()
|
||||
assert payment["extra"]["wallet_fiat_currency"] == "USD"
|
||||
assert payment["extra"]["wallet_fiat_amount"] != payment["amount"]
|
||||
assert payment["extra"]["wallet_fiat_rate"]
|
||||
|
||||
await update_currency("EUR")
|
||||
|
||||
payment = await create_invoice()
|
||||
assert payment["extra"]["wallet_fiat_currency"] == "EUR"
|
||||
assert payment["extra"]["wallet_fiat_amount"] != payment["amount"]
|
||||
assert payment["extra"]["wallet_fiat_rate"]
|
||||
|
||||
|
||||
async def get_node_balance_sats():
|
||||
audit = await api_auditor()
|
||||
return audit["node_balance_msats"] / 1000
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue