Add multi-mint swap to CLI and API (#212)

* Add multi-mint swap to CLI and API

* Add confirmation prompt to CLI

* Replace several assert statements

* Define response for API endpoint swap

* Adapt to latest changes on main

* Fix: missing argument after latest changes on main

* API: use local wallet for swap

* Improve swap command

* clean up cli swap

* fix comment

* clean up router

* fix up

* remove outgoing

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
sihamon
2023-06-23 20:04:28 +02:00
committed by GitHub
parent 25385f1f16
commit a0df47f13a
5 changed files with 103 additions and 3 deletions

View File

@@ -209,6 +209,48 @@ async def invoice(ctx: Context, amount: int, hash: str, split: int):
return
@cli.command("swap", help="Swap funds between mints.")
@click.pass_context
@coro
async def swap(ctx: Context):
if not settings.lightning:
raise Exception("lightning not supported.")
print("Select the mint to swap from:")
outgoing_wallet = await get_mint_wallet(ctx, force_select=True)
print("Select the mint to swap to:")
incoming_wallet = await get_mint_wallet(ctx, force_select=True)
await incoming_wallet.load_mint()
await outgoing_wallet.load_mint()
if incoming_wallet.url == outgoing_wallet.url:
raise Exception("mints for swap have to be different")
amount = int(input("Enter amount to swap in sats: "))
assert amount > 0, "amount is not positive"
# request invoice from incoming mint
invoice = await incoming_wallet.request_mint(amount)
# pay invoice from outgoing mint
total_amount, fee_reserve_sat = await outgoing_wallet.get_pay_amount_with_fees(
invoice.pr
)
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)
# mint token in incoming mint
await incoming_wallet.mint(amount, hash=invoice.hash)
await incoming_wallet.load_proofs(reload=True)
await print_mint_balances(incoming_wallet, show_mints=True)
@cli.command("balance", help="Balance.")
@click.option(
"--verbose",