Skip to content

Commit

Permalink
refactor feature construction
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 24, 2024
1 parent 78a03b3 commit 31337dc
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions cashu/mint/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

class LedgerFeatures(SupportsBackends):
def mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
mint_features = self.create_mint_features()
mint_features = self.add_supported_features(mint_features)
mint_features = self.add_mpp_features(mint_features)
mint_features = self.add_websocket_features(mint_features)
mint_features = self.add_cache_features(mint_features)

return mint_features

def create_mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
mint_method_settings: List[MintMethodSetting] = []
for method, unit_dict in self.backends.items():
for unit in unit_dict.keys():
Expand All @@ -43,8 +52,6 @@ def mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
melt_setting.min_amount = 0
melt_method_settings.append(melt_setting)

supported_dict = dict(supported=True)

mint_features: Dict[int, Union[List[Any], Dict[str, Any]]] = {
MINT_NUT: dict(
methods=mint_method_settings,
Expand All @@ -54,15 +61,25 @@ def mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
methods=melt_method_settings,
disabled=False,
),
STATE_NUT: supported_dict,
FEE_RETURN_NUT: supported_dict,
RESTORE_NUT: supported_dict,
SCRIPT_NUT: supported_dict,
P2PK_NUT: supported_dict,
DLEQ_NUT: supported_dict,
HTLC_NUT: supported_dict,
}
return mint_features

def add_supported_features(
self, mint_features: Dict[int, Union[List[Any], Dict[str, Any]]]
):
supported_dict = dict(supported=True)
mint_features[STATE_NUT] = supported_dict
mint_features[FEE_RETURN_NUT] = supported_dict
mint_features[RESTORE_NUT] = supported_dict
mint_features[SCRIPT_NUT] = supported_dict
mint_features[P2PK_NUT] = supported_dict
mint_features[DLEQ_NUT] = supported_dict
mint_features[HTLC_NUT] = supported_dict
return mint_features

def add_mpp_features(
self, mint_features: Dict[int, Union[List[Any], Dict[str, Any]]]
):
# signal which method-unit pairs support MPP
mpp_features = []
for method, unit_dict in self.backends.items():
Expand All @@ -79,6 +96,11 @@ def mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
if mpp_features:
mint_features[MPP_NUT] = dict(methods=mpp_features)

return mint_features

def add_websocket_features(
self, mint_features: Dict[int, Union[List[Any], Dict[str, Any]]]
):
# specify which websocket features are supported
# these two are supported by default
websocket_features: Dict[str, List[Dict[str, Union[str, List[str]]]]] = {
Expand Down Expand Up @@ -106,26 +128,30 @@ def mint_features(self) -> Dict[int, Union[List[Any], Dict[str, Any]]]:
if websocket_features:
mint_features[WEBSOCKETS_NUT] = websocket_features

return mint_features

def add_cache_features(
self, mint_features: Dict[int, Union[List[Any], Dict[str, Any]]]
):
if settings.mint_redis_cache_enabled:
cache_features: dict[str, list[dict[str, str]] | int] = {
"cached_endpoints": [
{
"path": "/v1/mint/bolt11",
"method": "POST",
"path": "/v1/mint/bolt11",
},
{
"path": "/v1/melt/bolt11",
"method": "POST",
"path": "/v1/melt/bolt11",
},
{
"path": "/v1/swap",
"method": "POST",
"path": "/v1/swap",
},
]
}
if settings.mint_redis_cache_ttl:
cache_features["ttl"] = settings.mint_redis_cache_ttl

mint_features[CACHE_NUT] = cache_features

return mint_features

0 comments on commit 31337dc

Please sign in to comment.