Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set maximum peg in/out for mint #209

Merged
merged 4 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class GetInfoResponse(BaseModel):
contact: Optional[List[List[str]]] = None
nuts: Optional[List[str]] = None
motd: Optional[str] = None
parameter: Optional[dict] = None


# ------- API: KEYS -------
Expand Down
2 changes: 2 additions & 0 deletions cashu/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class MintSettings(CashuSettings):
mint_lightning_backend: str = Field(default="LNbitsWallet")
mint_database: str = Field(default="data/mint")
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_key: str = Field(default=None)
Expand Down
10 changes: 10 additions & 0 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ async def request_mint(self, amount: int):
Returns:
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)
assert payment_request and payment_hash, Exception(
"could not fetch invoice from Lightning backend"
Expand Down Expand Up @@ -619,6 +625,10 @@ async def melt(
total_provided = sum_proofs(proofs)
invoice_obj = bolt11.decode(invoice)
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)
assert total_provided >= invoice_amount + fees_msat / 1000, Exception(
"provided proofs not enough for Lightning payment."
Expand Down
19 changes: 12 additions & 7 deletions cashu/mint/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ async def info():
contact=settings.mint_info_contact,
nuts=settings.mint_info_nuts,
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,
},
)


Expand Down Expand Up @@ -99,10 +104,13 @@ async def request_mint(amount: int = 0) -> Union[GetMintResponse, CashuError]:
"""
if settings.mint_peg_out_only:
return CashuError(code=0, error="Mint does not allow minting new tokens.")
payment_request, hash = await ledger.request_mint(amount)
print(f"Lightning invoice: {payment_request}")
resp = GetMintResponse(pr=payment_request, hash=hash)
return resp
try:
payment_request, hash = await ledger.request_mint(amount)
print(f"Lightning invoice: {payment_request}")
resp = GetMintResponse(pr=payment_request, hash=hash)
return resp
except Exception as exc:
return CashuError(code=0, error=str(exc))


@router.post(
Expand All @@ -120,14 +128,11 @@ async def 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:
# 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.
hash = payment_hash or hash
# END: backwards compatibility < 0.12

promises = await ledger.mint(payload.outputs, hash=hash)
blinded_signatures = PostMintResponse(promises=promises)
return blinded_signatures
Expand Down