From 00928910deba38e48bdb7d655de60981678941fa Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:58:41 +0200 Subject: [PATCH] wallet can now handle PostMeltResponse_deprecated from v1 api --- cashu/wallet/v1_api.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cashu/wallet/v1_api.py b/cashu/wallet/v1_api.py index c4241226..36dc20df 100644 --- a/cashu/wallet/v1_api.py +++ b/cashu/wallet/v1_api.py @@ -7,6 +7,7 @@ import httpx from httpx import Response from loguru import logger +from pydantic import ValidationError from ..core.base import ( BlindedMessage, @@ -467,14 +468,26 @@ def _meltrequest_include_fields( json=payload.dict(include=_meltrequest_include_fields(proofs, outputs)), # type: ignore timeout=None, ) - # BEGIN backwards compatibility < 0.15.0 - # assume the mint has not upgraded yet if we get a 404 - if resp.status_code == 404: - invoice = await get_lightning_invoice(id=quote, db=self.db) - assert invoice, f"no invoice found for id {quote}" - ret: PostMeltResponse_deprecated = await self.melt_deprecated( - proofs=proofs, outputs=outputs, invoice=invoice.bolt11 - ) + try: + self.raise_on_error_request(resp) + return_dict = resp.json() + return PostMeltQuoteResponse.parse_obj(return_dict) + except Exception as e: + # BEGIN backwards compatibility < 0.15.0 + # assume the mint has not upgraded yet if we get a 404 + if resp.status_code == 404: + invoice = await get_lightning_invoice(id=quote, db=self.db) + assert invoice, f"no invoice found for id {quote}" + ret: PostMeltResponse_deprecated = await self.melt_deprecated( + proofs=proofs, outputs=outputs, invoice=invoice.bolt11 + ) + elif isinstance(e, ValidationError): + # BEGIN backwards compatibility < 0.16.0 + # before 0.16.0, mints return PostMeltResponse_deprecated + ret = PostMeltResponse_deprecated.parse_obj(return_dict) + # END backwards compatibility < 0.16.0 + else: + raise e return PostMeltQuoteResponse( quote=quote, amount=0, @@ -489,10 +502,7 @@ def _meltrequest_include_fields( change=ret.change, expiry=None, ) - # END backwards compatibility < 0.15.0 - self.raise_on_error_request(resp) - return_dict = resp.json() - return PostMeltQuoteResponse.parse_obj(return_dict) + # END backwards compatibility < 0.15.0 @async_set_httpx_client @async_ensure_mint_loaded