choose which mint to send from

This commit is contained in:
callebtc
2022-12-26 23:20:42 +01:00
parent bdd3f3ae1f
commit 4c045abadd
3 changed files with 70 additions and 30 deletions

View File

@@ -47,7 +47,12 @@ from cashu.wallet.crud import (
) )
from cashu.wallet.wallet import Wallet as Wallet from cashu.wallet.wallet import Wallet as Wallet
from .cli_helpers import redeem_multimint, verify_mints from .cli_helpers import (
redeem_multimint,
verify_mints,
print_mint_balances,
get_mint_wallet,
)
async def init_wallet(wallet: Wallet): async def init_wallet(wallet: Wallet):
@@ -210,27 +215,7 @@ async def balance(ctx, verbose):
) )
print("") print("")
# get balances per mint await print_mint_balances(ctx, wallet)
mint_balances = await wallet.balance_per_minturl()
# if we have a balance on a non-default mint, we show its URL
show_mints = False
keysets = [k for k, v in wallet.balance_per_keyset().items()]
for k in keysets:
ks = await get_keyset(id=str(k), db=wallet.db)
if ks and ks.mint_url != ctx.obj["HOST"]:
show_mints = True
# or we have a balance on more than one mint
# show balances per mint
if len(mint_balances) > 1 or show_mints:
print(f"You have balances in {len(mint_balances)} mints:")
print("")
for k, v in mint_balances.items():
print(
f"Mint: {k} - Balance: {v['available']} sat (pending: {v['balance']-v['available']} sat)"
)
print("")
if verbose: if verbose:
print( print(
@@ -259,9 +244,10 @@ async def send(ctx, amount: int, lock: str, legacy: bool):
p2sh = False p2sh = False
if lock and len(lock.split("P2SH:")) == 2: if lock and len(lock.split("P2SH:")) == 2:
p2sh = True p2sh = True
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint() wallet = await get_mint_wallet(ctx)
wallet.status() await wallet.load_proofs()
_, send_proofs = await wallet.split_to_send( _, send_proofs = await wallet.split_to_send(
wallet.proofs, amount, lock, set_reserved=True wallet.proofs, amount, lock, set_reserved=True
) )
@@ -444,6 +430,7 @@ async def pending(ctx):
) )
print(f"{token}\n") print(f"{token}\n")
print(f"--------------------------\n") print(f"--------------------------\n")
print("To remove all spent tokens use: cashu burn -a")
wallet.status() wallet.status()
@@ -545,9 +532,8 @@ async def invoices(ctx):
@click.pass_context @click.pass_context
@coro @coro
async def nsend(ctx, amount: int, pubkey: str, verbose: bool, yes: bool): async def nsend(ctx, amount: int, pubkey: str, verbose: bool, yes: bool):
wallet: Wallet = ctx.obj["WALLET"] wallet = await get_mint_wallet(ctx)
await wallet.load_mint() await wallet.load_proofs()
wallet.status()
_, send_proofs = await wallet.split_to_send( _, send_proofs = await wallet.split_to_send(
wallet.proofs, amount, set_reserved=True wallet.proofs, amount, set_reserved=True
) )

View File

@@ -2,7 +2,7 @@ import os
import click import click
from cashu.core.base import Proof from cashu.core.base import Proof, WalletKeyset
from cashu.core.settings import CASHU_DIR from cashu.core.settings import CASHU_DIR
from cashu.wallet.crud import get_keyset from cashu.wallet.crud import get_keyset
from cashu.wallet.wallet import Wallet as Wallet from cashu.wallet.wallet import Wallet as Wallet
@@ -67,3 +67,56 @@ async def redeem_multimint(ctx, dtoken, script, signature):
_, _ = await keyset_wallet.redeem( _, _ = await keyset_wallet.redeem(
redeem_proofs, scnd_script=script, scnd_siganture=signature redeem_proofs, scnd_script=script, scnd_siganture=signature
) )
async def print_mint_balances(ctx, wallet, show_mints=False):
# get balances per mint
mint_balances = await wallet.balance_per_minturl()
# if we have a balance on a non-default mint, we show its URL
keysets = [k for k, v in wallet.balance_per_keyset().items()]
for k in keysets:
ks = await get_keyset(id=str(k), db=wallet.db)
if ks and ks.mint_url != ctx.obj["HOST"]:
show_mints = True
# or we have a balance on more than one mint
# show balances per mint
if len(mint_balances) > 1 or show_mints:
print(f"You have balances in {len(mint_balances)} mints:")
print("")
for i, (k, v) in enumerate(mint_balances.items()):
print(
f"Mint {i+1}: {k} - Balance: {v['available']} sat (pending: {v['balance']-v['available']} sat)"
)
print("")
async def get_mint_wallet(ctx):
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint()
mint_balances = await wallet.balance_per_minturl()
print(mint_balances)
if len(mint_balances) == 1:
return wallet
await print_mint_balances(ctx, wallet, show_mints=True)
mint_nr = input(
f"Which mint do you want to use? [1-{len(mint_balances)}, default: 1] "
)
mint_nr = "1" if mint_nr == "" else mint_nr
if not mint_nr.isdigit():
raise Exception("invalid input.")
mint_nr = int(mint_nr)
mint_url = list(mint_balances.keys())[mint_nr - 1]
mint_wallet = Wallet(mint_url, os.path.join(CASHU_DIR, ctx.obj["WALLET_NAME"]))
mint_keysets: WalletKeyset = await get_keyset(mint_url=mint_url, db=mint_wallet.db) # type: ignore
print(mint_keysets.id)
# load the keys
await mint_wallet.load_mint(keyset_id=mint_keysets.id)
return mint_wallet

View File

@@ -665,13 +665,14 @@ class Wallet(LedgerAPI):
async def balance_per_minturl(self): async def balance_per_minturl(self):
balances = await self._get_proofs_per_minturl(self.proofs) balances = await self._get_proofs_per_minturl(self.proofs)
return { balances_return = {
key: { key: {
"balance": sum_proofs(proofs), "balance": sum_proofs(proofs),
"available": sum_proofs([p for p in proofs if not p.reserved]), "available": sum_proofs([p for p in proofs if not p.reserved]),
} }
for key, proofs in balances.items() for key, proofs in balances.items()
} }
return dict(sorted(balances_return.items(), key=lambda item: item[1]["available"], reverse=True)) # type: ignore
def proof_amounts(self): def proof_amounts(self):
return [p["amount"] for p in sorted(self.proofs, key=lambda p: p["amount"])] return [p["amount"] for p in sorted(self.proofs, key=lambda p: p["amount"])]