From a626155c33193b88477b6616113135a21a7ebaaa Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Mon, 25 Oct 2021 11:54:58 +0100 Subject: [PATCH] satsdice still errors on withdraw --- lnbits/extensions/satsdice/crud.py | 15 ++-- lnbits/extensions/satsdice/lnurl.py | 29 +++----- .../satsdice/templates/satsdice/index.html | 10 +-- lnbits/extensions/satsdice/views.py | 71 ++++++++++--------- lnbits/extensions/satsdice/views_api.py | 11 ++- lnbits/extensions/withdraw/lnurl.py | 10 +-- 6 files changed, 68 insertions(+), 78 deletions(-) diff --git a/lnbits/extensions/satsdice/crud.py b/lnbits/extensions/satsdice/crud.py index f6833487..c0164bc5 100644 --- a/lnbits/extensions/satsdice/crud.py +++ b/lnbits/extensions/satsdice/crud.py @@ -1,21 +1,22 @@ from datetime import datetime from typing import List, Optional, Union + from lnbits.helpers import urlsafe_short_hash -from typing import List, Optional + from . import db from .models import ( - satsdiceWithdraw, - HashCheck, - satsdiceLink, - satsdicePayment, CreateSatsDiceLink, CreateSatsDicePayment, CreateSatsDiceWithdraw, + HashCheck, + satsdiceLink, + satsdicePayment, + satsdiceWithdraw, ) -from lnbits.helpers import urlsafe_short_hash async def create_satsdice_pay( + wallet_id: str, data: CreateSatsDiceLink, ) -> satsdiceLink: satsdice_id = urlsafe_short_hash() @@ -40,7 +41,7 @@ async def create_satsdice_pay( """, ( satsdice_id, - data.wallet_id, + wallet_id, data.title, data.base_url, data.min_bet, diff --git a/lnbits/extensions/satsdice/lnurl.py b/lnbits/extensions/satsdice/lnurl.py index ef09fbbd..b5b48370 100644 --- a/lnbits/extensions/satsdice/lnurl.py +++ b/lnbits/extensions/satsdice/lnurl.py @@ -1,33 +1,24 @@ -import shortuuid # type: ignore import hashlib -import math import json +import math from http import HTTPStatus -from datetime import datetime -from lnbits.core.services import pay_invoice, create_invoice -from http import HTTPStatus -from starlette.exceptions import HTTPException -from starlette.responses import HTMLResponse, JSONResponse # type: ignore -from lnbits.utils.exchange_rates import get_fiat_rate_satoshis -from fastapi import FastAPI, Request -from fastapi.params import Depends -from typing import Optional + +from fastapi import Request from fastapi.param_functions import Query +from starlette.exceptions import HTTPException +from starlette.responses import HTMLResponse # type: ignore + +from lnbits.core.services import create_invoice, pay_invoice + from . import satsdice_ext from .crud import ( + create_satsdice_payment, + get_satsdice_pay, get_satsdice_withdraw_by_hash, update_satsdice_withdraw, - get_satsdice_pay, - create_satsdice_payment, -) -from lnurl import ( - LnurlPayResponse, - LnurlPayActionResponse, - LnurlErrorResponse, ) from .models import CreateSatsDicePayment - ##############LNURLP STUFF diff --git a/lnbits/extensions/satsdice/templates/satsdice/index.html b/lnbits/extensions/satsdice/templates/satsdice/index.html index a5ec243d..f383f0df 100644 --- a/lnbits/extensions/satsdice/templates/satsdice/index.html +++ b/lnbits/extensions/satsdice/templates/satsdice/index.html @@ -342,7 +342,7 @@ LNbits.api .request( 'GET', - '/satsdice/api/v1/links?all_wallets', + '/satsdice/api/v1/links?all_wallets=true', this.g.user.wallets[0].inkey ) .then(response => { @@ -446,7 +446,7 @@ key === 'success_url') && (value === null || value === '') ) - + LNbits.api .request( 'PUT', @@ -516,9 +516,9 @@ if (this.g.user.wallets.length) { var getPayLinks = this.getPayLinks getPayLinks() - this.checker = setInterval(() => { - getPayLinks() - }, 20000) + // this.checker = setInterval(() => { + // getPayLinks() + // }, 20000) } } }) diff --git a/lnbits/extensions/satsdice/views.py b/lnbits/extensions/satsdice/views.py index 9e6ef259..bbe19fe0 100644 --- a/lnbits/extensions/satsdice/views.py +++ b/lnbits/extensions/satsdice/views.py @@ -1,48 +1,53 @@ +import random from datetime import datetime from http import HTTPStatus -from lnbits.decorators import check_user_exists, WalletTypeInfo, get_key_type -from . import satsdice_ext, satsdice_renderer -from .crud import ( - get_satsdice_pay, - update_satsdice_payment, - get_satsdice_payment, - create_satsdice_withdraw, - get_satsdice_withdraw, -) -from lnbits.core.crud import ( - get_payments, - get_standalone_payment, - delete_expired_invoices, - get_balance_checks, -) -from lnbits.core.views.api import api_payment -from lnbits.core.services import check_invoice_status + from fastapi import FastAPI, Request +from fastapi.param_functions import Query from fastapi.params import Depends from fastapi.templating import Jinja2Templates from starlette.exceptions import HTTPException from starlette.responses import HTMLResponse -from lnbits.core.models import User, Payment -from fastapi.params import Depends -from fastapi.param_functions import Query -import random -from .models import CreateSatsDiceWithdraw + +from lnbits.core.crud import ( + delete_expired_invoices, + get_balance_checks, + get_payments, + get_standalone_payment, +) +from lnbits.core.models import Payment, User +from lnbits.core.services import check_invoice_status +from lnbits.core.views.api import api_payment +from lnbits.decorators import WalletTypeInfo, check_user_exists, get_key_type + +from . import satsdice_ext, satsdice_renderer +from .crud import ( + create_satsdice_withdraw, + get_satsdice_pay, + get_satsdice_payment, + get_satsdice_withdraw, + update_satsdice_payment, +) +from .models import CreateSatsDiceWithdraw, satsdiceLink templates = Jinja2Templates(directory="templates") -@satsdice_ext.get("/") +@satsdice_ext.get("/", response_class=HTMLResponse) async def index(request: Request, user: User = Depends(check_user_exists)): return satsdice_renderer().TemplateResponse( "satsdice/index.html", {"request": request, "user": user.dict()} ) -@satsdice_ext.get("/{link_id}") +@satsdice_ext.get("/{link_id}", response_class=HTMLResponse) async def display(request: Request, link_id: str = Query(None)): - link = await get_satsdice_pay(link_id) or abort( - HTTPStatus.NOT_FOUND, "satsdice link does not exist." - ) + link = await get_satsdice_pay(link_id) + if not link: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist." + ) + return satsdice_renderer().TemplateResponse( "satsdice/display.html", { @@ -55,13 +60,15 @@ async def display(request: Request, link_id: str = Query(None)): ) -@satsdice_ext.get("/win/{link_id}/{payment_hash}", name="satsdice.displaywin") +@satsdice_ext.get("/win/{link_id}/{payment_hash}", name="satsdice.displaywin", response_class=HTMLResponse) async def displaywin( request: Request, link_id: str = Query(None), payment_hash: str = Query(None) ): - satsdicelink = await get_satsdice_pay(link_id) or abort( - HTTPStatus.NOT_FOUND, "satsdice link does not exist." - ) + satsdicelink = await get_satsdice_pay(link_id) + if not satsdiceLink: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist." + ) withdrawLink = await get_satsdice_withdraw(payment_hash) if withdrawLink: @@ -118,7 +125,7 @@ async def displaywin( ) -@satsdice_ext.get("/img/{link_id}") +@satsdice_ext.get("/img/{link_id}", response_class=HTMLResponse) async def img(link_id): link = await get_satsdice_pay(link_id) or abort( HTTPStatus.NOT_FOUND, "satsdice link does not exist." diff --git a/lnbits/extensions/satsdice/views_api.py b/lnbits/extensions/satsdice/views_api.py index 91ce62df..b935ba88 100644 --- a/lnbits/extensions/satsdice/views_api.py +++ b/lnbits/extensions/satsdice/views_api.py @@ -31,7 +31,7 @@ from .models import CreateSatsDiceLink, CreateSatsDiceWithdraws async def api_links( request: Request, wallet: WalletTypeInfo = Depends(get_key_type), - all_wallets: str = Query(None), + all_wallets: bool = Query(False), ): wallet_ids = [wallet.wallet.id] @@ -81,7 +81,6 @@ async def api_link_create_or_update( raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request") if link_id: link = await get_satsdice_pay(link_id) - if not link: raise HTTPException( status_code=HTTPStatus.NOT_FOUND, detail="Satsdice does not exist" @@ -92,11 +91,11 @@ async def api_link_create_or_update( status_code=HTTPStatus.FORBIDDEN, detail="Come on, seriously, this isn't your satsdice!", ) - data.link_id = link_id - link = await update_satsdice_pay(data) - else: + data.wallet_id = wallet.wallet.id - link = await create_satsdice_pay(data) + link = await update_satsdice_pay(link_id, **data.dict()) + else: + link = await create_satsdice_pay(wallet_id=wallet.wallet.id, data=data) return {**link.dict(), **{"lnurl": link.lnurl}} diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index af3ecff4..f4728819 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -59,24 +59,16 @@ async def api_lnurl_callback( raise HTTPException( status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found." ) - # return ( - # {"status": "ERROR", "reason": "LNURL-withdraw not found."}, - # HTTPStatus.OK, - # ) + if link.is_spent: raise HTTPException( # WHAT STATUS_CODE TO USE?? detail="Withdraw is spent." ) - # return ( - # {"status": "ERROR", "reason": "Withdraw is spent."}, - # HTTPStatus.OK, - # ) if link.k1 != k1: raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.") - # return {"status": "ERROR", "reason": "Bad request."}, HTTPStatus.OK if now < link.open_time: return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}