diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 34bb0ee5..d181cb0f 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -380,9 +380,10 @@ async def melt_quote( async def melt( self, + *, proofs: List[Proof], quote: str, - outputs: Optional[List[BlindedMessage]], + outputs: Optional[List[BlindedMessage]] = None, keyset: Optional[MintKeyset] = None, ) -> Tuple[bool, str, List[BlindedSignature]]: """Invalidates proofs and pays a Lightning invoice. diff --git a/cashu/mint/router.py b/cashu/mint/router.py index 9819e684..6a855de9 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -223,7 +223,7 @@ async def melt(payload: PostMeltRequest) -> PostMeltResponse: """ logger.trace(f"> POST /v1/melt: {payload}") ok, preimage, change_promises = await ledger.melt( - payload.inputs, payload.quote, payload.outputs + proofs=payload.inputs, quote=payload.quote, outputs=payload.outputs ) resp = PostMeltResponse( quote="to_be_replaced", paid=ok, proof=preimage, change=change_promises diff --git a/cashu/mint/router_deprecated.py b/cashu/mint/router_deprecated.py index 1a67c4de..1ac0de10 100644 --- a/cashu/mint/router_deprecated.py +++ b/cashu/mint/router_deprecated.py @@ -9,6 +9,7 @@ GetMintResponse_deprecated, KeysetsResponse_deprecated, KeysResponse_deprecated, + PostMeltQuoteRequest, PostMeltRequest_deprecated, PostMeltResponse_deprecated, PostMintQuoteRequest, @@ -167,8 +168,11 @@ async def melt_deprecated( Requests tokens to be destroyed and sent out via Lightning. """ logger.trace(f"> POST /melt: {payload}") + quote = await ledger.melt_quote( + PostMeltQuoteRequest(request=payload.pr, symbol="sat", method="bolt11") + ) ok, preimage, change_promises = await ledger.melt( - payload.proofs, payload.pr, payload.outputs + proofs=payload.proofs, quote=quote.quote, outputs=payload.outputs ) resp = PostMeltResponse_deprecated( paid=ok, preimage=preimage, change=change_promises diff --git a/cashu/wallet/lightning/lightning.py b/cashu/wallet/lightning/lightning.py index 7efab863..63454079 100644 --- a/cashu/wallet/lightning/lightning.py +++ b/cashu/wallet/lightning/lightning.py @@ -54,18 +54,21 @@ async def pay_invoice(self, pr: str) -> PaymentResponse: Returns: bool: True if successful """ - total_amount, fee_reserve_sat = await self.get_pay_amount_with_fees(pr) + quote = await self.get_pay_amount_with_fees(pr) + total_amount = quote.amount + quote.fee_reserve assert total_amount > 0, "amount is not positive" if self.available_balance < total_amount: print("Error: Balance too low.") return PaymentResponse(ok=False) _, send_proofs = await self.split_to_send(self.proofs, total_amount) try: - resp = await self.pay_lightning(send_proofs, pr, fee_reserve_sat) + resp = await self.pay_lightning( + send_proofs, pr, quote.fee_reserve, quote.quote + ) if resp.change: - fees_paid_sat = fee_reserve_sat - sum_promises(resp.change) + fees_paid_sat = quote.fee_reserve - sum_promises(resp.change) else: - fees_paid_sat = fee_reserve_sat + fees_paid_sat = quote.fee_reserve invoice_obj = bolt11.decode(pr) return PaymentResponse( diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index a858456e..f0a65205 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -540,7 +540,6 @@ def _splitrequest_include_fields(proofs: List[Proof]): ) self.raise_on_error_request(resp) promises_dict = resp.json() - print(promises_dict) mint_response = PostSplitResponse.parse_obj(promises_dict) promises = [BlindedSignature(**p.dict()) for p in mint_response.signatures] diff --git a/tests/test_mint.py b/tests/test_mint.py index 36387e6b..e92c12bf 100644 --- a/tests/test_mint.py +++ b/tests/test_mint.py @@ -2,7 +2,7 @@ import pytest -from cashu.core.base import BlindedMessage, Proof +from cashu.core.base import BlindedMessage, PostMintQuoteRequest, Proof from cashu.core.crypto.b_dhke import step1_alice from cashu.core.helpers import calculate_number_of_blank_outputs from cashu.core.settings import settings @@ -77,14 +77,16 @@ async def test_get_keyset(ledger: Ledger): @pytest.mark.asyncio async def test_mint(ledger: Ledger): - invoice, id = await ledger.request_mint(8) + quote = await ledger.mint_quote( + PostMintQuoteRequest(amount=8, symbol="sat", method="bolt11") + ) blinded_messages_mock = [ BlindedMessage( amount=8, B_="02634a2c2b34bec9e8a4aba4361f6bf202d7fa2365379b0840afe249a7a9d71239", ) ] - promises = await ledger.mint(outputs=blinded_messages_mock, id=id) + promises = await ledger.mint(outputs=blinded_messages_mock, quote_id=quote.quote) assert len(promises) assert promises[0].amount == 8 assert ( @@ -95,7 +97,9 @@ async def test_mint(ledger: Ledger): @pytest.mark.asyncio async def test_mint_invalid_blinded_message(ledger: Ledger): - invoice, id = await ledger.request_mint(8) + quote = await ledger.mint_quote( + PostMintQuoteRequest(amount=8, symbol="sat", method="bolt11") + ) blinded_messages_mock_invalid_key = [ BlindedMessage( amount=8, @@ -103,7 +107,7 @@ async def test_mint_invalid_blinded_message(ledger: Ledger): ) ] await assert_err( - ledger.mint(outputs=blinded_messages_mock_invalid_key, id=id), + ledger.mint(outputs=blinded_messages_mock_invalid_key, quote_id=quote.quote), "invalid public key", ) diff --git a/tests/test_mint_operations.py b/tests/test_mint_operations.py index 35e8c479..b07f5099 100644 --- a/tests/test_mint_operations.py +++ b/tests/test_mint_operations.py @@ -1,6 +1,7 @@ import pytest import pytest_asyncio +from cashu.core.base import PostMeltQuoteRequest from cashu.mint.ledger import Ledger from cashu.wallet.wallet import Wallet from cashu.wallet.wallet import Wallet as Wallet1 @@ -27,15 +28,15 @@ async def test_melt(wallet1: Wallet, ledger: Ledger): invoice = await wallet1.request_mint(64) await wallet1.mint(64, id=invoice.id) assert wallet1.balance == 128 - total_amount, fee_reserve_sat = await wallet1.get_pay_amount_with_fees( - invoice.bolt11 - ) - mint_fees = await ledger.get_melt_fees(invoice.bolt11) - assert mint_fees == fee_reserve_sat - + mint_quote = await wallet1.get_pay_amount_with_fees(invoice.bolt11) + mint_fees = await ledger._get_lightning_fees(invoice.bolt11) + assert mint_fees == mint_quote.fee_reserve + total_amount = mint_quote.amount + mint_quote.fee_reserve keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, total_amount) - - await ledger.melt(send_proofs, invoice.bolt11, outputs=None) + melt_quote = await ledger.melt_quote( + PostMeltQuoteRequest(request=invoice.bolt11, symbol="sat", method="bolt11") + ) + await ledger.melt(proofs=send_proofs, quote=melt_quote.quote) @pytest.mark.asyncio diff --git a/tests/test_wallet.py b/tests/test_wallet.py index ab5a135d..c2c20b3c 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -158,8 +158,8 @@ async def test_mint(wallet1: Wallet): @pytest.mark.asyncio async def test_mint_amounts(wallet1: Wallet): """Mint predefined amounts""" - invoice = await wallet1.request_mint(64) amts = [1, 1, 1, 2, 2, 4, 16] + invoice = await wallet1.request_mint(sum(amts)) await wallet1.mint(amount=sum(amts), split=amts, id=invoice.id) assert wallet1.balance == 27 assert wallet1.proof_amounts == amts @@ -168,9 +168,11 @@ async def test_mint_amounts(wallet1: Wallet): @pytest.mark.asyncio async def test_mint_amounts_wrong_sum(wallet1: Wallet): """Mint predefined amounts""" + amts = [1, 1, 1, 2, 2, 4, 16] + invoice = await wallet1.request_mint(sum(amts)) await assert_err( - wallet1.mint(amount=sum(amts) + 1, split=amts), + wallet1.mint(amount=sum(amts) + 1, split=amts, id=invoice.id), "split must sum to amount", ) @@ -179,8 +181,9 @@ async def test_mint_amounts_wrong_sum(wallet1: Wallet): async def test_mint_amounts_wrong_order(wallet1: Wallet): """Mint amount that is not part in 2^n""" amts = [1, 2, 3] + invoice = await wallet1.request_mint(sum(amts)) await assert_err( - wallet1.mint(amount=sum(amts), split=[1, 2, 3]), + wallet1.mint(amount=sum(amts), split=[1, 2, 3], id=invoice.id), f"Can only mint amounts with 2^n up to {2**settings.max_order}.", ) @@ -236,16 +239,18 @@ async def test_melt(wallet1: Wallet): await wallet1.mint(64, id=invoice.id) assert wallet1.balance == 128 - total_amount, fee_reserve_sat = await wallet1.get_pay_amount_with_fees( - invoice.bolt11 - ) + quote = await wallet1.get_pay_amount_with_fees(invoice.bolt11) + total_amount = quote.amount + quote.fee_reserve assert total_amount == 66 - assert fee_reserve_sat == 2 + assert quote.fee_reserve == 2 _, send_proofs = await wallet1.split_to_send(wallet1.proofs, total_amount) melt_response = await wallet1.pay_lightning( - send_proofs, invoice=invoice.bolt11, fee_reserve_sat=fee_reserve_sat + send_proofs, + invoice=invoice.bolt11, + fee_reserve_sat=quote.fee_reserve, + quote_id=quote.quote, ) assert melt_response.change @@ -269,7 +274,7 @@ async def test_melt(wallet1: Wallet): assert all([p.melt_id == invoice_db.id for p in proofs_used]) # the payment was without fees so we need to remove it from the total amount - assert wallet1.balance == 128 - (total_amount - fee_reserve_sat) + assert wallet1.balance == 128 - (total_amount - quote.fee_reserve) assert wallet1.balance == 64