Skip to content

Commit

Permalink
NUTs: info endpoint method settings (#487)
Browse files Browse the repository at this point in the history
* adjust info endpoint settings

* respect MINT_MAX_PEG_IN MINT_MAX_PEG_OUT settings
  • Loading branch information
callebtc authored Mar 21, 2024
1 parent e93837f commit df2c81e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 7 additions & 0 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ def from_row(cls, row: Row):
# ------- API: INFO -------


class MintMeltMethodSetting(BaseModel):
method: str
unit: str
min_amount: Optional[int] = None
max_amount: Optional[int] = None


class GetInfoResponse(BaseModel):
name: Optional[str] = None
pubkey: Optional[str] = None
Expand Down
27 changes: 20 additions & 7 deletions cashu/mint/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
KeysetsResponseKeyset,
KeysResponse,
KeysResponseKeyset,
MintMeltMethodSetting,
PostCheckStateRequest,
PostCheckStateResponse,
PostMeltQuoteRequest,
Expand Down Expand Up @@ -41,19 +42,31 @@ async def info() -> GetInfoResponse:
logger.trace("> GET /v1/info")

# determine all method-unit pairs
method_unit_pairs: List[List[str]] = []
for method, unit_dict in ledger.backends.items():
for unit in unit_dict.keys():
method_unit_pairs.append([method.name, unit.name])
method_settings: Dict[int, List[MintMeltMethodSetting]] = {}
for nut in [4, 5]:
method_settings[nut] = []
for method, unit_dict in ledger.backends.items():
for unit in unit_dict.keys():
setting = MintMeltMethodSetting(method=method.name, unit=unit.name)

if nut == 4 and settings.mint_max_peg_in:
setting.max_amount = settings.mint_max_peg_in
setting.min_amount = 0
elif nut == 5 and settings.mint_max_peg_out:
setting.max_amount = settings.mint_max_peg_out
setting.min_amount = 0

method_settings[nut].append(setting)

supported_dict = dict(supported=True)

mint_features: Dict[int, Dict[str, Any]] = {
4: dict(
methods=method_unit_pairs,
disabled=False,
methods=method_settings[4],
disabled=settings.mint_peg_out_only,
),
5: dict(
methods=method_unit_pairs,
methods=method_settings[5],
disabled=False,
),
7: supported_dict,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_mint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pytest_asyncio

from cashu.core.base import (
GetInfoResponse,
MintMeltMethodSetting,
PostCheckStateRequest,
PostCheckStateResponse,
PostMintRequest,
Expand Down Expand Up @@ -40,6 +42,12 @@ async def test_info(ledger: Ledger):
assert response.status_code == 200, f"{response.url} {response.status_code}"
assert ledger.pubkey
assert response.json()["pubkey"] == ledger.pubkey.serialize().hex()
info = GetInfoResponse(**response.json())
assert info.nuts
assert info.nuts[4]["disabled"] is False
setting = MintMeltMethodSetting.parse_obj(info.nuts[4]["methods"][0])
assert setting.method == "bolt11"
assert setting.unit == "sat"


@pytest.mark.asyncio
Expand Down

0 comments on commit df2c81e

Please sign in to comment.