-
Notifications
You must be signed in to change notification settings - Fork 34
Feat/tendermint module queries #313
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
Changes from all commits
ec1fb93
52033b4
dd4fea0
f26f536
7521f81
1a4abc3
2016ad7
75a982e
136174c
6b102f2
211b888
60202fc
94726dd
3f0254c
cdd4bb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import asyncio | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| node_info = await client.fetch_node_info() | ||
| print(node_info) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.get_event_loop().run_until_complete(main()) | ||
|
Comment on lines
+1
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The script |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import asyncio | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| syncing = await client.fetch_syncing() | ||
| print(syncing) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.get_event_loop().run_until_complete(main()) | ||
|
Comment on lines
+1
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The script |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import asyncio | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| latest_block = await client.fetch_latest_block() | ||
| print(latest_block) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.get_event_loop().run_until_complete(main()) | ||
|
Comment on lines
+1
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The script |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import asyncio | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| block = await client.fetch_block_by_height(height=15793860) | ||
| print(block) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.get_event_loop().run_until_complete(main()) | ||
|
Comment on lines
+1
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The script |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import asyncio | ||
|
|
||
| from google.protobuf import symbol_database | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| validator_set = await client.fetch_latest_validator_set() | ||
| print(validator_set) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| symbol_db = symbol_database.Default() | ||
| asyncio.get_event_loop().run_until_complete(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import asyncio | ||
|
|
||
| from google.protobuf import symbol_database | ||
|
|
||
| from pyinjective.async_client import AsyncClient | ||
| from pyinjective.client.model.pagination import PaginationOption | ||
| from pyinjective.core.network import Network | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.testnet() | ||
| client = AsyncClient(network) | ||
|
|
||
| pagination = PaginationOption(skip=2, limit=4) | ||
|
|
||
| validator_set = await client.fetch_validator_set_by_height(height=23040174, pagination=pagination) | ||
| print(validator_set) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| symbol_db = symbol_database.Default() | ||
| asyncio.get_event_loop().run_until_complete(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket | ||
| from pyinjective.core.network import Network | ||
| from pyinjective.core.token import Token | ||
| from pyinjective.core.tx.grpc.tendermint_grpc_api import TendermintGrpcApi | ||
| from pyinjective.core.tx.grpc.tx_grpc_api import TxGrpcApi | ||
| from pyinjective.exceptions import NotFoundError | ||
| from pyinjective.proto.cosmos.auth.v1beta1 import query_pb2 as auth_query, query_pb2_grpc as auth_query_grpc | ||
|
|
@@ -49,6 +50,7 @@ | |
| query_pb2 as tendermint_query, | ||
| query_pb2_grpc as tendermint_query_grpc, | ||
| ) | ||
| from pyinjective.proto.cosmos.crypto.ed25519 import keys_pb2 as ed25519_keys # noqa: F401 for validator set responses | ||
| from pyinjective.proto.cosmos.tx.v1beta1 import service_pb2 as tx_service, service_pb2_grpc as tx_service_grpc | ||
| from pyinjective.proto.exchange import ( | ||
| injective_accounts_rpc_pb2 as exchange_accounts_rpc_pb, | ||
|
|
@@ -192,6 +194,12 @@ def __init__( | |
| metadata_query_provider=self._chain_cookie_metadata_requestor | ||
| ), | ||
| ) | ||
| self.tendermint_api = TendermintGrpcApi( | ||
| channel=self.chain_channel, | ||
| metadata_provider=lambda: self.network.chain_metadata( | ||
| metadata_query_provider=self._chain_cookie_metadata_requestor | ||
| ), | ||
| ) | ||
| self.token_factory_api = ChainGrpcTokenFactoryApi( | ||
| channel=self.chain_channel, | ||
| metadata_provider=lambda: self.network.chain_metadata( | ||
|
|
@@ -379,18 +387,15 @@ async def close_chain_channel(self): | |
|
|
||
| async def sync_timeout_height(self): | ||
| try: | ||
| block = await self.get_latest_block() | ||
| self.timeout_height = block.block.header.height + DEFAULT_TIMEOUTHEIGHT | ||
| block = await self.fetch_latest_block() | ||
| self.timeout_height = int(block["block"]["header"]["height"]) + DEFAULT_TIMEOUTHEIGHT | ||
|
Comment on lines
+390
to
+391
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method Would you like me to help generate tests for this method or open a GitHub issue to track this task? |
||
| except Exception as e: | ||
| LoggerProvider().logger_for_class(logging_class=self.__class__).debug( | ||
| f"error while fetching latest block, setting timeout height to 0: {e}" | ||
| ) | ||
| self.timeout_height = 0 | ||
|
|
||
| # default client methods | ||
| async def get_latest_block(self) -> tendermint_query.GetLatestBlockResponse: | ||
| req = tendermint_query.GetLatestBlockRequest() | ||
| return await self.stubCosmosTendermint.GetLatestBlock(req) | ||
|
|
||
| async def get_account(self, address: str) -> Optional[account_pb2.EthAccount]: | ||
| """ | ||
|
|
@@ -500,8 +505,8 @@ async def send_tx_block_mode(self, tx_byte: bytes) -> abci_type.TxResponse: | |
| return result.tx_response | ||
|
|
||
| async def get_chain_id(self) -> str: | ||
| latest_block = await self.get_latest_block() | ||
| return latest_block.block.header.chain_id | ||
| latest_block = await self.fetch_latest_block() | ||
| return latest_block["block"]["header"]["chainId"] | ||
|
Comment on lines
+508
to
+509
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method Would you like assistance in creating tests for this method or should I open a GitHub issue to ensure it's tracked? |
||
|
|
||
| async def get_grants(self, granter: str, grantee: str, **kwargs): | ||
| """ | ||
|
|
@@ -1225,6 +1230,44 @@ async def fetch_denoms_from_creator( | |
| async def fetch_tokenfactory_module_state(self) -> Dict[str, Any]: | ||
| return await self.token_factory_api.fetch_tokenfactory_module_state() | ||
|
|
||
| # ------------------------------ | ||
| # region Tendermint module | ||
| async def fetch_node_info(self) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_node_info() | ||
|
|
||
| async def fetch_syncing(self) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_syncing() | ||
|
|
||
| async def get_latest_block(self) -> tendermint_query.GetLatestBlockResponse: | ||
| """ | ||
| This method is deprecated and will be removed soon. Please use `fetch_latest_block` instead | ||
| """ | ||
| warn("This method is deprecated. Use fetch_latest_block instead", DeprecationWarning, stacklevel=2) | ||
| req = tendermint_query.GetLatestBlockRequest() | ||
| return await self.stubCosmosTendermint.GetLatestBlock(req) | ||
|
|
||
| async def fetch_latest_block(self) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_latest_block() | ||
|
|
||
| async def fetch_block_by_height(self, height: int) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_block_by_height(height=height) | ||
|
|
||
| async def fetch_latest_validator_set(self) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_latest_validator_set() | ||
|
|
||
| async def fetch_validator_set_by_height( | ||
| self, height: int, pagination: Optional[PaginationOption] = None | ||
| ) -> Dict[str, Any]: | ||
| return await self.tendermint_api.fetch_validator_set_by_height(height=height, pagination=pagination) | ||
|
|
||
| async def abci_query( | ||
| self, path: str, data: Optional[bytes] = None, height: Optional[int] = None, prove: bool = False | ||
| ) -> Dict[str, Any]: | ||
| return await self.tendermint_api.abci_query(path=path, data=data, height=height, prove=prove) | ||
|
|
||
| # endregion | ||
|
Comment on lines
+1234
to
+1268
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newly added methods in the Tendermint module ( Would you like help in creating tests for these Tendermint module methods, or should I open a GitHub issue to track this task? |
||
|
|
||
| # ------------------------------ | ||
| # Explorer RPC | ||
|
|
||
| async def get_tx_by_hash(self, tx_hash: str): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from typing import Any, Callable, Dict, Optional | ||
|
|
||
| from grpc.aio import Channel | ||
|
|
||
| from pyinjective.client.model.pagination import PaginationOption | ||
| from pyinjective.proto.cosmos.base.tendermint.v1beta1 import ( | ||
| query_pb2 as tendermint_query, | ||
| query_pb2_grpc as tendermint_query_grpc, | ||
| ) | ||
| from pyinjective.utils.grpc_api_request_assistant import GrpcApiRequestAssistant | ||
|
|
||
|
|
||
| class TendermintGrpcApi: | ||
| def __init__(self, channel: Channel, metadata_provider: Callable): | ||
| self._stub = tendermint_query_grpc.ServiceStub(channel) | ||
| self._assistant = GrpcApiRequestAssistant(metadata_provider=metadata_provider) | ||
|
|
||
| async def fetch_node_info(self) -> Dict[str, Any]: | ||
| request = tendermint_query.GetNodeInfoRequest() | ||
| response = await self._execute_call(call=self._stub.GetNodeInfo, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def fetch_syncing(self) -> Dict[str, Any]: | ||
| request = tendermint_query.GetSyncingRequest() | ||
| response = await self._execute_call(call=self._stub.GetSyncing, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def fetch_latest_block(self) -> Dict[str, Any]: | ||
| request = tendermint_query.GetLatestBlockRequest() | ||
| response = await self._execute_call(call=self._stub.GetLatestBlock, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def fetch_block_by_height(self, height: int) -> Dict[str, Any]: | ||
| request = tendermint_query.GetBlockByHeightRequest(height=height) | ||
| response = await self._execute_call(call=self._stub.GetBlockByHeight, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def fetch_latest_validator_set(self) -> Dict[str, Any]: | ||
| request = tendermint_query.GetLatestValidatorSetRequest() | ||
| response = await self._execute_call(call=self._stub.GetLatestValidatorSet, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def fetch_validator_set_by_height( | ||
| self, height: int, pagination: Optional[PaginationOption] = None | ||
| ) -> Dict[str, Any]: | ||
| if pagination is None: | ||
| pagination = PaginationOption() | ||
| request = tendermint_query.GetValidatorSetByHeightRequest( | ||
| height=height, pagination=pagination.create_pagination_request() | ||
| ) | ||
| response = await self._execute_call(call=self._stub.GetValidatorSetByHeight, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def abci_query( | ||
| self, path: str, data: Optional[bytes] = None, height: Optional[int] = None, prove: bool = False | ||
| ) -> Dict[str, Any]: | ||
| request = tendermint_query.ABCIQueryRequest(path=path, data=data, height=height, prove=prove) | ||
| response = await self._execute_call(call=self._stub.ABCIQuery, request=request) | ||
|
|
||
| return response | ||
|
|
||
| async def _execute_call(self, call: Callable, request) -> Dict[str, Any]: | ||
|
Comment on lines
+13
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The A few points to consider for further refinement:
Overall, the implementation aligns well with the objectives of enhancing the software by adding comprehensive support for Tendermint module queries. These suggestions aim to further improve the code's robustness, clarity, and maintainability. |
||
| return await self._assistant.execute_call(call=call, request=request) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entry for version 1.5.0 uses a placeholder date
9999-99-99. It's important to replace this with the actual release date before the changelog is finalized.Committable suggestion