Merge pull request #1040 from lnbits/fix/splitpayments_scrub_multi_wallet

Fix/splitpayments scrub multi wallet
This commit is contained in:
Arc 2022-10-06 17:49:41 +01:00 committed by GitHub
commit 4fb4aa40e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View file

@ -4,6 +4,8 @@
SCRUB is a small but handy extension that allows a user to take advantage of all the functionalities inside **LNbits** and upon a payment received to your LNbits wallet, automatically forward it to your desired wallet via LNURL or LNAddress! SCRUB is a small but handy extension that allows a user to take advantage of all the functionalities inside **LNbits** and upon a payment received to your LNbits wallet, automatically forward it to your desired wallet via LNURL or LNAddress!
<small>Only whole values, integers, are Scrubbed, amounts will be rounded down (example: 6.3 will be 6)! The decimals, if existing, will be kept in your wallet!</small>
[**Wallets supporting LNURL**](https://github.com/fiatjaf/awesome-lnurl#wallets) [**Wallets supporting LNURL**](https://github.com/fiatjaf/awesome-lnurl#wallets)
## Usage ## Usage

View file

@ -1,6 +1,7 @@
import asyncio import asyncio
import json import json
from http import HTTPStatus from http import HTTPStatus
from math import floor
from urllib.parse import urlparse from urllib.parse import urlparse
import httpx import httpx
@ -26,7 +27,7 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
# (avoid loops) # (avoid loops)
if "scrubed" == payment.extra.get("tag"): if payment.extra.get("tag") == "scrubed":
# already scrubbed # already scrubbed
return return
@ -42,12 +43,13 @@ async def on_invoice_paid(payment: Payment) -> None:
# I REALLY HATE THIS DUPLICATION OF CODE!! CORE/VIEWS/API.PY, LINE 267 # I REALLY HATE THIS DUPLICATION OF CODE!! CORE/VIEWS/API.PY, LINE 267
domain = urlparse(data["callback"]).netloc domain = urlparse(data["callback"]).netloc
rounded_amount = floor(payment.amount / 1000) * 1000
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
r = await client.get( r = await client.get(
data["callback"], data["callback"],
params={"amount": payment.amount}, params={"amount": rounded_amount},
timeout=40, timeout=40,
) )
if r.is_error: if r.is_error:
@ -66,7 +68,8 @@ async def on_invoice_paid(payment: Payment) -> None:
) )
invoice = bolt11.decode(params["pr"]) invoice = bolt11.decode(params["pr"])
if invoice.amount_msat != payment.amount:
if invoice.amount_msat != rounded_amount:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, status_code=HTTPStatus.BAD_REQUEST,
detail=f"{domain} returned an invalid invoice. Expected {payment.amount} msat, got {invoice.amount_msat}.", detail=f"{domain} returned an invalid invoice. Expected {payment.amount} msat, got {invoice.amount_msat}.",

View file

@ -28,6 +28,10 @@ async def on_invoice_paid(payment: Payment) -> None:
# now we make some special internal transfers (from no one to the receiver) # now we make some special internal transfers (from no one to the receiver)
targets = await get_targets(payment.wallet_id) targets = await get_targets(payment.wallet_id)
if not targets:
return
transfers = [ transfers = [
(target.wallet, int(target.percent * payment.amount / 100)) (target.wallet, int(target.percent * payment.amount / 100))
for target in targets for target in targets
@ -41,9 +45,6 @@ async def on_invoice_paid(payment: Payment) -> None:
) )
return return
if not targets:
return
# mark the original payment with one extra key, "splitted" # mark the original payment with one extra key, "splitted"
# (this prevents us from doing this process again and it's informative) # (this prevents us from doing this process again and it's informative)
# and reduce it by the amount we're going to send to the producer # and reduce it by the amount we're going to send to the producer
@ -76,5 +77,5 @@ async def on_invoice_paid(payment: Payment) -> None:
) )
# manually send this for now # manually send this for now
await internal_invoice_queue.put(internal_checking_id) await internal_invoice_queue.put(internal_checking_id)
return return