mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-21 02:54:20 +01:00
check lightning invoices
This commit is contained in:
@@ -7,6 +7,7 @@ import math
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import time
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
@@ -39,8 +40,14 @@ class NaturalOrderGroup(click.Group):
|
|||||||
|
|
||||||
|
|
||||||
@click.group(cls=NaturalOrderGroup)
|
@click.group(cls=NaturalOrderGroup)
|
||||||
@click.option("--host", "-h", default=MINT_URL, help="Mint URL.")
|
@click.option("--host", "-h", default=MINT_URL, help=f"Mint URL (default: {MINT_URL}).")
|
||||||
@click.option("--wallet", "-w", "walletname", default="wallet", help="Wallet name.")
|
@click.option(
|
||||||
|
"--wallet",
|
||||||
|
"-w",
|
||||||
|
"walletname",
|
||||||
|
default="wallet",
|
||||||
|
help="Wallet name (default: wallet).",
|
||||||
|
)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def cli(ctx, host: str, walletname: str):
|
def cli(ctx, host: str, walletname: str):
|
||||||
# configure logger
|
# configure logger
|
||||||
@@ -64,7 +71,7 @@ def coro(f):
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
@cli.command("mint", help="Mint.")
|
@cli.command("invoice", help="Create Lighting invoice.")
|
||||||
@click.argument("amount", type=int)
|
@click.argument("amount", type=int)
|
||||||
@click.option("--hash", default="", help="Hash of the paid invoice.", type=str)
|
@click.option("--hash", default="", help="Hash of the paid invoice.", type=str)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
@@ -78,18 +85,61 @@ async def mint(ctx, amount: int, hash: str):
|
|||||||
elif amount and not hash:
|
elif amount and not hash:
|
||||||
r = await wallet.request_mint(amount)
|
r = await wallet.request_mint(amount)
|
||||||
if "pr" in r:
|
if "pr" in r:
|
||||||
print(f"Pay this invoice to mint {amount} sat:")
|
print(f"Pay invoice to mint {amount} sat:")
|
||||||
|
print("")
|
||||||
print(f"Invoice: {r['pr']}")
|
print(f"Invoice: {r['pr']}")
|
||||||
print("")
|
print("")
|
||||||
print(
|
print(
|
||||||
f"After paying the invoice, run this command:\ncashu mint {amount} --hash {r['hash']}"
|
f"Execute this command if you abort the check:\ncashu mint {amount} --hash {r['hash']}"
|
||||||
)
|
)
|
||||||
|
check_until = time.time() + 5 * 60 # check for five minutes
|
||||||
|
print("")
|
||||||
|
print(
|
||||||
|
f"Checking invoice ...",
|
||||||
|
end="",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
paid = False
|
||||||
|
while time.time() < check_until and not paid:
|
||||||
|
time.sleep(3)
|
||||||
|
try:
|
||||||
|
await wallet.mint(amount, r["hash"])
|
||||||
|
except Exception as e:
|
||||||
|
if str(e) == "Error: Lightning invoice not paid yet.":
|
||||||
|
print(".", end="", flush=True)
|
||||||
|
continue
|
||||||
|
paid = True
|
||||||
|
print(" Invoice paid.")
|
||||||
elif amount and hash:
|
elif amount and hash:
|
||||||
await wallet.mint(amount, hash)
|
await wallet.mint(amount, hash)
|
||||||
wallet.status()
|
wallet.status()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command("pay", help="Pay Lightning invoice.")
|
||||||
|
@click.argument("invoice", type=str)
|
||||||
|
@click.pass_context
|
||||||
|
@coro
|
||||||
|
async def pay(ctx, invoice: str):
|
||||||
|
wallet: Wallet = ctx.obj["WALLET"]
|
||||||
|
wallet.load_mint()
|
||||||
|
wallet.status()
|
||||||
|
decoded_invoice: Invoice = bolt11.decode(invoice)
|
||||||
|
amount = math.ceil(
|
||||||
|
(decoded_invoice.amount_msat + fee_reserve(decoded_invoice.amount_msat)) / 1000
|
||||||
|
) # 1% fee for Lightning
|
||||||
|
print(
|
||||||
|
f"Paying Lightning invoice of {decoded_invoice.amount_msat // 1000} sat ({amount} sat incl. fees)"
|
||||||
|
)
|
||||||
|
assert amount > 0, "amount is not positive"
|
||||||
|
if wallet.available_balance < amount:
|
||||||
|
print("Error: Balance too low.")
|
||||||
|
return
|
||||||
|
_, send_proofs = await wallet.split_to_send(wallet.proofs, amount)
|
||||||
|
await wallet.pay_lightning(send_proofs, amount, invoice)
|
||||||
|
wallet.status()
|
||||||
|
|
||||||
|
|
||||||
@cli.command("balance", help="Balance.")
|
@cli.command("balance", help="Balance.")
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
@coro
|
@coro
|
||||||
@@ -210,30 +260,6 @@ async def pending(ctx):
|
|||||||
wallet.status()
|
wallet.status()
|
||||||
|
|
||||||
|
|
||||||
@cli.command("pay", help="Pay Lightning invoice.")
|
|
||||||
@click.argument("invoice", type=str)
|
|
||||||
@click.pass_context
|
|
||||||
@coro
|
|
||||||
async def pay(ctx, invoice: str):
|
|
||||||
wallet: Wallet = ctx.obj["WALLET"]
|
|
||||||
wallet.load_mint()
|
|
||||||
wallet.status()
|
|
||||||
decoded_invoice: Invoice = bolt11.decode(invoice)
|
|
||||||
amount = math.ceil(
|
|
||||||
(decoded_invoice.amount_msat + fee_reserve(decoded_invoice.amount_msat)) / 1000
|
|
||||||
) # 1% fee for Lightning
|
|
||||||
print(
|
|
||||||
f"Paying Lightning invoice of {decoded_invoice.amount_msat // 1000} sat ({amount} sat incl. fees)"
|
|
||||||
)
|
|
||||||
assert amount > 0, "amount is not positive"
|
|
||||||
if wallet.available_balance < amount:
|
|
||||||
print("Error: Balance too low.")
|
|
||||||
return
|
|
||||||
_, send_proofs = await wallet.split_to_send(wallet.proofs, amount)
|
|
||||||
await wallet.pay_lightning(send_proofs, amount, invoice)
|
|
||||||
wallet.status()
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command("lock", help="Generate receiving lock.")
|
@cli.command("lock", help="Generate receiving lock.")
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
@coro
|
@coro
|
||||||
|
|||||||
Reference in New Issue
Block a user