Working on withdraw

This commit is contained in:
benarc 2024-01-22 13:34:48 +00:00
parent d90babebc8
commit 7c0e227fd1
7 changed files with 55 additions and 61 deletions

Binary file not shown.

Binary file not shown.

View file

@ -7,7 +7,8 @@ from fastapi import Depends, Query, Request
from . import myextension_ext from . import myextension_ext
from .crud import get_myextension from .crud import get_myextension
from lnbits.core.services import create_invoice from lnbits.core.services import create_invoice
from loguru import logger
from uuid import UUID
################################################# #################################################
########### A very simple LNURLpay ############## ########### A very simple LNURLpay ##############
@ -29,8 +30,8 @@ async def api_lnurl_pay(
return {"status": "ERROR", "reason": "No myextension found"} return {"status": "ERROR", "reason": "No myextension found"}
return { return {
"callback": str(request.url_for("myextension.api_lnurl_pay_callback", myextension_id=myextension_id)), "callback": str(request.url_for("myextension.api_lnurl_pay_callback", myextension_id=myextension_id)),
"maxSendable": myextension.lnurlpayamount, "maxSendable": myextension.lnurlpayamount * 1000,
"minSendable": myextension.lnurlpayamount, "minSendable": myextension.lnurlpayamount * 1000,
"metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", "metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]",
"tag": "payRequest" "tag": "payRequest"
} }
@ -46,21 +47,29 @@ async def api_lnurl_pay_cb(
amount: int = Query(...), amount: int = Query(...),
): ):
myextension = await get_myextension(myextension_id) myextension = await get_myextension(myextension_id)
logger.debug(myextension)
if not myextension: if not myextension:
return {"status": "ERROR", "reason": "No myextension found"} return {"status": "ERROR", "reason": "No myextension found"}
payment_request = await create_invoice( payment_hash, payment_request = await create_invoice(
wallet_id=myextension.wallet, wallet_id=myextension.wallet,
amount=int(amount / 1000), amount=int(amount / 1000),
memo=myextension.name, memo=myextension.name,
unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), unhashed_description=f"[[\"text/plain\", \"{myextension.name}\"]]".encode(),
extra= { extra= {
"tag": "myextension", "tag": "MyExtension",
"link": myextension_id, "myextensionId": myextension_id,
"extra": request.query_params.get("amount"), "extra": request.query_params.get("amount"),
}, },
) )
return { "pr": payment_request, "routes": []} return {
"pr": payment_request,
"routes": [],
"successAction": {
"tag": "message",
"message": f"Paid {myextension.name}"
}
}
################################################# #################################################
######## A very simple LNURLwithdraw ############ ######## A very simple LNURLwithdraw ############
@ -81,13 +90,20 @@ async def api_lnurl_pay(
myextension = await get_myextension(myextension_id) myextension = await get_myextension(myextension_id)
if not myextension: if not myextension:
return {"status": "ERROR", "reason": "No myextension found"} return {"status": "ERROR", "reason": "No myextension found"}
k1 = UUID(myextension_id + str(myextension.ticker), version=4)
data_to_update = {
"ticker": myextension.ticker + 1
}
await update_myextension(myextension_id=myextension_id, **data_to_update)
return { return {
"callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)), "callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)),
"maxSendable": myextension.lnurlwithdrawamount, "maxSendable": myextension.lnurlwithdrawamount,
"minSendable": myextension.lnurlwithdrawamount, "minSendable": myextension.lnurlwithdrawamount,
"k1": "", "k1": k1,
"defaultDescription": myextension.name, "defaultDescription": myextension.name,
"metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", "metadata":f"[[\"text/plain\", \"{myextension.name}\"]]",
"tag": "withdrawRequest" "tag": "withdrawRequest"
} }
@ -96,24 +112,31 @@ async def api_lnurl_pay(
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
name="myextension.api_lnurl_withdraw_callback", name="myextension.api_lnurl_withdraw_callback",
) )
async def api_lnurl_pay_cb( async def api_lnurl_withdraw_cb(
request: Request, request: Request,
myextension_id: str, myextension_id: str,
amount: int = Query(...), pr: Optional[str] = None,
k1: Optional[str] = None,
): ):
assert k1, "k1 is required"
assert pr, "pr is required"
myextension = await get_myextension(myextension_id) myextension = await get_myextension(myextension_id)
if not myextension: if not myextension:
return {"status": "ERROR", "reason": "No myextension found"} return {"status": "ERROR", "reason": "No myextension found"}
payment_request = await create_invoice( k1Check = UUID(myextension_id + str(myextension.ticker - 1), version=4)
wallet_id=myextension.wallet, if k1Check != k1:
amount=int(amount / 1000), return {"status": "ERROR", "reason": "Already spent"}
memo=myextension.name, try:
unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), await pay_invoice(
extra= { wallet_id=tpos.wallet,
"tag": "myextension", payment_request=pr,
"link": myextension_id, max_sat=myextension.lnurlwithdrawamount * 1000,
"extra": request.query_params.get("amount"), extra={"tag": "MyExtension", "myextensionId": myextension_id,}
}, )
) except Exception as e:
return { "pr": payment_request, "routes": []} raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=f"withdraw not working. {str(e)}"
)
return {"status": "OK"}

