From 52c992db5992e3710d7a23d196e8a15dcaa61e90 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:11:42 +0200 Subject: [PATCH] check lightning invoices --- cashu/wallet/cli.py | 84 +++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index ebc0edf..56f7058 100755 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -7,6 +7,7 @@ import math import os import sys from datetime import datetime +import time from functools import wraps from itertools import groupby from operator import itemgetter @@ -39,8 +40,14 @@ class NaturalOrderGroup(click.Group): @click.group(cls=NaturalOrderGroup) -@click.option("--host", "-h", default=MINT_URL, help="Mint URL.") -@click.option("--wallet", "-w", "walletname", default="wallet", help="Wallet name.") +@click.option("--host", "-h", default=MINT_URL, help=f"Mint URL (default: {MINT_URL}).") +@click.option( + "--wallet", + "-w", + "walletname", + default="wallet", + help="Wallet name (default: wallet).", +) @click.pass_context def cli(ctx, host: str, walletname: str): # configure logger @@ -64,7 +71,7 @@ def coro(f): return wrapper -@cli.command("mint", help="Mint.") +@cli.command("invoice", help="Create Lighting invoice.") @click.argument("amount", type=int) @click.option("--hash", default="", help="Hash of the paid invoice.", type=str) @click.pass_context @@ -78,18 +85,61 @@ async def mint(ctx, amount: int, hash: str): elif amount and not hash: r = await wallet.request_mint(amount) 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("") 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: await wallet.mint(amount, hash) wallet.status() 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.") @click.pass_context @coro @@ -210,30 +260,6 @@ async def pending(ctx): 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.") @click.pass_context @coro