From 045e0693126761454f82fdae7b12105142df126d Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Thu, 1 Apr 2021 00:02:24 +0100 Subject: [PATCH 1/3] All fees are negative --- lnbits/core/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index d623b1183..703817fc4 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -154,7 +154,7 @@ async def pay_invoice( if payment.checking_id: await create_payment( checking_id=payment.checking_id, - fee=payment.fee_msat, + fee=-abs(payment.fee_msat), preimage=payment.preimage, pending=payment.ok == None, conn=conn, From 11679a4fa71b81f08143bb74886763db14610aaf Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 31 Mar 2021 20:09:33 -0300 Subject: [PATCH 2/3] actually do the fee abs() thing on the balances view. --- lnbits/core/migrations.py | 26 ++++++++++++++++++++++++++ lnbits/core/services.py | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 890bf51fe..0f14d9df5 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -135,3 +135,29 @@ async def m003_add_invoice_webhook(db): await db.execute("ALTER TABLE apipayments ADD COLUMN webhook TEXT") await db.execute("ALTER TABLE apipayments ADD COLUMN webhook_status TEXT") + + +async def m004_ensure_fees_are_always_negative(db): + """ + Use abs() so wallet backends don't have to care about the sign of the fees. + """ + + await db.execute("DROP VIEW balances") + + await db.execute( + """ + CREATE VIEW IF NOT EXISTS balances AS + SELECT wallet, COALESCE(SUM(s), 0) AS balance FROM ( + SELECT wallet, SUM(amount) AS s -- incoming + FROM apipayments + WHERE amount > 0 AND pending = 0 -- don't sum pending + GROUP BY wallet + UNION ALL + SELECT wallet, SUM(amount - abs(fee)) AS s -- outgoing, sum fees + FROM apipayments + WHERE amount < 0 -- do sum pending + GROUP BY wallet + ) + GROUP BY wallet; + """ + ) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 703817fc4..d623b1183 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -154,7 +154,7 @@ async def pay_invoice( if payment.checking_id: await create_payment( checking_id=payment.checking_id, - fee=-abs(payment.fee_msat), + fee=payment.fee_msat, preimage=payment.preimage, pending=payment.ok == None, conn=conn, From 946279674a457fbb1f9c575b5aa3638d5f0242cb Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 31 Mar 2021 20:24:46 -0300 Subject: [PATCH 3/3] stop bloating the logs with checking routine. --- lnbits/core/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lnbits/core/models.py b/lnbits/core/models.py index 9f7b53a32..d0b648c1e 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -145,13 +145,13 @@ class Payment(NamedTuple): else: status = await WALLET.get_invoice_status(self.checking_id) - print( - f" - checking '{'in' if self.is_in else 'out'}' {self.checking_id}: {status}" - ) - if self.is_out and status.failed: + print(f" - deleting outgoing failed payment {self.checking_id}: {status}") await self.delete() elif not status.pending: + print( + f" - marking '{'in' if self.is_in else 'out'}' {self.checking_id} as not pending anymore: {status}" + ) await self.set_pending(status.pending) async def delete(self) -> None: