Skip to content

Commit

Permalink
feat: add blueprint list apis for builtin and on chain (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroferreira1 authored Feb 19, 2025
1 parent c9aa615 commit 17dbae3
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gateways/clients/hathor_core_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
NC_HISTORY_ENDPOINT = "/v1a/nano_contract/history"
NC_BLUEPRINT_INFORMATION_ENDPOINT = "/v1a/nano_contract/blueprint/info"
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT = "/v1a/nano_contract/blueprint/source"
NC_BUILTIN_BLUEPRINTS_ENDPOINT = "/v1a/nano_contract/blueprint/builtin"
NC_ON_CHAIN_BLUEPRINTS_ENDPOINT = "/v1a/nano_contract/blueprint/on_chain"
STATUS_ENDPOINT = "/v1a/status"
TOKEN_ENDPOINT = "/v1a/thin_wallet/token"
TOKEN_HISTORY_ENDPOINT = "/v1a/thin_wallet/token_history"
Expand Down
46 changes: 46 additions & 0 deletions gateways/node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
GRAPHVIZ_DOT_NEIGHBORS_ENDPOINT,
NC_BLUEPRINT_INFORMATION_ENDPOINT,
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT,
NC_BUILTIN_BLUEPRINTS_ENDPOINT,
NC_HISTORY_ENDPOINT,
NC_ON_CHAIN_BLUEPRINTS_ENDPOINT,
NC_STATE_ENDPOINT,
PUSH_TX_ENDPOINT,
TOKEN_ENDPOINT,
Expand Down Expand Up @@ -263,3 +265,47 @@ def get_nc_blueprint_source_code(self, blueprint_id: str) -> Optional[dict]:
params={"blueprint_id": blueprint_id},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)

def get_nc_builtin_blueprints(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
) -> Optional[dict]:
"""Get the list of built in blueprints."""
return self.hathor_core_client.get(
NC_BUILTIN_BLUEPRINTS_ENDPOINT,
params={
"after": after,
"count": count,
"before": before,
"find_blueprint_id": find_blueprint_id,
"find_blueprint_name": find_blueprint_name,
},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)

def get_nc_on_chain_blueprints(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
"""Get the list of on chain blueprints."""
return self.hathor_core_client.get(
NC_ON_CHAIN_BLUEPRINTS_ENDPOINT,
params={
"after": after,
"count": count,
"before": before,
"find_blueprint_id": find_blueprint_id,
"find_blueprint_name": find_blueprint_name,
"order": order,
},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)
65 changes: 65 additions & 0 deletions handlers/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,68 @@ def nc_blueprint_source_code(
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}


@ApiGateway()
def nc_builtin_blueprints(
event: ApiGatewayEvent, _context: LambdaContext, node_api: Optional[NodeApi] = None
) -> dict:
"""Get the list of built in blueprints."""
node_api = node_api or NodeApi()
after = event.query.get("after")
before = event.query.get("before")
count = event.query.get("count")
find_blueprint_id = event.query.get("find_blueprint_id")
find_blueprint_name = event.query.get("find_blueprint_name")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_builtin_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name
)

if response is None or "error" in response:
message = (
response.get("error")
if (response and "error" in response)
else "Unknown error"
)
raise ApiError(message)

return {
"statusCode": 200,
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}


@ApiGateway()
def nc_on_chain_blueprints(
event: ApiGatewayEvent, _context: LambdaContext, node_api: Optional[NodeApi] = None
) -> dict:
"""Get the list of on chain blueprints."""
node_api = node_api or NodeApi()
after = event.query.get("after")
before = event.query.get("before")
count = event.query.get("count")
find_blueprint_id = event.query.get("find_blueprint_id")
find_blueprint_name = event.query.get("find_blueprint_name")
order = event.query.get("order")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_on_chain_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name, order
)

if response is None or "error" in response:
message = (
response.get("error")
if (response and "error" in response)
else "Unknown error"
)
raise ApiError(message)