View file

@ -11,12 +11,15 @@ async def m001_initial(db):
wallet TEXT NOT NULL, wallet TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
total INTEGER DEFAULT 0, total INTEGER DEFAULT 0,
lnurlpayamount INTEGER DEFAULT 0 lnurlpayamount INTEGER DEFAULT 0,
lnurlwithdrawamount INTEGER DEFAULT 0,
lnurlwithdraw TEXT,
lnurlpay TEXT
); );
""" """
) )
# Here we are adding an extra field to the database # Here we add another field to the database
async def m002_addtip_wallet(db): async def m002_addtip_wallet(db):
""" """
@ -24,30 +27,6 @@ async def m002_addtip_wallet(db):
""" """
await db.execute( await db.execute(
""" """
ALTER TABLE myextension.maintable ADD lnurlwithdrawamount INTEGER DEFAULT 0; ALTER TABLE myextension.maintable ADD ticker INTEGER DEFAULT 1;
"""
)
# Here we add another field to the database, always add never edit!
async def m004_addtip_wallet(db):
"""
Add total to templates table
"""
await db.execute(
"""
ALTER TABLE myextension.maintable ADD lnurlwithdraw TEXT;
"""
)
# Here we add another field to the database
async def m005_addtip_wallet(db):
"""
Add total to templates table
"""
await db.execute(
"""
ALTER TABLE myextension.maintable ADD lnurlpay TEXT;
""" """
) )

File diff suppressed because one or more lines are too long

View file

@ -38,11 +38,10 @@ async def on_invoice_paid(payment: Payment) -> None:
myextension = await get_myextension(myextension_id) myextension = await get_myextension(myextension_id)
# update something in the db # update something in the db
data_to_update = { data_to_update = {
"total": myextension.total + payment.amount "total": myextension.total + payment.amount
} }
await update_myextension(myextension_id=myextension_id, **data_to_update) await update_myextension(myextension_id=myextension_id, **data_to_update)
# here we could send some data to a websocket on wss://<your-lnbits>/api/v1/ws/<myextension_id> # here we could send some data to a websocket on wss://<your-lnbits>/api/v1/ws/<myextension_id>

View file

@ -419,6 +419,7 @@
openUrlDialog(id) { openUrlDialog(id) {
this.urlDialog.data = _.findWhere(this.temps, {id}) this.urlDialog.data = _.findWhere(this.temps, {id})
this.qrValue = this.urlDialog.data.lnurlpay this.qrValue = this.urlDialog.data.lnurlpay
console.log(this.urlDialog.data.id)
this.connectWebocket(this.urlDialog.data.id) this.connectWebocket(this.urlDialog.data.id)
this.urlDialog.show = true this.urlDialog.show = true
}, },