diff --git a/routstr/balance.py b/routstr/balance.py index 68e5ab2e..9e3063b7 100644 --- a/routstr/balance.py +++ b/routstr/balance.py @@ -85,7 +85,7 @@ async def refund_wallet_endpoint( key.refund_mint_url or PRIMARY_MINT_URL, key.refund_address, ) - result = {"recipient": key.refund_address, "msats": remaining_balance_msats} + result = {"recipient": key.refund_address} else: refund_amount = ( remaining_balance_msats // 1000 @@ -96,12 +96,13 @@ async def refund_wallet_endpoint( token = await send_token( refund_amount, refund_currency, key.refund_mint_url ) + result = {"token": token} + + if key.refund_currency == "sat": + result["sats"] = str(remaining_balance_msats // 1000) + else: + result["msats"] = str(remaining_balance_msats) - result = { - "msats": remaining_balance_msats, - "recipient": None, - "token": token, - } except HTTPException: # Re-raise HTTP exceptions (like 400 for balance too small) raise diff --git a/routstr/payment/models.py b/routstr/payment/models.py index 1ad0f644..48dce0f8 100644 --- a/routstr/payment/models.py +++ b/routstr/payment/models.py @@ -176,6 +176,6 @@ async def update_sats_pricing() -> None: @models_router.get("/v1/models") -@models_router.get("/models") +@models_router.get("/models", include_in_schema=False) async def models() -> dict: return {"data": MODELS} diff --git a/routstr/wallet.py b/routstr/wallet.py index 83acbdba..4543ce35 100644 --- a/routstr/wallet.py +++ b/routstr/wallet.py @@ -234,6 +234,8 @@ async def fetch_all_balances( ) proofs = await slow_filter_spend_proofs(proofs, wallet) user_balance = await db.balances_for_mint_and_unit(session, mint_url, unit) + if unit == "sat": + user_balance = user_balance // 1000 proofs_balance = sum(proof.amount for proof in proofs) result: BalanceDetail = { @@ -317,12 +319,14 @@ async def periodic_payout() -> None: user_balance = await db.balances_for_mint_and_unit( session, mint_url, unit ) + if unit == "sat": + user_balance = user_balance // 1000 proofs_balance = sum(proof.amount for proof in proofs) available_balance = proofs_balance - user_balance print(f"Balance: {proofs_balance} {unit}") print(f"User balance: {user_balance} {unit}") print(f"Available balance: {available_balance} {unit}") - min_amount = 5 if unit == "sat" else 5000 + min_amount = 210 if unit == "sat" else 210000 if proofs_balance > min_amount: amount_received = await raw_send_to_lnurl( wallet, proofs, RECEIVE_LN_ADDRESS, unit diff --git a/tests/integration/test_example.py b/tests/integration/test_example.py index e4c025ac..6657517c 100644 --- a/tests/integration/test_example.py +++ b/tests/integration/test_example.py @@ -105,7 +105,15 @@ async def test_full_wallet_flow( assert refund_response.status_code == 200 refund_data = refund_response.json() assert "token" in refund_data - assert refund_data["msats"] == (initial_amount + topup_amount) * 1000 + + # Check for either sats or msats depending on refund_currency + total_amount = initial_amount + topup_amount + if "sats" in refund_data: + assert refund_data["sats"] == str(total_amount) + elif "msats" in refund_data: + assert refund_data["msats"] == str(total_amount * 1000) + else: + pytest.fail("Response should contain either 'sats' or 'msats'") @pytest.mark.integration diff --git a/tests/integration/test_wallet_refund.py b/tests/integration/test_wallet_refund.py index 9cbf2a76..c2461cae 100644 --- a/tests/integration/test_wallet_refund.py +++ b/tests/integration/test_wallet_refund.py @@ -41,14 +41,18 @@ async def test_full_balance_refund_returns_cashu_token( assert response.status_code == 200 data = response.json() - # Should return msats, recipient (None), and token - assert "msats" in data - assert "recipient" in data + # Should return either sats or msats (as string), and token assert "token" in data - assert data["msats"] == initial_balance - assert data["recipient"] is None assert data["token"].startswith("cashuA") + # Check for either sats or msats depending on refund_currency + if "sats" in data: + assert data["sats"] == str(initial_balance // 1000) # Convert msats to sats + elif "msats" in data: + assert data["msats"] == str(initial_balance) + else: + pytest.fail("Response should contain either 'sats' or 'msats'") + # Validate token format token = data["token"] try: @@ -88,7 +92,14 @@ async def test_partial_refund_not_supported( # Should still refund full balance (endpoint ignores the parameter) assert response.status_code == 200 data = response.json() - assert data["msats"] == 10_000_000 # Full balance + + # Check for either sats or msats + if "sats" in data: + assert data["sats"] == "10000" # Full balance in sats + elif "msats" in data: + assert data["msats"] == "10000000" # Full balance in msats + else: + pytest.fail("Response should contain either 'sats' or 'msats'") @pytest.mark.integration @@ -432,13 +443,17 @@ async def test_refund_response_format( data = response.json() assert isinstance(data, dict) - assert "msats" in data - assert "recipient" in data assert "token" in data - assert isinstance(data["msats"], int) - assert data["recipient"] is None assert isinstance(data["token"], str) + # Should have either sats or msats (both as strings) + if "sats" in data: + assert isinstance(data["sats"], str) + elif "msats" in data: + assert isinstance(data["msats"], str) + else: + pytest.fail("Response should contain either 'sats' or 'msats'") + # Test 2: Test with refund address would require creating key via proxy endpoint # Since refund address headers only work on proxy endpoints, not wallet endpoints # Skip this part as it's already tested in test_refund_with_lightning_address