return {
"statusCode": 200,
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[tool.poetry]
name = "hathor-explorer-service"
version = "0.18.2"
package-mode = false
description = ""
authors = ["Hathor Labs <[email protected]>"]
license = "MIT"
Expand Down
49 changes: 49 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,55 @@ functions:
- name: request.querystring.blueprint_id
- name: request.header.origin

node_api_nc_builtin_blueprints:
handler: handlers/node_api.nc_builtin_blueprints
maximumRetryAttempts: 0
package:
patterns:
- 'handlers/node_api.py'
layers:
- { Ref: PythonRequirementsLambdaLayer }
events:
- http:
path: node_api/nc_builtin_blueprints
method: get
cors: true
caching:
enabled: true
ttlInSeconds: 300
cacheKeyParameters:
- name: request.querystring.after
- name: request.querystring.before
- name: request.querystring.count
- name: request.querystring.find_blueprint_id
- name: request.querystring.find_blueprint_name
- name: request.header.origin

node_api_nc_on_chain_blueprints:
handler: handlers/node_api.nc_on_chain_blueprints
maximumRetryAttempts: 0
package:
patterns:
- 'handlers/node_api.py'
layers:
- { Ref: PythonRequirementsLambdaLayer }
events:
- http:
path: node_api/nc_on_chain_blueprints
method: get
cors: true
caching:
enabled: true
ttlInSeconds: 300
cacheKeyParameters:
- name: request.querystring.after
- name: request.querystring.before
- name: request.querystring.count
- name: request.querystring.find_blueprint_id
- name: request.querystring.find_blueprint_name
- name: request.querystring.order
- name: request.header.origin

walletServiceAddressBalance:
handler: handlers/wallet_service.handle_address_balance
timeout: 15
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/gateways/test_node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,42 @@ def test_nc_blueprint_source_code(self, hathor_client):
)
assert result
assert sorted(result) == sorted(obj)

@patch("gateways.node_api_gateway.NC_BUILTIN_BLUEPRINTS_ENDPOINT", "mock-endpoint")
def test_nc_builtin_blueprints(self, hathor_client):
obj = {"foo": "bar"}
data = {
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
result = gateway.get_nc_builtin_blueprints(**data)
hathor_client.get.assert_called_once_with(
"mock-endpoint", params=data, timeout=NODE_API_TIMEOUT_IN_SECONDS
)
assert result
assert sorted(result) == sorted(obj)

@patch("gateways.node_api_gateway.NC_ON_CHAIN_BLUEPRINTS_ENDPOINT", "mock-endpoint")
def test_nc_on_chain_blueprints(self, hathor_client):
obj = {"foo": "bar"}
data = {
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"order": "asc",
}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
result = gateway.get_nc_on_chain_blueprints(**data)
hathor_client.get.assert_called_once_with(
"mock-endpoint", params=data, timeout=NODE_API_TIMEOUT_IN_SECONDS
)
assert result
assert sorted(result) == sorted(obj)
37 changes: 37 additions & 0 deletions tests/unit/usecases/test_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,40 @@ def test_get_blueprint_source_code(self, node_api_gateway):
node_api_gateway.get_nc_blueprint_source_code.assert_called_once_with("1234")
assert result
assert sorted(result) == sorted(obj)

def test_get_builtin_blueprints(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.get_nc_builtin_blueprints = MagicMock(return_value=obj)
node_api = NodeApi(node_api_gateway)
data = {
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
}
result = node_api.get_nc_builtin_blueprints(**data)
node_api_gateway.get_nc_builtin_blueprints.assert_called_once_with(
*data.values()
)
assert result
assert sorted(result) == sorted(obj)

def test_get_on_chain_blueprints(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.get_nc_on_chain_blueprints = MagicMock(return_value=obj)
node_api = NodeApi(node_api_gateway)
data = {
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"order": "asc",
}
result = node_api.get_nc_on_chain_blueprints(**data)
node_api_gateway.get_nc_on_chain_blueprints.assert_called_once_with(
*data.values()
)
assert result
assert sorted(result) == sorted(obj)
25 changes: 25 additions & 0 deletions usecases/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,28 @@ def get_nc_blueprint_information(self, blueprint_id: str) -> Optional[dict]:

def get_nc_blueprint_source_code(self, blueprint_id: str) -> Optional[dict]:
return self.node_api_gateway.get_nc_blueprint_source_code(blueprint_id)

def get_nc_builtin_blueprints(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
) -> Optional[dict]:
return self.node_api_gateway.get_nc_builtin_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name
)

def get_nc_on_chain_blueprints(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
return self.node_api_gateway.get_nc_on_chain_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name, order
)

0 comments on commit 17dbae3

Please sign in to comment.