Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix depleted daily limit from unspent hits #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ async def get_hits(cards_ids: List[str]) -> List[Hit]:
return [Hit(**row) for row in rows]


async def get_hits_today(card_id: str) -> List[Hit]:
async def get_spent_hits_today(card_id: str) -> List[Hit]:
rows = await db.fetchall(
"SELECT * FROM boltcards.hits WHERE card_id = ?",
(card_id,),
"SELECT * FROM boltcards.hits WHERE card_id = ? AND spent = ?",
(card_id, True),
)
updatedrow = []
for row in rows:
Expand All @@ -202,6 +202,14 @@ async def spend_hit(id: str, amount: int):
return await get_hit(id)


async def unspend_hit(id: str):
await db.execute(
"UPDATE boltcards.hits SET spent = ? WHERE id = ?",
(False, id),
)
return await get_hit(id)


dni marked this conversation as resolved.
Show resolved Hide resolved
async def create_hit(card_id, ip, useragent, old_ctr, new_ctr) -> Hit:
hit_id = urlsafe_short_hash()
await db.execute(
Expand Down
34 changes: 20 additions & 14 deletions lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
get_card_by_external_id,
get_card_by_otp,
get_hit,
get_hits_today,
get_spent_hits_today,
spend_hit,
unspend_hit,
update_card_counter,
update_card_otp,
)
Expand Down Expand Up @@ -67,7 +68,7 @@ async def api_scan(p, c, request: Request, external_id: str):
ip = request.headers["x-forwarded-for"]

agent = request.headers["user-agent"] if "user-agent" in request.headers else ""
todays_hits = await get_hits_today(card.id)
todays_hits = await get_spent_hits_today(card.id)

hits_amount = 0
for hit in todays_hits:
Expand Down Expand Up @@ -118,18 +119,23 @@ async def lnurl_callback(

card = await get_card(hit.card_id)
assert card
hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000))
assert hit
try:
await pay_invoice(
wallet_id=card.wallet,
payment_request=pr,
max_sat=card.tx_limit,
extra={"tag": "boltcards", "hit": hit.id},
)
return {"status": "OK"}
except Exception as exc:
return {"status": "ERROR", "reason": f"Payment failed - {exc}"}

if invoice.amount_msat <= card.tx_limit * 1000:
dni marked this conversation as resolved.
Show resolved Hide resolved
hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000))
dni marked this conversation as resolved.
Show resolved Hide resolved
assert hit
try:
await pay_invoice(
wallet_id=card.wallet,
payment_request=pr,
max_sat=card.tx_limit,
extra={"tag": "boltcards", "hit": hit.id},
)
return {"status": "OK"}
except Exception as exc:
await unspend_hit(id=hit.id) # consider it unspent
return {"status": "ERROR", "reason": f"Payment failed - {exc}"}
else:
return {"status": "ERROR", "reason": "Amount exceeds card limit"}


# /boltcards/api/v1/auth?a=00000000000000000000000000000000
Expand Down
1 change: 1 addition & 0 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ new Vue({
.then(function (response) {
self.hits = response.data.map(function (obj) {
obj.card_name = self.cards.find(d => d.id == obj.card_id).card_name
obj.amount = obj.spent ? obj.amount : "(" + obj.amount + ")"
return mapCards(obj)
})
})
Expand Down