mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-23 19:54:18 +01:00
add msat support for strike backend (#755)
This commit is contained in:
@@ -605,15 +605,19 @@ class Amount:
|
|||||||
return self.cents_to_usd()
|
return self.cents_to_usd()
|
||||||
elif self.unit == Unit.sat:
|
elif self.unit == Unit.sat:
|
||||||
return self.sat_to_btc()
|
return self.sat_to_btc()
|
||||||
|
elif self.unit == Unit.msat:
|
||||||
|
return self.msat_to_btc()
|
||||||
else:
|
else:
|
||||||
raise Exception("Amount must be in satoshis or cents")
|
raise Exception("Amount must be in satoshis or cents")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_float(cls, amount: float, unit: Unit) -> "Amount":
|
def from_float(cls, amount: float, unit: Unit) -> "Amount":
|
||||||
if unit == Unit.usd or unit == Unit.eur:
|
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:
|
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:
|
else:
|
||||||
raise Exception("Amount must be in satoshis or cents")
|
raise Exception("Amount must be in satoshis or cents")
|
||||||
|
|
||||||
@@ -622,6 +626,12 @@ class Amount:
|
|||||||
raise Exception("Amount must be in satoshis")
|
raise Exception("Amount must be in satoshis")
|
||||||
return f"{self.amount/1e8:.8f}"
|
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:
|
def cents_to_usd(self) -> str:
|
||||||
if self.unit != Unit.usd and self.unit != Unit.eur:
|
if self.unit != Unit.usd and self.unit != Unit.eur:
|
||||||
raise Exception("Amount must be in cents")
|
raise Exception("Amount must be in cents")
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ class MintDeprecationFlags(MintSettings):
|
|||||||
class MintBackends(MintSettings):
|
class MintBackends(MintSettings):
|
||||||
mint_lightning_backend: str = Field(default="") # deprecated
|
mint_lightning_backend: str = Field(default="") # deprecated
|
||||||
mint_backend_bolt11_sat: str = Field(default="")
|
mint_backend_bolt11_sat: str = Field(default="")
|
||||||
|
mint_backend_bolt11_msat: str = Field(default="")
|
||||||
mint_backend_bolt11_usd: str = Field(default="")
|
mint_backend_bolt11_usd: str = Field(default="")
|
||||||
mint_backend_bolt11_eur: str = Field(default="")
|
mint_backend_bolt11_eur: str = Field(default="")
|
||||||
|
|
||||||
|
|||||||
@@ -155,6 +155,8 @@ class FakeWallet(LightningBackend):
|
|||||||
amount_msat = 0
|
amount_msat = 0
|
||||||
if self.unit == Unit.sat:
|
if self.unit == Unit.sat:
|
||||||
amount_msat = MilliSatoshi(amount.to(Unit.msat, round="up").amount)
|
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:
|
elif self.unit == Unit.usd or self.unit == Unit.eur:
|
||||||
amount_msat = MilliSatoshi(
|
amount_msat = MilliSatoshi(
|
||||||
math.ceil(amount.amount / self.fake_btc_price * 1e9)
|
math.ceil(amount.amount / self.fake_btc_price * 1e9)
|
||||||
@@ -250,7 +252,7 @@ class FakeWallet(LightningBackend):
|
|||||||
invoice_obj = decode(melt_quote.request)
|
invoice_obj = decode(melt_quote.request)
|
||||||
assert invoice_obj.amount_msat, "invoice has no amount."
|
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)
|
amount_msat = int(invoice_obj.amount_msat)
|
||||||
fees_msat = fee_reserve(amount_msat)
|
fees_msat = fee_reserve(amount_msat)
|
||||||
fees = Amount(unit=Unit.msat, amount=fees_msat)
|
fees = Amount(unit=Unit.msat, amount=fees_msat)
|
||||||
|
|||||||
@@ -89,16 +89,21 @@ INVOICE_RESULT_MAP = {
|
|||||||
class StrikeWallet(LightningBackend):
|
class StrikeWallet(LightningBackend):
|
||||||
"""https://docs.strike.me/api/"""
|
"""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
|
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(
|
def fee_int(
|
||||||
self, strike_quote: Union[StrikePaymentQuoteResponse, StrikePaymentResponse]
|
self,
|
||||||
|
strike_quote: Union[StrikePaymentQuoteResponse, StrikePaymentResponse],
|
||||||
|
unit: Unit,
|
||||||
) -> int:
|
) -> int:
|
||||||
fee_str = strike_quote.totalFee.amount
|
fee_str = strike_quote.totalFee.amount
|
||||||
if strike_quote.totalFee.currency == self.currency_map[Unit.sat]:
|
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 [
|
elif strike_quote.totalFee.currency in [
|
||||||
self.currency_map[Unit.usd],
|
self.currency_map[Unit.usd],
|
||||||
self.currency_map[Unit.eur],
|
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}"
|
f"Expected currency {self.currency_map[self.unit]}, got {strike_quote.amount.currency}"
|
||||||
)
|
)
|
||||||
amount = Amount.from_float(float(strike_quote.amount.amount), self.unit)
|
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(
|
quote = PaymentQuoteResponse(
|
||||||
amount=amount,
|
amount=amount,
|
||||||
@@ -245,7 +250,7 @@ class StrikeWallet(LightningBackend):
|
|||||||
)
|
)
|
||||||
|
|
||||||
payment = StrikePaymentResponse.parse_obj(r.json())
|
payment = StrikePaymentResponse.parse_obj(r.json())
|
||||||
fee = self.fee_int(payment)
|
fee = self.fee_int(payment, self.unit)
|
||||||
return PaymentResponse(
|
return PaymentResponse(
|
||||||
result=PAYMENT_RESULT_MAP[payment.state],
|
result=PAYMENT_RESULT_MAP[payment.state],
|
||||||
checking_id=payment.paymentId,
|
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 = await self.client.get(url=f"{self.endpoint}/v1/payments/{checking_id}")
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
payment = StrikePaymentResponse.parse_obj(r.json())
|
payment = StrikePaymentResponse.parse_obj(r.json())
|
||||||
fee = self.fee_int(payment)
|
fee = self.fee_int(payment, self.unit)
|
||||||
return PaymentStatus(
|
return PaymentStatus(
|
||||||
result=PAYMENT_RESULT_MAP[payment.state],
|
result=PAYMENT_RESULT_MAP[payment.state],
|
||||||
fee=Amount(self.unit, fee),
|
fee=Amount(self.unit, fee),
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ if settings.mint_backend_bolt11_sat:
|
|||||||
unit=Unit.sat
|
unit=Unit.sat
|
||||||
)
|
)
|
||||||
backends.setdefault(Method.bolt11, {})[Unit.sat] = backend_bolt11_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:
|
if settings.mint_backend_bolt11_usd:
|
||||||
backend_bolt11_usd = getattr(wallets_module, settings.mint_backend_bolt11_usd)(
|
backend_bolt11_usd = getattr(wallets_module, settings.mint_backend_bolt11_usd)(
|
||||||
unit=Unit.usd
|
unit=Unit.usd
|
||||||
|
|||||||
Reference in New Issue
Block a user