Skip to content

Commit

Permalink
selfpay to refresh tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 3, 2023
1 parent b35a7b1 commit 7e60410
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
27 changes: 27 additions & 0 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions cashu/wallet/cli/cli_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7e60410

Please sign in to comment.