Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 7, 2023
1 parent 9f2f82d commit 4566c10
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cashu/mint/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion cashu/mint/router_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
GetMintResponse_deprecated,
KeysetsResponse_deprecated,
KeysResponse_deprecated,
PostMeltQuoteRequest,
PostMeltRequest_deprecated,
PostMeltResponse_deprecated,
PostMintQuoteRequest,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions cashu/wallet/lightning/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
14 changes: 9 additions & 5 deletions tests/test_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -95,15 +97,17 @@ 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,
B_="02634a2c2b34bec9e8a4aba4361f6bff02d7fa2365379b0840afe249a7a9d71237",
)
]
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",
)

Expand Down
17 changes: 9 additions & 8 deletions tests/test_mint_operations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
23 changes: 14 additions & 9 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
)

Expand All @@ -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}.",
)

Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit 4566c10

Please sign in to comment.