lnbits-legend/lnbits/extensions/gerty/views_api.py

170 lines
5.4 KiB
Python
Raw Normal View History

2022-09-23 14:38:55 +01:00
from http import HTTPStatus
import json
import httpx
import random
import os
from fastapi import Query
from fastapi.params import Depends
from lnurl import decode as decode_lnurl
from loguru import logger
from starlette.exceptions import HTTPException
2022-09-26 18:06:09 +01:00
from lnbits.core.crud import get_wallet_for_key
2022-09-23 14:38:55 +01:00
from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice
2022-09-26 16:16:41 +01:00
from lnbits.core.views.api import api_payment, api_wallet
2022-09-23 14:38:55 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from fastapi.templating import Jinja2Templates
from . import gerty_ext
2022-09-26 16:16:41 +01:00
from .crud import create_gerty, update_gerty, delete_gerty, get_gerty, get_gertys
2022-09-23 14:38:55 +01:00
from .models import Gerty
2022-09-26 18:06:09 +01:00
from lnbits.utils.exchange_rates import satoshis_amount_as_fiat
2022-09-23 14:38:55 +01:00
from ...settings import LNBITS_PATH
@gerty_ext.get("/api/v1/gerty", status_code=HTTPStatus.OK)
async def api_gertys(
all_wallets: bool = Query(False), wallet: WalletTypeInfo = Depends(get_key_type)
):
wallet_ids = [wallet.wallet.id]
if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return [gerty.dict() for gerty in await get_gertys(wallet_ids)]
@gerty_ext.post("/api/v1/gerty", status_code=HTTPStatus.CREATED)
@gerty_ext.put("/api/v1/gerty/{gerty_id}", status_code=HTTPStatus.OK)
async def api_link_create_or_update(
data: Gerty,
wallet: WalletTypeInfo = Depends(get_key_type),
gerty_id: str = Query(None),
):
if gerty_id:
gerty = await get_gerty(gerty_id)
if not gerty:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist"
)
if gerty.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Come on, seriously, this isn't your Gerty!",
)
data.wallet = wallet.wallet.id
gerty = await update_gerty(gerty_id, **data.dict())
else:
gerty = await create_gerty(wallet_id=wallet.wallet.id, data=data)
return {**gerty.dict()}
@gerty_ext.delete("/api/v1/gerty/{gerty_id}")
async def api_gerty_delete(
gerty_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
gerty = await get_gerty(gerty_id)
if not gerty:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist."
)
if gerty.wallet != wallet.wallet.id:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your Gerty.")
await delete_gerty(gerty_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
#######################
with open(os.path.join(LNBITS_PATH, 'extensions/gerty/static/satoshi.json')) as fd:
satoshiQuotes = json.load(fd)
@gerty_ext.get("/api/v1/gerty/satoshiquote", status_code=HTTPStatus.OK)
async def api_gerty_satoshi():
return satoshiQuotes[random.randint(0, 100)]
@gerty_ext.get("/api/v1/gerty/{gerty_id}")
async def api_gerty_json(
2022-09-26 10:30:48 +01:00
gerty_id: str
2022-09-23 14:38:55 +01:00
):
gerty = await get_gerty(gerty_id)
2022-09-26 10:30:48 +01:00
logger.debug(gerty.wallet)
2022-09-23 14:38:55 +01:00
if not gerty:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Gerty does not exist."
)
gertyReturn = []
2022-09-26 18:06:09 +01:00
# Get Wallet info
wallets = ['wallets']
2022-09-23 14:38:55 +01:00
if gerty.lnbits_wallets != "":
2022-09-26 16:16:41 +01:00
for lnbits_wallet in json.loads(gerty.lnbits_wallets):
2022-09-26 18:06:09 +01:00
wallet = await get_wallet_for_key(key=lnbits_wallet)
2022-09-26 19:30:40 +01:00
if wallet:
wallets.append({
"name": wallet.name,
"balance": wallet.balance_msat,
"inkey": wallet.inkey,
})
gertyReturn.append(wallets)
2022-09-26 18:06:09 +01:00
#Get Satoshi quotes
satoshi = ['sats_quote']
2022-09-23 14:38:55 +01:00
if gerty.sats_quote:
2022-09-26 19:30:40 +01:00
quote = await api_gerty_satoshi()
if quote:
satoshi.append(await api_gerty_satoshi())
gertyReturn.append(satoshi)
2022-09-23 14:38:55 +01:00
2022-09-26 18:06:09 +01:00
#Get Exchange Value
exchange = ['exchange']
2022-09-23 14:38:55 +01:00
if gerty.exchange != "":
try:
2022-09-26 19:30:40 +01:00
amount = await satoshis_amount_as_fiat(100000000, gerty.exchange)
if amount:
exchange.append({
"fiat": gerty.exchange,
"amount": amount,
})
2022-09-23 14:38:55 +01:00
except:
pass
2022-09-26 19:30:40 +01:00
gertyReturn.append(exchange)
2022-09-26 18:06:09 +01:00
onchain = ['onchain']
2022-09-26 19:30:40 +01:00
if gerty.onchain_stats and isinstance(gerty.mempool_endpoint, str):
2022-09-23 14:38:55 +01:00
async with httpx.AsyncClient() as client:
2022-09-26 18:06:09 +01:00
difficulty = ['difficulty']
2022-09-23 14:38:55 +01:00
r = await client.get(gerty.mempool_endpoint + "/api/v1/difficulty-adjustment")
2022-09-26 19:30:40 +01:00
if r:
difficulty.append(r.json())
onchain.append(difficulty)
2022-09-26 18:06:09 +01:00
mempool = ['mempool']
2022-09-23 14:38:55 +01:00
r = await client.get(gerty.mempool_endpoint + "/api/v1/fees/mempool-blocks")
2022-09-26 19:30:40 +01:00
if r:
mempool.append(r.json())
onchain.append(mempool)
2022-09-26 18:06:09 +01:00
threed = ['threed']
2022-09-23 14:38:55 +01:00
r = await client.get(gerty.mempool_endpoint + "/api/v1/mining/hashrate/3d")
2022-09-26 19:30:40 +01:00
if r:
threed.append(r.json())
onchain.append(threed)
gertyReturn.append(onchain)
2022-09-23 14:38:55 +01:00
2022-09-26 18:06:09 +01:00
ln = ['ln']
2022-09-26 19:30:40 +01:00
if gerty.ln_stats and isinstance(gerty.mempool_endpoint, str):
2022-09-23 14:38:55 +01:00
async with httpx.AsyncClient() as client:
r = await client.get(gerty.mempool_endpoint + "/api/v1/lightning/statistics/latest")
2022-09-26 19:30:40 +01:00
if r:
ln.append(r.json())
gertyReturn.append(ln)
2022-09-23 14:38:55 +01:00
return gertyReturn