From 7e6041080f43d91f80d445646969d866b213b5f4 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:31:50 +0200 Subject: [PATCH] selfpay to refresh tokens --- cashu/wallet/cli/cli.py | 27 +++++++++++++++++++++++++++ cashu/wallet/cli/cli_helpers.py | 3 +-- tests/test_cli.py | 11 +++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/cashu/wallet/cli/cli.py b/cashu/wallet/cli/cli.py index bdbe5167..56e5e01f 100644 --- a/cashu/wallet/cli/cli.py +++ b/cashu/wallet/cli/cli.py @@ -809,3 +809,30 @@ async def restore(ctx: Context, to: int, batch: int): await wallet.restore_wallet_from_mnemonic(mnemonic, to=to, batch=batch) await wallet.load_proofs() wallet.status() + + +@cli.command("selfpay", help="Refresh tokens.") +# @click.option("--all", default=False, is_flag=True, help="Execute on all available mints.") +@click.pass_context +@coro +async def selfpay(ctx: Context, all: bool = False): + wallet = await get_mint_wallet(ctx, force_select=True) + await wallet.load_mint() + + # get balance on this mint + mint_balance_dict = await wallet.balance_per_minturl() + mint_balance = mint_balance_dict[wallet.url]["available"] + # send balance once to mark as reserved + await wallet.split_to_send(wallet.proofs, mint_balance, None, set_reserved=True) + # load all reserved proofs (including the one we just sent) + reserved_proofs = await get_reserved_proofs(wallet.db) + if not len(reserved_proofs): + print("No balance on this mint.") + return + + token = await wallet.serialize_proofs(reserved_proofs) + print(f"Selfpay token for mint {wallet.url}:") + print("") + print(token) + tokenObj = TokenV3.deserialize(token) + await receive(wallet, tokenObj) diff --git a/cashu/wallet/cli/cli_helpers.py b/cashu/wallet/cli/cli_helpers.py index e7d12b21..d2b58c7e 100644 --- a/cashu/wallet/cli/cli_helpers.py +++ b/cashu/wallet/cli/cli_helpers.py @@ -46,12 +46,11 @@ async def get_mint_wallet(ctx: Context, force_select: bool = False): mint_url = wallet.url # load this mint_url into a wallet - mint_wallet = Wallet( + mint_wallet = await Wallet.with_db( mint_url, os.path.join(settings.cashu_dir, ctx.obj["WALLET_NAME"]), name=wallet.name, ) - # await mint_wallet.load_mint() await mint_wallet.load_proofs(reload=True) return mint_wallet diff --git a/tests/test_cli.py b/tests/test_cli.py index bc1208fd..9bb1f86b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -284,3 +284,14 @@ def test_pending(cli_prefix): assert result.exception is None print(result.output) assert result.exit_code == 0 + + +def test_selfpay(cli_prefix): + runner = CliRunner() + result = runner.invoke( + cli, + [*cli_prefix, "selfpay"], + ) + assert result.exception is None + print(result.output) + assert result.exit_code == 0