satsdice still errors on withdraw

This commit is contained in:
Tiago vasconcelos
2021-10-25 11:54:58 +01:00
parent d1b628375a
commit a626155c33
6 changed files with 68 additions and 78 deletions

View File

@@ -1,21 +1,22 @@
from datetime import datetime from datetime import datetime
from typing import List, Optional, Union from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash from lnbits.helpers import urlsafe_short_hash
from typing import List, Optional
from . import db from . import db
from .models import ( from .models import (
satsdiceWithdraw,
HashCheck,
satsdiceLink,
satsdicePayment,
CreateSatsDiceLink, CreateSatsDiceLink,
CreateSatsDicePayment, CreateSatsDicePayment,
CreateSatsDiceWithdraw, CreateSatsDiceWithdraw,
HashCheck,
satsdiceLink,
satsdicePayment,
satsdiceWithdraw,
) )
from lnbits.helpers import urlsafe_short_hash
async def create_satsdice_pay( async def create_satsdice_pay(
wallet_id: str,
data: CreateSatsDiceLink, data: CreateSatsDiceLink,
) -> satsdiceLink: ) -> satsdiceLink:
satsdice_id = urlsafe_short_hash() satsdice_id = urlsafe_short_hash()
@@ -40,7 +41,7 @@ async def create_satsdice_pay(
""", """,
( (
satsdice_id, satsdice_id,
data.wallet_id, wallet_id,
data.title, data.title,
data.base_url, data.base_url,
data.min_bet, data.min_bet,

View File

@@ -1,33 +1,24 @@
import shortuuid # type: ignore
import hashlib import hashlib
import math
import json import json
import math
from http import HTTPStatus from http import HTTPStatus
from datetime import datetime
from lnbits.core.services import pay_invoice, create_invoice from fastapi import Request
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.param_functions import Query 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 . import satsdice_ext
from .crud import ( from .crud import (
create_satsdice_payment,
get_satsdice_pay,
get_satsdice_withdraw_by_hash, get_satsdice_withdraw_by_hash,
update_satsdice_withdraw, update_satsdice_withdraw,
get_satsdice_pay,
create_satsdice_payment,
)
from lnurl import (
LnurlPayResponse,
LnurlPayActionResponse,
LnurlErrorResponse,
) )
from .models import CreateSatsDicePayment from .models import CreateSatsDicePayment
##############LNURLP STUFF ##############LNURLP STUFF

View File

@@ -342,7 +342,7 @@
LNbits.api LNbits.api
.request( .request(
'GET', 'GET',
'/satsdice/api/v1/links?all_wallets', '/satsdice/api/v1/links?all_wallets=true',
this.g.user.wallets[0].inkey this.g.user.wallets[0].inkey
) )
.then(response => { .then(response => {
@@ -446,7 +446,7 @@
key === 'success_url') && key === 'success_url') &&
(value === null || value === '') (value === null || value === '')
) )
LNbits.api LNbits.api
.request( .request(
'PUT', 'PUT',
@@ -516,9 +516,9 @@
if (this.g.user.wallets.length) { if (this.g.user.wallets.length) {
var getPayLinks = this.getPayLinks var getPayLinks = this.getPayLinks
getPayLinks() getPayLinks()
this.checker = setInterval(() => { // this.checker = setInterval(() => {
getPayLinks() // getPayLinks()
}, 20000) // }, 20000)
} }
} }
}) })

View File

@@ -1,48 +1,53 @@
import random
from datetime import datetime from datetime import datetime
from http import HTTPStatus 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 import FastAPI, Request
from fastapi.param_functions import Query
from fastapi.params import Depends from fastapi.params import Depends
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse from starlette.responses import HTMLResponse
from lnbits.core.models import User, Payment
from fastapi.params import Depends from lnbits.core.crud import (
from fastapi.param_functions import Query delete_expired_invoices,
import random get_balance_checks,
from .models import CreateSatsDiceWithdraw 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") templates = Jinja2Templates(directory="templates")
@satsdice_ext.get("/") @satsdice_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)): async def index(request: Request, user: User = Depends(check_user_exists)):
return satsdice_renderer().TemplateResponse( return satsdice_renderer().TemplateResponse(
"satsdice/index.html", {"request": request, "user": user.dict()} "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)): async def display(request: Request, link_id: str = Query(None)):
link = await get_satsdice_pay(link_id) or abort( link = await get_satsdice_pay(link_id)
HTTPStatus.NOT_FOUND, "satsdice link does not exist." if not link:
) raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist."
)
return satsdice_renderer().TemplateResponse( return satsdice_renderer().TemplateResponse(
"satsdice/display.html", "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( async def displaywin(
request: Request, link_id: str = Query(None), payment_hash: str = Query(None) request: Request, link_id: str = Query(None), payment_hash: str = Query(None)
): ):
satsdicelink = await get_satsdice_pay(link_id) or abort( satsdicelink = await get_satsdice_pay(link_id)
HTTPStatus.NOT_FOUND, "satsdice link does not exist." if not satsdiceLink:
) raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist."
)
withdrawLink = await get_satsdice_withdraw(payment_hash) withdrawLink = await get_satsdice_withdraw(payment_hash)
if withdrawLink: 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): async def img(link_id):
link = await get_satsdice_pay(link_id) or abort( link = await get_satsdice_pay(link_id) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist." HTTPStatus.NOT_FOUND, "satsdice link does not exist."

View File

@@ -31,7 +31,7 @@ from .models import CreateSatsDiceLink, CreateSatsDiceWithdraws
async def api_links( async def api_links(
request: Request, request: Request,
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: str = Query(None), all_wallets: bool = Query(False),
): ):
wallet_ids = [wallet.wallet.id] 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") raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request")
if link_id: if link_id:
link = await get_satsdice_pay(link_id) link = await get_satsdice_pay(link_id)
if not link: if not link:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Satsdice does not exist" 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, status_code=HTTPStatus.FORBIDDEN,
detail="Come on, seriously, this isn't your satsdice!", 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 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}} return {**link.dict(), **{"lnurl": link.lnurl}}

View File

@@ -59,24 +59,16 @@ async def api_lnurl_callback(
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found." status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
) )
# return (
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
# HTTPStatus.OK,
# )
if link.is_spent: if link.is_spent:
raise HTTPException( raise HTTPException(
# WHAT STATUS_CODE TO USE?? # WHAT STATUS_CODE TO USE??
detail="Withdraw is spent." detail="Withdraw is spent."
) )
# return (
# {"status": "ERROR", "reason": "Withdraw is spent."},
# HTTPStatus.OK,
# )
if link.k1 != k1: if link.k1 != k1:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.") raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.")
# return {"status": "ERROR", "reason": "Bad request."}, HTTPStatus.OK
if now < link.open_time: if now < link.open_time:
return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."} return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}