Browse Source

Internal payments working

internalpayments
arcbtc 5 years ago
parent
commit
748612e2d2
  1. 20
      lnbits/core/crud.py
  2. 20
      lnbits/core/migrations.py

20
lnbits/core/crud.py

@ -140,9 +140,9 @@ def get_wallet_payment(wallet_id: str, checking_id: str) -> Optional[Payment]:
with open_db() as db: with open_db() as db:
row = db.fetchone( row = db.fetchone(
""" """
SELECT payhash as checking_id, amount, fee, pending, memo, time SELECT id as checking_id, amount, fee, pending, memo, time
FROM apipayments FROM apipayment
WHERE wallet = ? AND payhash = ? WHERE wallet = ? AND id = ?
""", """,
(wallet_id, checking_id), (wallet_id, checking_id),
) )
@ -159,8 +159,8 @@ def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) ->
rows = db.fetchall( rows = db.fetchall(
f""" f"""
SELECT payhash as checking_id, amount, fee, pending, memo, time SELECT id as checking_id, amount, fee, pending, memo, time
FROM apipayments FROM apipayment
WHERE wallet = ? AND {clause} WHERE wallet = ? AND {clause}
ORDER BY time DESC ORDER BY time DESC
""", """,
@ -175,7 +175,7 @@ def delete_wallet_payments_expired(wallet_id: str, *, seconds: int = 86400) -> N
db.execute( db.execute(
""" """
DELETE DELETE
FROM apipayments WHERE wallet = ? AND pending = 1 AND time < strftime('%s', 'now') - ? FROM apipayment WHERE wallet = ? AND pending = 1 AND time < strftime('%s', 'now') - ?
""", """,
(wallet_id, seconds), (wallet_id, seconds),
) )
@ -191,7 +191,7 @@ def create_payment(
with open_db() as db: with open_db() as db:
db.execute( db.execute(
""" """
INSERT INTO apipayments (wallet, id, payment_hash, amount, pending, memo, fee) INSERT INTO apipayment (wallet, id, payment_hash, amount, pending, memo, fee)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
""", """,
(wallet_id, checking_id, payment_hash, amount, int(pending), memo, fee), (wallet_id, checking_id, payment_hash, amount, int(pending), memo, fee),
@ -205,17 +205,17 @@ def create_payment(
def update_payment_status(checking_id: str, pending: bool) -> None: def update_payment_status(checking_id: str, pending: bool) -> None:
with open_db() as db: with open_db() as db:
db.execute("UPDATE apipayments SET pending = ? WHERE id = ?", (int(pending), checking_id,)) db.execute("UPDATE apipayment SET pending = ? WHERE id = ?", (int(pending), checking_id,))
def delete_payment(checking_id: str) -> None: def delete_payment(checking_id: str) -> None:
with open_db() as db: with open_db() as db:
db.execute("DELETE FROM apipayments WHERE id = ?", (checking_id,)) db.execute("DELETE FROM apipayment WHERE id = ?", (checking_id,))
def check_internal(payment_hash: str) -> None: def check_internal(payment_hash: str) -> None:
with open_db() as db: with open_db() as db:
row = db.fetchone("SELECT * FROM apipayments WHERE payment_hash = ?", (payment_hash,)) row = db.fetchone("SELECT * FROM apipayment WHERE payment_hash = ?", (payment_hash,))
if not row: if not row:
return False return False
else: else:

20
lnbits/core/migrations.py

@ -51,6 +51,7 @@ def m001_initial(db):
); );
""" """
) )
db.execute( db.execute(
""" """
CREATE VIEW IF NOT EXISTS balances AS CREATE VIEW IF NOT EXISTS balances AS
@ -68,7 +69,24 @@ def m001_initial(db):
GROUP BY wallet; GROUP BY wallet;
""" """
) )
db.execute("DROP VIEW balances")
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 apipayment
WHERE amount > 0 AND pending = 0 -- don't sum pending
GROUP BY wallet
UNION ALL
SELECT wallet, SUM(amount + fee) AS s -- outgoing, sum fees
FROM apipayment
WHERE amount < 0 -- do sum pending
GROUP BY wallet
)
GROUP BY wallet;
"""
)
def m002_changed(db): def m002_changed(db):

Loading…
Cancel
Save