Browse Source

Internal payments workingish

Table is throwing sql error :(
internalpayments
arcbtc 4 years ago
parent
commit
443c841f88
  1. 3
      lnbits/__init__.py
  2. 19
      lnbits/core/crud.py
  3. 49
      lnbits/core/migrations.py
  4. 35
      lnbits/core/services.py

3
lnbits/__init__.py

@ -89,4 +89,5 @@ def migrate_databases():
# ----
if __name__ == "__main__":
app.run()
app.run(debug=True)

19
lnbits/core/crud.py

@ -186,15 +186,15 @@ def delete_wallet_payments_expired(wallet_id: str, *, seconds: int = 86400) -> N
def create_payment(
*, wallet_id: str, checking_id: str, amount: int, memo: str, fee: int = 0, pending: bool = True
*, wallet_id: str, checking_id: str, payment_hash: str, amount: int, memo: str, fee: int = 0, pending: bool = True
) -> Payment:
with open_db() as db:
db.execute(
"""
INSERT INTO apipayments (wallet, payhash, amount, pending, memo, fee)
INSERT INTO apipayments (wallet, id, payment_hash, amount, pending, memo, fee)
VALUES (?, ?, ?, ?, ?, ?)
""",
(wallet_id, checking_id, amount, int(pending), memo, fee),
(wallet_id, checking_id, payment_hash, amount, int(pending), memo, fee),
)
new_payment = get_wallet_payment(wallet_id, checking_id)
@ -205,9 +205,18 @@ def create_payment(
def update_payment_status(checking_id: str, pending: bool) -> None:
with open_db() as db:
db.execute("UPDATE apipayments SET pending = ? WHERE payhash = ?", (int(pending), checking_id,))
db.execute("UPDATE apipayments SET pending = ? WHERE id = ?", (int(pending), checking_id,))
def delete_payment(checking_id: str) -> None:
with open_db() as db:
db.execute("DELETE FROM apipayments WHERE payhash = ?", (checking_id,))
db.execute("DELETE FROM apipayments WHERE id = ?", (checking_id,))
def check_internal(payment_hash: str) -> None:
with open_db() as db:
row = db.fetchone("SELECT * FROM apipayments WHERE payment_hash = ?", (payment_hash,))
if not row:
return False
else:
return row['id']

49
lnbits/core/migrations.py

@ -70,6 +70,55 @@ def m001_initial(db):
)
def m002_changed(db):
db.execute(
"""
CREATE TABLE IF NOT EXISTS apipayment (
id TEXT NOT NULL,
payment_hash TEXT NOT NULL,
amount INTEGER NOT NULL,
fee INTEGER NOT NULL DEFAULT 0,
wallet TEXT NOT NULL,
pending BOOLEAN NOT NULL,
memo TEXT,
time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now')),
UNIQUE (wallet, id)
);
"""
)
for row in [list(row) for row in db.fetchall("SELECT * FROM apipayments")]:
db.execute(
"""
INSERT INTO apipayment (
id,
payment_hash,
amount,
fee,
wallet,
pending,
memo,
time
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
row[0],
"oldinvoice",
row[1],
row[2],
row[3],
row[4],
row[5],
row[6],
),
)
db.execute("DROP TABLE apipayments")
def migrate():
with open_db() as db:
m001_initial(db)
m002_changed(db)

35
lnbits/core/services.py

@ -4,7 +4,7 @@ from lnbits.bolt11 import decode as bolt11_decode # type: ignore
from lnbits.helpers import urlsafe_short_hash
from lnbits.settings import WALLET
from .crud import get_wallet, create_payment, delete_payment
from .crud import get_wallet, create_payment, delete_payment, check_internal
def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]:
@ -26,6 +26,8 @@ def pay_invoice(*, wallet_id: str, bolt11: str, max_sat: Optional[int] = None) -
temp_id = f"temp_{urlsafe_short_hash()}"
try:
invoice = bolt11_decode(bolt11)
print(invoice.payment_hash)
internal = check_internal(invoice.payment_hash)
if invoice.amount_msat == 0:
raise ValueError("Amountless invoices not supported.")
@ -34,25 +36,40 @@ def pay_invoice(*, wallet_id: str, bolt11: str, max_sat: Optional[int] = None) -
raise ValueError("Amount in invoice is too high.")
fee_reserve = max(1000, int(invoice.amount_msat * 0.01))
create_payment(
wallet_id=wallet_id,
checking_id=temp_id,
amount=-invoice.amount_msat,
fee=-fee_reserve,
memo=temp_id,
)
if not internal:
create_payment(
wallet_id=wallet_id,
checking_id=temp_id,
payment_hash=invoice.payment_hash,
amount=-invoice.amount_msat,
fee=-fee_reserve,
memo=temp_id,
)
wallet = get_wallet(wallet_id)
assert wallet, "invalid wallet id"
if wallet.balance_msat < 0:
raise PermissionError("Insufficient balance.")
print(internal)
if internal:
create_payment(
wallet_id=wallet_id,
checking_id=temp_id,
payment_hash=invoice.payment_hash,
amount=-invoice.amount_msat,
fee=0,
pending=False,
memo=invoice.description,
)
update_payment_status(checking_id=internal, pending=False)
return temp_id
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(bolt11)
if ok:
create_payment(
wallet_id=wallet_id,
checking_id=checking_id,
payment_hash=invoice.payment_hash,
amount=-invoice.amount_msat,
fee=fee_msat,
memo=invoice.description,

Loading…
Cancel
Save