add msat support for strike backend (#755)

This commit is contained in:
callebtc
2025-05-18 15:48:15 +02:00
committed by GitHub
parent 53126456a5
commit 3e32dc40e3
5 changed files with 33 additions and 10 deletions

View File

@@ -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")

View File

@@ -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="")

View File

@@ -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)

View File

@@ -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),

View File

@@ -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