mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-23 19:54:18 +01:00
Allow to set maximum peg in/out for mint (#209)
* Allow to set maximum peg in/out for mint * Make format * remove duplicate error * move business logic to ledger --------- Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
@@ -116,6 +116,7 @@ class GetInfoResponse(BaseModel):
|
|||||||
contact: Optional[List[List[str]]] = None
|
contact: Optional[List[List[str]]] = None
|
||||||
nuts: Optional[List[str]] = None
|
nuts: Optional[List[str]] = None
|
||||||
motd: Optional[str] = None
|
motd: Optional[str] = None
|
||||||
|
parameter: Optional[dict] = None
|
||||||
|
|
||||||
|
|
||||||
# ------- API: KEYS -------
|
# ------- API: KEYS -------
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ class MintSettings(CashuSettings):
|
|||||||
mint_lightning_backend: str = Field(default="LNbitsWallet")
|
mint_lightning_backend: str = Field(default="LNbitsWallet")
|
||||||
mint_database: str = Field(default="data/mint")
|
mint_database: str = Field(default="data/mint")
|
||||||
mint_peg_out_only: bool = Field(default=False)
|
mint_peg_out_only: bool = Field(default=False)
|
||||||
|
mint_max_peg_in: int = Field(default=None)
|
||||||
|
mint_max_peg_out: int = Field(default=None)
|
||||||
|
|
||||||
mint_lnbits_endpoint: str = Field(default=None)
|
mint_lnbits_endpoint: str = Field(default=None)
|
||||||
mint_lnbits_key: str = Field(default=None)
|
mint_lnbits_key: str = Field(default=None)
|
||||||
|
|||||||
@@ -536,6 +536,12 @@ class Ledger:
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple[str, str]: Bolt11 invoice and a hash (for looking it up later)
|
Tuple[str, str]: Bolt11 invoice and a hash (for looking it up later)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if settings.mint_max_peg_in and amount > settings.mint_max_peg_in:
|
||||||
|
raise Exception(f"Maximum mint amount is {settings.mint_max_peg_in} sats.")
|
||||||
|
if settings.mint_peg_out_only:
|
||||||
|
raise Exception("Mint does not allow minting new tokens.")
|
||||||
|
|
||||||
payment_request, payment_hash = await self._request_lightning_invoice(amount)
|
payment_request, payment_hash = await self._request_lightning_invoice(amount)
|
||||||
assert payment_request and payment_hash, Exception(
|
assert payment_request and payment_hash, Exception(
|
||||||
"could not fetch invoice from Lightning backend"
|
"could not fetch invoice from Lightning backend"
|
||||||
@@ -619,6 +625,10 @@ class Ledger:
|
|||||||
total_provided = sum_proofs(proofs)
|
total_provided = sum_proofs(proofs)
|
||||||
invoice_obj = bolt11.decode(invoice)
|
invoice_obj = bolt11.decode(invoice)
|
||||||
invoice_amount = math.ceil(invoice_obj.amount_msat / 1000)
|
invoice_amount = math.ceil(invoice_obj.amount_msat / 1000)
|
||||||
|
if settings.mint_max_peg_out and invoice_amount > settings.mint_max_peg_out:
|
||||||
|
raise Exception(
|
||||||
|
f"Maximum melt amount is {settings.mint_max_peg_out} sats."
|
||||||
|
)
|
||||||
fees_msat = await self.check_fees(invoice)
|
fees_msat = await self.check_fees(invoice)
|
||||||
assert total_provided >= invoice_amount + fees_msat / 1000, Exception(
|
assert total_provided >= invoice_amount + fees_msat / 1000, Exception(
|
||||||
"provided proofs not enough for Lightning payment."
|
"provided proofs not enough for Lightning payment."
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ async def info():
|
|||||||
contact=settings.mint_info_contact,
|
contact=settings.mint_info_contact,
|
||||||
nuts=settings.mint_info_nuts,
|
nuts=settings.mint_info_nuts,
|
||||||
motd=settings.mint_info_motd,
|
motd=settings.mint_info_motd,
|
||||||
|
parameter={
|
||||||
|
"max_peg_in": settings.mint_max_peg_in,
|
||||||
|
"max_peg_out": settings.mint_max_peg_out,
|
||||||
|
"peg_out_only": settings.mint_peg_out_only,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -99,10 +104,13 @@ async def request_mint(amount: int = 0) -> Union[GetMintResponse, CashuError]:
|
|||||||
"""
|
"""
|
||||||
if settings.mint_peg_out_only:
|
if settings.mint_peg_out_only:
|
||||||
return CashuError(code=0, error="Mint does not allow minting new tokens.")
|
return CashuError(code=0, error="Mint does not allow minting new tokens.")
|
||||||
|
try:
|
||||||
payment_request, hash = await ledger.request_mint(amount)
|
payment_request, hash = await ledger.request_mint(amount)
|
||||||
print(f"Lightning invoice: {payment_request}")
|
print(f"Lightning invoice: {payment_request}")
|
||||||
resp = GetMintResponse(pr=payment_request, hash=hash)
|
resp = GetMintResponse(pr=payment_request, hash=hash)
|
||||||
return resp
|
return resp
|
||||||
|
except Exception as exc:
|
||||||
|
return CashuError(code=0, error=str(exc))
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
@@ -120,14 +128,11 @@ async def mint(
|
|||||||
|
|
||||||
Call this endpoint after `GET /mint`.
|
Call this endpoint after `GET /mint`.
|
||||||
"""
|
"""
|
||||||
if settings.mint_peg_out_only:
|
|
||||||
return CashuError(code=0, error="Mint does not allow minting new tokens.")
|
|
||||||
try:
|
try:
|
||||||
# BEGIN: backwards compatibility < 0.12 where we used to lookup payments with payment_hash
|
# BEGIN: backwards compatibility < 0.12 where we used to lookup payments with payment_hash
|
||||||
# We use the payment_hash to lookup the hash from the database and pass that one along.
|
# We use the payment_hash to lookup the hash from the database and pass that one along.
|
||||||
hash = payment_hash or hash
|
hash = payment_hash or hash
|
||||||
# END: backwards compatibility < 0.12
|
# END: backwards compatibility < 0.12
|
||||||
|
|
||||||
promises = await ledger.mint(payload.outputs, hash=hash)
|
promises = await ledger.mint(payload.outputs, hash=hash)
|
||||||
blinded_signatures = PostMintResponse(promises=promises)
|
blinded_signatures = PostMintResponse(promises=promises)
|
||||||
return blinded_signatures
|
return blinded_signatures
|
||||||
|
|||||||
Reference in New Issue
Block a user