mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-24 22:58:46 +01:00
Splitting extension into 2, SatsPay Server, a BTCPay Server type extension, and WatchOnly
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
from typing import List, Optional, Union
|
|
|
|
#from lnbits.db import open_ext_db
|
|
from . import db
|
|
from .models import Charges
|
|
|
|
from lnbits.helpers import urlsafe_short_hash
|
|
|
|
from embit import bip32
|
|
from embit import ec
|
|
from embit.networks import NETWORKS
|
|
from embit import base58
|
|
from embit.util import hashlib
|
|
import io
|
|
from embit.util import secp256k1
|
|
from embit import hashes
|
|
from binascii import hexlify
|
|
from quart import jsonify
|
|
from embit import script
|
|
from embit import ec
|
|
from embit.networks import NETWORKS
|
|
from binascii import unhexlify, hexlify, a2b_base64, b2a_base64
|
|
import httpx
|
|
|
|
|
|
###############CHARGES##########################
|
|
|
|
|
|
async def create_charge(walletid: str, user: str, title: Optional[str] = None, time: Optional[int] = None, amount: Optional[int] = None) -> Charges:
|
|
wallet = await get_watch_wallet(walletid)
|
|
address = await get_derive_address(walletid, wallet[4] + 1)
|
|
|
|
charge_id = urlsafe_short_hash()
|
|
await db.execute(
|
|
"""
|
|
INSERT INTO charges (
|
|
id,
|
|
user,
|
|
title,
|
|
wallet,
|
|
address,
|
|
time_to_pay,
|
|
amount,
|
|
balance
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
""",
|
|
(charge_id, user, title, walletid, address, time, amount, 0),
|
|
)
|
|
return await get_charge(charge_id)
|
|
|
|
|
|
async def get_charge(charge_id: str) -> Charges:
|
|
row = await db.fetchone("SELECT * FROM charges WHERE id = ?", (charge_id,))
|
|
return Charges.from_row(row) if row else None
|
|
|
|
|
|
async def get_charges(user: str) -> List[Charges]:
|
|
rows = await db.fetchall("SELECT * FROM charges WHERE user = ?", (user,))
|
|
for row in rows:
|
|
await check_address_balance(row.address)
|
|
rows = await db.fetchall("SELECT * FROM charges WHERE user = ?", (user,))
|
|
return [charges.from_row(row) for row in rows]
|
|
|
|
|
|
async def delete_charge(charge_id: str) -> None:
|
|
await db.execute("DELETE FROM charges WHERE id = ?", (charge_id,))
|
|
|
|
async def check_address_balance(address: str) -> List[Charges]:
|
|
address_data = await get_address(address)
|
|
mempool = await get_mempool(address_data.user)
|
|
|
|
try:
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(mempool.endpoint + "/api/address/" + address)
|
|
except Exception:
|
|
pass
|
|
|
|
amount_paid = r.json()['chain_stats']['funded_txo_sum'] - r.json()['chain_stats']['spent_txo_sum']
|
|
print(amount_paid)
|