Skip to content

Commit

Permalink
feat: update to lnbits 1.0.0 (#66)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Vlad Stan <[email protected]>
Co-authored-by: Tiago Vasconcelos <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 3e00665 commit c7623e4
Show file tree
Hide file tree
Showing 15 changed files with 1,372 additions and 1,278 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Pay Links",
"short_description": "Make reusable LNURL pay links",
"tile": "/lnurlp/static/image/lnurl-pay.png",
"min_lnbits_version": "0.12.4",
"min_lnbits_version": "1.0.0",
"contributors": [
{
"name": "arcbtc",
Expand Down
141 changes: 51 additions & 90 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Optional, Union

from lnbits.db import Database
from lnbits.helpers import insert_query, update_query, urlsafe_short_hash
from lnbits.helpers import urlsafe_short_hash

from .models import CreatePayLinkData, LnurlpSettings, PayLink
from .nostr.key import PrivateKey
Expand All @@ -10,22 +10,19 @@


async def get_or_create_lnurlp_settings() -> LnurlpSettings:
row = await db.fetchone("SELECT * FROM lnurlp.settings LIMIT 1")
if row:
return LnurlpSettings(**row)
settings = await db.fetchone(
"SELECT * FROM lnurlp.settings LIMIT 1", model=LnurlpSettings
)
if settings:
return settings
else:
settings = LnurlpSettings(nostr_private_key=PrivateKey().hex())
await db.execute(
insert_query("lnurlp.settings", settings), (*settings.dict().values(),)
)
await db.insert("lnurlp.settings", settings)
return settings


async def update_lnurlp_settings(settings: LnurlpSettings) -> LnurlpSettings:
await db.execute(
update_query("lnurlp.settings", settings, where=""),
(*settings.dict().values(),),
)
await db.update("lnurlp.settings", settings, "")
return settings


Expand All @@ -34,110 +31,74 @@ async def delete_lnurlp_settings() -> None:


async def get_pay_link_by_username(username: str) -> Optional[PayLink]:
row = await db.fetchone(
"SELECT * FROM lnurlp.pay_links WHERE username = ?", (username,)
return await db.fetchone(
"SELECT * FROM lnurlp.pay_links WHERE username = :username",
{"username": username},
PayLink,
)
return PayLink.from_row(row) if row else None


async def create_pay_link(data: CreatePayLinkData) -> PayLink:

link_id = urlsafe_short_hash()[:6]

result = await db.execute(
"""
INSERT INTO lnurlp.pay_links (
id,
wallet,
description,
min,
max,
served_meta,
served_pr,
webhook_url,
webhook_headers,
webhook_body,
success_text,
success_url,
comment_chars,
currency,
fiat_base_multiplier,
username,
zaps
)
VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
link_id,
data.wallet,
data.description,
data.min,
data.max,
data.webhook_url,
data.webhook_headers,
data.webhook_body,
data.success_text,
data.success_url,
data.comment_chars,
data.currency,
data.fiat_base_multiplier,
data.username,
data.zaps,
),
assert data.wallet, "Wallet is required"

link = PayLink(
id=link_id,
wallet=data.wallet,
description=data.description,
min=data.min,
max=data.max,
served_meta=0,
served_pr=0,
username=data.username,
zaps=data.zaps,
domain=None,
webhook_url=data.webhook_url,
webhook_headers=data.webhook_headers,
webhook_body=data.webhook_body,
success_text=data.success_text,
success_url=data.success_url,
currency=data.currency,
comment_chars=data.comment_chars,
fiat_base_multiplier=data.fiat_base_multiplier,
)
assert result

link = await get_pay_link(link_id)
assert link, "Newly created link couldn't be retrieved"
await db.insert("lnurlp.pay_links", link)
return link


async def get_address_data(username: str) -> Optional[PayLink]:
row = await db.fetchone(
"SELECT * FROM lnurlp.pay_links WHERE username = ?", (username,)
return await db.fetchone(
"SELECT * FROM lnurlp.pay_links WHERE username = :username",
{"username": username},
PayLink,
)
return PayLink.from_row(row) if row else None


async def get_pay_link(link_id: str) -> Optional[PayLink]:
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None
return await db.fetchone(
"SELECT * FROM lnurlp.pay_links WHERE id = :id",
{"id": link_id},
PayLink,
)


async def get_pay_links(wallet_ids: Union[str, List[str]]) -> List[PayLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"""
SELECT * FROM lnurlp.pay_links WHERE wallet IN ({q})
ORDER BY Id
""",
(*wallet_ids,),
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM lnurlp.pay_links WHERE wallet IN ({q}) ORDER BY Id",
model=PayLink,
)
return [PayLink.from_row(row) for row in rows]


async def update_pay_link(link_id: str, **kwargs) -> Optional[PayLink]:

q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
)
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None


async def increment_pay_link(link_id: str, **kwargs) -> Optional[PayLink]:
q = ", ".join([f"{field[0]} = {field[0]} + ?" for field in kwargs.items()])
await db.execute(
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
)
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None
async def update_pay_link(link: PayLink) -> PayLink:
await db.update("lnurlp.pay_links", link)
return link


async def delete_pay_link(link_id: str) -> None:
await db.execute("DELETE FROM lnurlp.pay_links WHERE id = ?", (link_id,))
await db.execute("DELETE FROM lnurlp.pay_links WHERE id = :id", {"id": link_id})
7 changes: 7 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,10 @@ async def m009_add_settings(db):
);
"""
)


async def m010_add_pay_link_domain(db):
"""
Add domain to pay links
"""
await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN domain TEXT;")
12 changes: 1 addition & 11 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json
from sqlite3 import Row
from typing import Optional

from fastapi import Request
from fastapi.param_functions import Query
from fastapi import Query, Request
from lnurl import encode as lnurl_encode
from lnurl.types import LnurlPayMetadata
from pydantic import BaseModel
Expand Down Expand Up @@ -61,14 +59,6 @@ class PayLink(BaseModel):
max: float
fiat_base_multiplier: int

@classmethod
def from_row(cls, row: Row) -> "PayLink":
data = dict(row)
if data["currency"] and data["fiat_base_multiplier"]:
data["min"] /= data["fiat_base_multiplier"]
data["max"] /= data["fiat_base_multiplier"]
return cls(**data)

def lnurl(self, req: Request) -> str:
url = req.url_for("lnurlp.api_lnurl_response", link_id=self.id)
url_str = str(url)
Expand Down
Loading

0 comments on commit c7623e4

Please sign in to comment.