Wallet: Lightning interface (#318)

* mint does not start yet

* fix import

* revert mint db migrations

* handle zero fee case

* cli: adjust fee message

* wallet: replace requests with httpx

* clean up

* rename http client decorator

* fix pending check in main, todo: TEST PROXIES WITH HTTPX

* fix up

* use httpx for nostr as well

* update packages to same versions as https://github.com/lnbits/lnbits/pull/1609/files

* fix proof deserialization

* check for string

* tests passing

* adjust wallet api tests

* lockfile

* add correct responses to Lightning interface and delete melt_id for proofs for which the payent has failed

* fix create_invoice checking_id response

* migrations atomic

* proofs are stored automatically when created

* make format

* use bolt11 lib

* stricter type checking

* add fee response to payments

* assert fees in test_melt

* test that mint_id and melt_id is stored correctly in proofs and proofs_used

* remove traces

* refactor: Lightning interface into own file and LedgerCrud with typing

* fix tests

* fix payment response

* rename variable
This commit is contained in:
callebtc
2023-10-21 14:38:16 +02:00
committed by GitHub
parent 8a4813aee6
commit 0490f20932
41 changed files with 1899 additions and 1664 deletions

View File

@@ -168,9 +168,12 @@ async def pay(ctx: Context, invoice: str, yes: bool):
wallet.status()
total_amount, fee_reserve_sat = await wallet.get_pay_amount_with_fees(invoice)
if not yes:
potential = (
f" ({total_amount} sat with potential fees)" if fee_reserve_sat else ""
)
message = f"Pay {total_amount - fee_reserve_sat} sat{potential}?"
click.confirm(
f"Pay {total_amount - fee_reserve_sat} sat ({total_amount} sat with"
" potential fees)?",
message,
abort=True,
default=True,
)
@@ -187,7 +190,7 @@ async def pay(ctx: Context, invoice: str, yes: bool):
@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.option("--id", default="", help="Id of the paid invoice.", type=str)
@click.option(
"--split",
"-s",
@@ -197,7 +200,7 @@ async def pay(ctx: Context, invoice: str, yes: bool):
)
@click.pass_context
@coro
async def invoice(ctx: Context, amount: int, hash: str, split: int):
async def invoice(ctx: Context, amount: int, id: str, split: int):
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint()
wallet.status()
@@ -213,16 +216,16 @@ async def invoice(ctx: Context, amount: int, hash: str, split: int):
if not settings.lightning:
await wallet.mint(amount, split=optional_split)
# user requests an invoice
elif amount and not hash:
elif amount and not id:
invoice = await wallet.request_mint(amount)
if invoice.pr:
if invoice.bolt11:
print(f"Pay invoice to mint {amount} sat:")
print("")
print(f"Invoice: {invoice.pr}")
print(f"Invoice: {invoice.bolt11}")
print("")
print(
"If you abort this you can use this command to recheck the"
f" invoice:\ncashu invoice {amount} --hash {invoice.hash}"
f" invoice:\ncashu invoice {amount} --id {invoice.id}"
)
check_until = time.time() + 5 * 60 # check for five minutes
print("")
@@ -235,7 +238,7 @@ async def invoice(ctx: Context, amount: int, hash: str, split: int):
while time.time() < check_until and not paid:
time.sleep(3)
try:
await wallet.mint(amount, split=optional_split, hash=invoice.hash)
await wallet.mint(amount, split=optional_split, id=invoice.id)
paid = True
print(" Invoice paid.")
except Exception as e:
@@ -253,8 +256,8 @@ async def invoice(ctx: Context, amount: int, hash: str, split: int):
)
# user paid invoice and want to check it
elif amount and hash:
await wallet.mint(amount, split=optional_split, hash=hash)
elif amount and id:
await wallet.mint(amount, split=optional_split, id=id)
wallet.status()
return
@@ -285,17 +288,17 @@ async def swap(ctx: Context):
# pay invoice from outgoing mint
total_amount, fee_reserve_sat = await outgoing_wallet.get_pay_amount_with_fees(
invoice.pr
invoice.bolt11
)
if outgoing_wallet.available_balance < total_amount:
raise Exception("balance too low")
_, send_proofs = await outgoing_wallet.split_to_send(
outgoing_wallet.proofs, total_amount, set_reserved=True
)
await outgoing_wallet.pay_lightning(send_proofs, invoice.pr, fee_reserve_sat)
await outgoing_wallet.pay_lightning(send_proofs, invoice.bolt11, fee_reserve_sat)
# mint token in incoming mint
await incoming_wallet.mint(amount, hash=invoice.hash)
await incoming_wallet.mint(amount, id=invoice.id)
await incoming_wallet.load_proofs(reload=True)
await print_mint_balances(incoming_wallet, show_mints=True)
@@ -629,8 +632,8 @@ async def invoices(ctx):
print(f"Paid: {invoice.paid}")
print(f"Incoming: {invoice.amount > 0}")
print(f"Amount: {abs(invoice.amount)}")
if invoice.hash:
print(f"Hash: {invoice.hash}")
if invoice.id:
print(f"ID: {invoice.id}")
if invoice.preimage:
print(f"Preimage: {invoice.preimage}")
if invoice.time_created:
@@ -644,7 +647,7 @@ async def invoices(ctx):
)
print(f"Paid: {d}")
print("")
print(f"Payment request: {invoice.pr}")
print(f"Payment request: {invoice.bolt11}")
print("")
print("--------------------------\n")
else: