From 3e32dc40e3f56145f63cd82d9466f6ed842ad17f Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 18 May 2025 15:48:15 +0200 Subject: [PATCH] add msat support for strike backend (#755) --- cashu/core/base.py | 14 ++++++++++++-- cashu/core/settings.py | 1 + cashu/lightning/fake.py | 4 +++- cashu/lightning/strike.py | 19 ++++++++++++------- cashu/mint/startup.py | 5 +++++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index 397eb4c..870e589 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -605,15 +605,19 @@ class Amount: return self.cents_to_usd() elif self.unit == Unit.sat: return self.sat_to_btc() + elif self.unit == Unit.msat: + return self.msat_to_btc() else: raise Exception("Amount must be in satoshis or cents") @classmethod def from_float(cls, amount: float, unit: Unit) -> "Amount": if unit == Unit.usd or unit == Unit.eur: - return cls(unit, int(amount * 100)) + return cls(unit, int(round(amount * 100))) elif unit == Unit.sat: - return cls(unit, int(amount * 1e8)) + return cls(unit, int(round(amount * 1e8))) + elif unit == Unit.msat: + return cls(unit, int(round(amount * 1e11))) else: raise Exception("Amount must be in satoshis or cents") @@ -622,6 +626,12 @@ class Amount: raise Exception("Amount must be in satoshis") return f"{self.amount/1e8:.8f}" + def msat_to_btc(self) -> str: + if self.unit != Unit.msat: + raise Exception("Amount must be in msat") + sat_amount = Amount(Unit.msat, self.amount).to(Unit.sat, round="up") + return f"{sat_amount.amount/1e8:.8f}" + def cents_to_usd(self) -> str: if self.unit != Unit.usd and self.unit != Unit.eur: raise Exception("Amount must be in cents") diff --git a/cashu/core/settings.py b/cashu/core/settings.py index 26f078c..3b469b9 100644 --- a/cashu/core/settings.py +++ b/cashu/core/settings.py @@ -92,6 +92,7 @@ class MintDeprecationFlags(MintSettings): class MintBackends(MintSettings): mint_lightning_backend: str = Field(default="") # deprecated mint_backend_bolt11_sat: str = Field(default="") + mint_backend_bolt11_msat: str = Field(default="") mint_backend_bolt11_usd: str = Field(default="") mint_backend_bolt11_eur: str = Field(default="") diff --git a/cashu/lightning/fake.py b/cashu/lightning/fake.py index 1a445d7..54344a8 100644 --- a/cashu/lightning/fake.py +++ b/cashu/lightning/fake.py @@ -155,6 +155,8 @@ class FakeWallet(LightningBackend): amount_msat = 0 if self.unit == Unit.sat: amount_msat = MilliSatoshi(amount.to(Unit.msat, round="up").amount) + elif self.unit == Unit.msat: + amount_msat = MilliSatoshi(amount.amount) elif self.unit == Unit.usd or self.unit == Unit.eur: amount_msat = MilliSatoshi( math.ceil(amount.amount / self.fake_btc_price * 1e9) @@ -250,7 +252,7 @@ class FakeWallet(LightningBackend): invoice_obj = decode(melt_quote.request) assert invoice_obj.amount_msat, "invoice has no amount." - if self.unit == Unit.sat: + if self.unit == Unit.sat or self.unit == Unit.msat: amount_msat = int(invoice_obj.amount_msat) fees_msat = fee_reserve(amount_msat) fees = Amount(unit=Unit.msat, amount=fees_msat) diff --git a/cashu/lightning/strike.py b/cashu/lightning/strike.py index 59bfb40..9d927f2 100644 --- a/cashu/lightning/strike.py +++ b/cashu/lightning/strike.py @@ -89,16 +89,21 @@ INVOICE_RESULT_MAP = { class StrikeWallet(LightningBackend): """https://docs.strike.me/api/""" - supported_units = {Unit.sat, Unit.usd, Unit.eur} + supported_units = {Unit.sat, Unit.msat, Unit.usd, Unit.eur} supports_description: bool = False - currency_map = {Unit.sat: "BTC", Unit.usd: "USD", Unit.eur: "EUR"} + currency_map = {Unit.sat: "BTC", Unit.msat: "BTC", Unit.usd: "USD", Unit.eur: "EUR"} def fee_int( - self, strike_quote: Union[StrikePaymentQuoteResponse, StrikePaymentResponse] + self, + strike_quote: Union[StrikePaymentQuoteResponse, StrikePaymentResponse], + unit: Unit, ) -> int: fee_str = strike_quote.totalFee.amount if strike_quote.totalFee.currency == self.currency_map[Unit.sat]: - fee = int(float(fee_str) * 1e8) + if unit == Unit.sat: + fee = int(float(fee_str) * 1e8) + elif unit == Unit.msat: + fee = int(float(fee_str) * 1e11) elif strike_quote.totalFee.currency in [ self.currency_map[Unit.usd], self.currency_map[Unit.eur], @@ -219,7 +224,7 @@ class StrikeWallet(LightningBackend): f"Expected currency {self.currency_map[self.unit]}, got {strike_quote.amount.currency}" ) amount = Amount.from_float(float(strike_quote.amount.amount), self.unit) - fee = self.fee_int(strike_quote) + fee = self.fee_int(strike_quote, self.unit) quote = PaymentQuoteResponse( amount=amount, @@ -245,7 +250,7 @@ class StrikeWallet(LightningBackend): ) payment = StrikePaymentResponse.parse_obj(r.json()) - fee = self.fee_int(payment) + fee = self.fee_int(payment, self.unit) return PaymentResponse( result=PAYMENT_RESULT_MAP[payment.state], checking_id=payment.paymentId, @@ -266,7 +271,7 @@ class StrikeWallet(LightningBackend): r = await self.client.get(url=f"{self.endpoint}/v1/payments/{checking_id}") r.raise_for_status() payment = StrikePaymentResponse.parse_obj(r.json()) - fee = self.fee_int(payment) + fee = self.fee_int(payment, self.unit) return PaymentStatus( result=PAYMENT_RESULT_MAP[payment.state], fee=Amount(self.unit, fee), diff --git a/cashu/mint/startup.py b/cashu/mint/startup.py index 4c99a3c..cf6ac31 100644 --- a/cashu/mint/startup.py +++ b/cashu/mint/startup.py @@ -53,6 +53,11 @@ if settings.mint_backend_bolt11_sat: unit=Unit.sat ) backends.setdefault(Method.bolt11, {})[Unit.sat] = backend_bolt11_sat +if settings.mint_backend_bolt11_msat: + backend_bolt11_msat = getattr(wallets_module, settings.mint_backend_bolt11_msat)( + unit=Unit.msat + ) + backends.setdefault(Method.bolt11, {})[Unit.msat] = backend_bolt11_msat if settings.mint_backend_bolt11_usd: backend_bolt11_usd = getattr(wallets_module, settings.mint_backend_bolt11_usd)( unit=Unit.usd