diff --git a/lnbits/extensions/gerty/crud.py b/lnbits/extensions/gerty/crud.py
index 19269306..81b71003 100644
--- a/lnbits/extensions/gerty/crud.py
+++ b/lnbits/extensions/gerty/crud.py
@@ -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
\ No newline at end of file
+ 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)
\ No newline at end of file
diff --git a/lnbits/extensions/gerty/helpers.py b/lnbits/extensions/gerty/helpers.py
index 097400ee..e1ed04d8 100644
--- a/lnbits/extensions/gerty/helpers.py
+++ b/lnbits/extensions/gerty/helpers.py
@@ -4,15 +4,7 @@ from datetime import datetime, timedelta
import httpx
from loguru import logger
-from .crud import (
- get_fees_recommended,
- get_hashrate_1w,
- get_hashrate_1m,
- get_statistics,
- get_difficulty_adjustment,
- get_tip_height,
- get_mempool
-)
+from .crud import get_mempool_info
from .number_prefixer import *
@@ -80,7 +72,7 @@ async def get_mining_dashboard(gerty):
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
# current hashrate
- r = await get_hashrate_1w(gerty)
+ r = await get_mempool_info("get_hashrate_1w", gerty)
data = r.json()
hashrateNow = data["currentHashrate"]
hashrateOneWeekAgo = data["hashrates"][6]["avgHashrate"]
@@ -102,7 +94,7 @@ async def get_mining_dashboard(gerty):
)
areas.append(text)
- r = await get_difficulty_adjustment(gerty)
+ r = await get_mempool_info("difficulty_adjustment", gerty)
# timeAvg
text = []
@@ -132,7 +124,7 @@ async def get_mining_dashboard(gerty):
)
areas.append(text)
- r = await get_hashrate_1m(gerty)
+ r = await get_mempool_info("hashrate_1m", gerty)
data = r.json()
stat = {}
stat["current"] = data["currentDifficulty"]
@@ -144,7 +136,7 @@ async def get_mining_dashboard(gerty):
async def get_lightning_stats(gerty):
- data = await get_statistics(gerty)
+ data = await get_mempool_info("statistics", gerty)
areas = []
text = []
@@ -271,14 +263,14 @@ async def api_get_mining_stat(stat_slug: str, gerty):
stat = ""
if stat_slug == "mining_current_hash_rate":
async with httpx.AsyncClient() as client:
- r = await get_hashrate_1m(gerty)
+ r = await get_mempool_info("hashrate_1m", gerty)
data = r.json()
stat = {}
stat['current'] = data['currentHashrate']
stat['1w'] = data['hashrates'][len(data['hashrates']) - 7]['avgHashrate']
elif stat_slug == "mining_current_difficulty":
async with httpx.AsyncClient() as client:
- r = await get_hashrate_1m(gerty)
+ r = await get_mempool_info("hashrate_1m", gerty)
data = r.json()
stat = {}
stat['current'] = data['currentDifficulty']
@@ -328,7 +320,7 @@ async def get_screen_data(screen_num: int, screens_list: dict, gerty):
elif screen_slug == "onchain_block_height":
logger.debug("iam block height")
text = []
- text.append(get_text_item_dict(text=format_number(await get_tip_height(gerty)), font_size=80, gerty_type=gerty.type))
+ text.append(get_text_item_dict(text=format_number(await get_mempool_info("tip_height", gerty)), font_size=80, gerty_type=gerty.type))
areas.append(text)
elif screen_slug == "onchain_difficulty_retarget_date":
areas.append(await get_onchain_stat(screen_slug, gerty))
@@ -383,7 +375,7 @@ async def get_dashboard(gerty):
# Mempool fees
text = []
- text.append(get_text_item_dict(text=format_number(await get_tip_height(gerty)), font_size=40,gerty_type=gerty.type))
+ text.append(get_text_item_dict(text=format_number(await get_mempool_info("tip_height", gerty)), font_size=40,gerty_type=gerty.type))
text.append(get_text_item_dict(text="Current block height", font_size=15,gerty_type=gerty.type))
areas.append(text)
@@ -467,7 +459,7 @@ async def get_onchain_stat(stat_slug: str, gerty):
):
async with httpx.AsyncClient() as client:
- r = await get_difficulty_adjustment(gerty)
+ r = await get_mempool_info("difficulty_adjustment", gerty)
if stat_slug == "onchain_difficulty_epoch_progress":
stat = round(r.json()['progressPercent'])
text.append(get_text_item_dict(text="Progress through current difficulty epoch", font_size=15,gerty_type=gerty.type))
@@ -491,7 +483,7 @@ async def get_onchain_dashboard(gerty):
areas = []
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
- r = await get_difficulty_adjustment(gerty)
+ r = await get_mempool_info("difficulty_adjustment", gerty)
text = []
stat = round(r.json()["progressPercent"])
text.append(get_text_item_dict(text="Progress through epoch", font_size=12,gerty_type=gerty.type))
@@ -523,7 +515,7 @@ async def get_onchain_dashboard(gerty):
async def get_time_remaining_next_difficulty_adjustment(gerty):
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
- r = await get_difficulty_adjustment(gerty)
+ r = await get_mempool_info("difficulty_adjustment", gerty)
stat = r.json()["remainingTime"]
time = get_time_remaining(stat / 1000, 3)
return time
@@ -534,7 +526,7 @@ async def get_mempool_stat(stat_slug: str, gerty):
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
if stat_slug == "mempool_tx_count":
- r = get_mempool(gerty)
+ r = get_mempool_info("mempool", gerty)
if stat_slug == "mempool_tx_count":
stat = round(r.json()["count"])
text.append(get_text_item_dict(text="Transactions in the mempool", font_size=15,gerty_type=gerty.type))
@@ -543,7 +535,7 @@ async def get_mempool_stat(stat_slug: str, gerty):
)
elif stat_slug == "mempool_recommended_fees":
y_offset = 60
- fees = await get_fees_recommended()
+ fees = await get_mempool_info("fees_recommended", gerty)
pos_y = 80 + y_offset
text.append(get_text_item_dict("mempool.space", 40, 160, pos_y, gerty.type))
pos_y = 180 + y_offset
diff --git a/lnbits/extensions/gerty/migrations.py b/lnbits/extensions/gerty/migrations.py
index 83db0930..b2a9d1e1 100644
--- a/lnbits/extensions/gerty/migrations.py
+++ b/lnbits/extensions/gerty/migrations.py
@@ -6,9 +6,9 @@ async def m001_initial(db):
"""
CREATE TABLE gerty.gertys (
id TEXT PRIMARY KEY,
+ wallet TEXT NOT NULL,
refresh_time INT,
name TEXT NOT NULL,
- wallet TEXT NOT NULL,
lnbits_wallets TEXT,
mempool_endpoint TEXT,
exchange TEXT,
@@ -37,57 +37,10 @@ async def m004_initial(db):
"""
Initial Gertys table.
"""
- await db.execute(
- """
- CREATE TABLE gerty.fees_recommended (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
- await db.execute(
- """
- CREATE TABLE gerty.hashrate_1w (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
- await db.execute(
- """
- CREATE TABLE gerty.hashrate_1m (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
- await db.execute(
- """
- CREATE TABLE gerty.statistics (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
- await db.execute(
- """
- CREATE TABLE gerty.difficulty_adjustment (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
- await db.execute(
- """
- CREATE TABLE gerty.tip_height (
- data TEXT NOT NULL,
- time TIMESTAMP
- );
- """
- )
await db.execute(
"""
CREATE TABLE gerty.mempool (
+ endpoint TEXT NOT NULL,
data TEXT NOT NULL,
time TIMESTAMP
);
diff --git a/lnbits/extensions/gerty/models.py b/lnbits/extensions/gerty/models.py
index 77c68110..fddf886c 100644
--- a/lnbits/extensions/gerty/models.py
+++ b/lnbits/extensions/gerty/models.py
@@ -27,30 +27,16 @@ class Gerty(BaseModel):
#########MEMPOOL MODELS###########
-class Fees_recommended(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
-
-class Hashrate_1w(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
-
-class Hashrate_1m(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
-
-class Statistics(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
-
-class Difficulty_adjustment(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
-
-class Tip_height(BaseModel):
- data: str = Query(None)
- time: int = Query(None)
+class MempoolEndpoint(BaseModel):
+ fees_recommended: str = "/api/v1/fees/recommended"
+ hashrate_1w: str = "/api/v1/mining/hashrate/1w"
+ hashrate_1m: str = "/api/v1/mining/hashrate/1m"
+ statistics: str = "/api/v1/lightning/statistics/latest"
+ difficulty_adjustment: str = "/api/v1/difficulty-adjustment"
+ tip_height: str = "/api/blocks/tip/height"
+ mempool: str = "/api/mempool"
class Mempool(BaseModel):
+ endpoint: str = Query(None)
data: str = Query(None)
time: int = Query(None)
\ No newline at end of file
diff --git a/lnbits/extensions/gerty/templates/gerty/index.html b/lnbits/extensions/gerty/templates/gerty/index.html
index db3dd7e6..dc246ceb 100644
--- a/lnbits/extensions/gerty/templates/gerty/index.html
+++ b/lnbits/extensions/gerty/templates/gerty/index.html
@@ -135,14 +135,6 @@
:options="['Gerty', 'Mini Gerty']"
label="Gerty Type *"
>
-
Update Gerty
@@ -661,7 +653,6 @@
this.formDialog.data.id = gerty.id
this.formDialog.data.name = gerty.name
this.formDialog.data.type = gerty.type
- this.formDialog.data.wallet = gerty.wallet
this.formDialog.data.utc_offset = gerty.utc_offset
this.formDialog.data.lnbits_wallets = JSON.parse(gerty.lnbits_wallets)
this.formDialog.data.exchange = gerty.exchange,
@@ -677,7 +668,6 @@
this.formDialog.data
)
} else {
- {#console.log('sendFormDataGerty', this.formDialog.data)#}
this.createGerty(
this.g.user.wallets[0].adminkey,
this.formDialog.data
@@ -687,7 +677,6 @@
createGerty: function () {
var data = {
name: this.formDialog.data.name,
- wallet: this.formDialog.data.wallet,
utc_offset: this.formDialog.data.utc_offset,
type: this.formDialog.data.type,
lnbits_wallets: JSON.stringify(this.formDialog.data.lnbits_wallets),
@@ -696,15 +685,13 @@
refresh_time: this.formDialog.data.refresh_time,
display_preferences: JSON.stringify(this.formDialog.data.display_preferences)
}
- console.log('createGerty', data)
var self = this
LNbits.api
.request(
'POST',
'/gerty/api/v1/gerty',
- _.findWhere(this.g.user.wallets, {id: this.formDialog.data.wallet})
- .inkey,
+ this.g.user.wallets[0].inkey,
data
)
.then(function (response) {
diff --git a/lnbits/extensions/gerty/views_api.py b/lnbits/extensions/gerty/views_api.py
index 56cf12fd..531acc30 100644
--- a/lnbits/extensions/gerty/views_api.py
+++ b/lnbits/extensions/gerty/views_api.py
@@ -28,17 +28,11 @@ from .crud import (
get_gerty,
get_gertys,
update_gerty,
- get_fees_recommended,
- get_hashrate_1w,
- get_hashrate_1m,
- get_statistics,
- get_difficulty_adjustment,
- get_tip_height,
- get_mempool
+ get_mempool_info
)
from .helpers import *
-from .models import Gerty
+from .models import Gerty, MempoolEndpoint
@gerty_ext.get("/api/v1/gerty", status_code=HTTPStatus.OK)
async def api_gertys(
@@ -164,37 +158,42 @@ async def api_gerty_json(gerty_id: str, p: int = None): # page number
@gerty_ext.get("/api/v1/gerty/fees-recommended/{gerty_id}")
async def api_gerty_get_fees_recommended(gerty_id):
- logger.debug("gerty_id")
+
gerty = await get_gerty(gerty_id)
- logger.debug(gerty)
- return get_fees_recommended(gerty)
+ return await get_mempool_info("fees_recommended", gerty)
@gerty_ext.get("/api/v1/gerty/hashrate-1w/{gerty_id}")
async def api_gerty_get_hashrate_1w(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_hashrate_1w(gerty)
+ return await get_mempool_info("hashrate_1w", gerty)
@gerty_ext.get("/api/v1/gerty/hashrate-1m/{gerty_id}")
async def api_gerty_get_hashrate_1m(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_hashrate_1m(gerty)
+ return await get_mempool_info("hashrate_1m", gerty)
@gerty_ext.get("/api/v1/gerty/statistics/{gerty_id}")
async def api_gerty_get_statistics(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_statistics(gerty)
+ return await get_mempool_info("statistics", gerty)
@gerty_ext.get("/api/v1/gerty/difficulty-adjustment/{gerty_id}")
async def api_gerty_get_difficulty_adjustment(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_difficulty_adjustment(gerty)
+ return await get_mempool_info("difficulty_adjustment", gerty)
@gerty_ext.get("/api/v1/gerty/tip-height/{gerty_id}")
async def api_gerty_get_tip_height(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_tip_height(gerty)
+ return await get_mempool_info("tip_height", gerty)
@gerty_ext.get("/api/v1/gerty/mempool/{gerty_id}")
async def api_gerty_get_mempool(gerty_id):
gerty = await get_gerty(gerty_id)
- return get_mempool(gerty)
\ No newline at end of file
+ return await get_mempool_info("mempool", gerty)
+
+@gerty_ext.get("/api/v1/gerty/wibble/{gerty_id}")
+async def api_gerty_get_wibble(gerty_id):
+ endPoint = "fees_recommended"
+ gerty = await get_gerty(gerty_id)
+ return await get_mempool_info("fees_recommended", gerty)
\ No newline at end of file