Skip to content

Commit d95494a

Browse files
authored
refactor: use new get_pr_from_lnurl (#7)
1 parent 3449a28 commit d95494a

File tree

5 files changed

+30
-38
lines changed

5 files changed

+30
-38
lines changed

helpers.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package-mode = false
77

88
[tool.poetry.dependencies]
99
python = "~3.12 | ~3.11 | ~3.10"
10-
lnbits = {version = "1.3.*", allow-prereleases = true}
10+
lnbits = {version = "*", allow-prereleases = true}
1111

1212
[tool.poetry.group.dev.dependencies]
1313
black = "^24.3.0"

tasks.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import random
33

44
from lnbits.core.models import Payment
5-
from lnbits.core.services import pay_invoice, websocket_updater
5+
from lnbits.core.services import get_pr_from_lnurl, pay_invoice, websocket_updater
66
from lnbits.tasks import register_invoice_listener
7+
from loguru import logger
78

89
from .crud import (
910
get_coinflip,
1011
get_coinflip_settings_from_id,
1112
update_coinflip,
1213
)
13-
from .helpers import get_pr
1414

1515

1616
async def wait_for_paid_invoices():
@@ -53,8 +53,10 @@ async def on_invoice_paid(payment: Payment) -> None:
5353
haircut_amount = coinflip.buy_in * (coinflip_settings.haircut / 100)
5454
# Calculate the refund amount
5555
max_sat = int(coinflip.buy_in - haircut_amount)
56-
pr = await get_pr(ln_address, max_sat)
57-
if not pr:
56+
try:
57+
pr = await get_pr_from_lnurl(ln_address, max_sat)
58+
except Exception as exc:
59+
logger.error(f"Error getting payment request for refund: {exc!s}")
5860
return
5961
await pay_invoice(
6062
wallet_id=coinflip_settings.wallet_id,
@@ -80,8 +82,10 @@ async def on_invoice_paid(payment: Payment) -> None:
8082
haircut_amount = total_amount * (coinflip_settings.haircut / 100)
8183
# Calculate the winnings minus haircut
8284
max_sat = int(total_amount - haircut_amount)
83-
pr = await get_pr(winner, max_sat)
84-
if not pr:
85+
try:
86+
pr = await get_pr_from_lnurl(winner, max_sat)
87+
except Exception as exc:
88+
logger.error(f"Error getting payment request for winner: {exc!s}")
8589
return
8690
if winner == ln_address:
8791
await websocket_updater("coinflip" + payment.payment_hash, f"won,{winner}")
@@ -108,8 +112,10 @@ async def on_invoice_paid(payment: Payment) -> None:
108112
async def pay_tribute(haircut_amount: int, wallet_id: str) -> None:
109113
try:
110114
tribute = int(2 * (haircut_amount / 100))
111-
pr = await get_pr("lnbits@nostr.com", tribute)
112-
if not pr:
115+
try:
116+
pr = await get_pr_from_lnurl("lnbits@nostr.com", tribute)
117+
except Exception as exc:
118+
logger.error(f"Error getting payment request for tribute: {exc!s}")
113119
return
114120
await pay_invoice(
115121
wallet_id=wallet_id,

views_api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from lnbits.core.models import WalletTypeInfo
66
from lnbits.core.services import create_invoice
77
from lnbits.decorators import require_admin_key, require_invoice_key
8+
from lnurl import LnurlPayResponse
9+
from lnurl import handle as lnurl_handle
810

911
from .crud import (
1012
create_coinflip,
@@ -14,7 +16,6 @@
1416
get_coinflip_settings_from_id,
1517
update_coinflip_settings,
1618
)
17-
from .helpers import get_pr
1819
from .models import (
1920
CoinflipSettings,
2021
CreateCoinflip,
@@ -114,10 +115,16 @@ async def api_join_coinflip(data: JoinCoinflipGame):
114115
raise HTTPException(
115116
status_code=HTTPStatus.BAD_REQUEST, detail="This game is disabled"
116117
)
117-
pay_req = await get_pr(data.ln_address, coinflip_game.buy_in)
118-
if not pay_req:
118+
try:
119+
res = await lnurl_handle(data.ln_address)
120+
except Exception as exc:
119121
raise HTTPException(
120-
status_code=HTTPStatus.BAD_REQUEST, detail="lnaddress check failed"
122+
status_code=HTTPStatus.BAD_REQUEST, detail=f"lnaddress error: {exc!s}"
123+
) from exc
124+
if not isinstance(res, LnurlPayResponse):
125+
raise HTTPException(
126+
status_code=HTTPStatus.BAD_REQUEST,
127+
detail="lnaddress return wrong response type",
121128
)
122129
payment = await create_invoice(
123130
wallet_id=coinflip_settings.wallet_id,

0 commit comments

Comments
 (0)