mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-03-04 01:53:59 +01:00
Upgrade conversion endpoint (#491)
This commit is contained in:
parent
fc021b3736
commit
918e9be7ed
2 changed files with 24 additions and 4 deletions
|
@ -3,7 +3,7 @@ import hashlib
|
|||
import json
|
||||
from binascii import unhexlify
|
||||
from http import HTTPStatus
|
||||
from typing import Dict, Optional, Union
|
||||
from typing import Dict, List, Optional, Union
|
||||
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
|
||||
|
||||
import httpx
|
||||
|
@ -12,6 +12,7 @@ from fastapi.exceptions import HTTPException
|
|||
from fastapi.param_functions import Depends
|
||||
from fastapi.params import Body
|
||||
from pydantic import BaseModel
|
||||
from pydantic.fields import Field
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
|
||||
from lnbits import bolt11, lnurl
|
||||
|
@ -25,7 +26,11 @@ from lnbits.decorators import (
|
|||
)
|
||||
from lnbits.helpers import url_for
|
||||
from lnbits.requestvars import g
|
||||
from lnbits.utils.exchange_rates import currencies, fiat_amount_as_satoshis
|
||||
from lnbits.utils.exchange_rates import (
|
||||
currencies,
|
||||
fiat_amount_as_satoshis,
|
||||
satoshis_amount_as_fiat,
|
||||
)
|
||||
|
||||
from .. import core_app, db
|
||||
from ..crud import (
|
||||
|
@ -504,9 +509,21 @@ async def api_list_currencies_available():
|
|||
|
||||
|
||||
class ConversionData(BaseModel):
|
||||
unit: str
|
||||
from_: str = Field('sat', alias="from")
|
||||
amount: float
|
||||
to: str = Query('usd')
|
||||
|
||||
@core_app.post("/api/v1/conversion")
|
||||
async def api_fiat_as_sats(data: ConversionData):
|
||||
return await fiat_amount_as_satoshis(data.amount, data.unit)
|
||||
output = {}
|
||||
if data.from_ == 'sat':
|
||||
output["sats"] = int(data.amount)
|
||||
output["BTC"] = data.amount / 100000000
|
||||
for currency in data.to.split(','):
|
||||
output[currency.strip().upper()] = await satoshis_amount_as_fiat(data.amount, currency.strip())
|
||||
return output
|
||||
else:
|
||||
output[data.from_.upper()] = data.amount
|
||||
output["sats"] = await fiat_amount_as_satoshis(data.amount, data.to)
|
||||
output["BTC"] = output["sats"] / 100000000
|
||||
return output
|
||||
|
|
|
@ -284,3 +284,6 @@ async def get_fiat_rate_satoshis(currency: str) -> float:
|
|||
|
||||
async def fiat_amount_as_satoshis(amount: float, currency: str) -> int:
|
||||
return int(amount * (await get_fiat_rate_satoshis(currency)))
|
||||
|
||||
async def satoshis_amount_as_fiat(amount: float, currency: str) -> float:
|
||||
return float(amount / (await get_fiat_rate_satoshis(currency)))
|
||||
|
|
Loading…
Add table
Reference in a new issue