cleaned up

This commit is contained in:
ben
2022-11-24 23:31:22 +00:00
parent e6a3c63dd5
commit e84eedd66f
6 changed files with 81 additions and 257 deletions
+37 -130
View File
@@ -1,18 +1,14 @@
from typing import List, Optional, Union
import httpx
from lnbits.helpers import urlsafe_short_hash
import time
import json
from loguru import logger
from . import db
from .models import (
Gerty,
Mempool,
Fees_recommended,
Hashrate_1w,
Hashrate_1m,
Statistics,
Difficulty_adjustment,
Tip_height)
from .models import Gerty, Mempool, MempoolEndpoint
async def create_gerty(wallet_id: str, data: Gerty) -> Gerty:
gerty_id = urlsafe_short_hash()
@@ -21,9 +17,9 @@ async def create_gerty(wallet_id: str, data: Gerty) -> Gerty:
INSERT INTO gerty.gertys (
id,
name,
wallet,
utc_offset,
type,
wallet,
lnbits_wallets,
mempool_endpoint,
exchange,
@@ -35,9 +31,9 @@ async def create_gerty(wallet_id: str, data: Gerty) -> Gerty:
(
gerty_id,
data.name,
data.wallet,
data.utc_offset,
data.type,
wallet_id,
data.lnbits_wallets,
data.mempool_endpoint,
data.exchange,
@@ -82,124 +78,35 @@ async def delete_gerty(gerty_id: str) -> None:
#############MEMPOOL###########
async def get_fees_recommended(gerty) -> Optional[Fees_recommended]:
row = await db.fetchone("SELECT * FROM gerty.fees_recommended", ())
async def get_mempool_info(endPoint: str, gerty) -> Optional[Mempool]:
endpoints = MempoolEndpoint()
url = ""
for endpoint in endpoints:
logger.debug(endpoint)
if endPoint == endpoint[0]:
url = endpoint[1]
row = await db.fetchone("SELECT * FROM gerty.mempool WHERE endpoint = ?", (endPoint,))
if not row:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + url)
await db.execute(
"""
INSERT INTO gerty.mempool (
endpoint,
data,
time,
)
VALUES (?, ?, ?)
""",
(endPoint, json.dumps(response.json()), int(time.time())),
)
return response.json()
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/v1/fees/recommended")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.fees_recommended
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Fees_recommended(**response) if response else None
else:
return Fees_recommended(**row) if row else None
async def get_hashrate_1w(gerty) -> Optional[Hashrate_1w]:
row = await db.fetchone("SELECT * FROM gerty.hashrate_1w", ())
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/v1/mining/hashrate/1w")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.hashrate_1w
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Hashrate_1w(**response) if response else None
else:
return Hashrate_1w(**row) if row else None
async def get_hashrate_1m(gerty) -> Optional[Hashrate_1m]:
row = await db.fetchone("SELECT * FROM gerty.hashrate_1m", ())
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/v1/mining/hashrate/1m")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.hashrate_1m
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Hashrate_1m(**response) if response else None
else:
return Hashrate_1m(**row) if row else None
async def get_statistics(gerty) -> Optional[Statistics]:
row = await db.fetchone("SELECT * FROM gerty.statistics", ())
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/v1/lightning/statistics/latest")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.statistics
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Statistics(**response) if response else None
else:
return Statistics(**row) if row else None
async def get_difficulty_adjustment(gerty) -> Optional[Difficulty_adjustment]:
row = await db.fetchone("SELECT * FROM gerty.difficulty_adjustment", ())
logger.debug(int(time.time()))
logger.debug(row.time)
logger.debug(int(time.time()) - row.time)
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/v1/difficulty-adjustment")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.difficulty_adjustment
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Difficulty_adjustment(**response) if response else None
else:
return Difficulty_adjustment(**row) if row else None
async def get_tip_height() -> Optional[Tip_height]:
row = await db.fetchone("SELECT * FROM gerty.tip_height", ())
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/blocks/tip/height")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.tip_height
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Tip_height(**response) if response else None
else:
return Tip_height(**row) if row else None
async def get_mempool() -> Optional[Mempool]:
row = await db.fetchone("SELECT * FROM gerty.mempool", ())
if int(time.time()) - row.time > 20:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + "/api/mempool")
if response.status_code == 200:
await db.execute(
"""
UPDATE gerty.mempool
SET data = ?, time = ?
""",
(response.json(), int(time.time())),
)
return Mempool(**response) if response else None
else:
return Mempool(**row) if row else None
response = await client.get(gerty.mempool_endpoint + url)
await db.execute(
"UPDATE gerty.mempool SET data = ?, time = ? WHERE endpoint = ?",
(json.dumps(response.json()), int(time.time()), endPoint),
)
return response.json()
return json.loads(row.data)