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

feat/ibc_connection_module_queries #322

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Support for all queries in the "IBC Transfer" module
- Support for all queries in the "IBC Channel" module
- Support for all queries in the "IBC Client" module
- Support for all queries in the "IBC Connection" module

### Changed
- Refactored cookies management logic to use all gRPC calls' responses to update the current cookies
Expand Down
21 changes: 21 additions & 0 deletions examples/chain_client/ibc/connection/query/1_Connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)

connection_id = "connection-0"

connection = await client.fetch_ibc_connection(connection_id=connection_id)
print(connection)


if __name__ == "__main__":
symbol_db = symbol_database.Default()
asyncio.get_event_loop().run_until_complete(main())
22 changes: 22 additions & 0 deletions examples/chain_client/ibc/connection/query/2_Connections.py
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)

connections = await client.fetch_ibc_connections(pagination=pagination)
print(connections)


if __name__ == "__main__":
symbol_db = symbol_database.Default()
asyncio.get_event_loop().run_until_complete(main())
21 changes: 21 additions & 0 deletions examples/chain_client/ibc/connection/query/3_ClientConnections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)

client_id = "07-tendermint-0"

connections = await client.fetch_ibc_client_connections(client_id=client_id)
print(connections)


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,21 @@
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)

connection_id = "connection-0"

state = await client.fetch_ibc_connection_client_state(connection_id=connection_id)
print(state)


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,25 @@
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)

connection_id = "connection-0"
revision_number = 0
revision_height = 7379538

state = await client.fetch_ibc_connection_consensus_state(
connection_id=connection_id, revision_number=revision_number, revision_height=revision_height
)
print(state)


if __name__ == "__main__":
symbol_db = symbol_database.Default()
asyncio.get_event_loop().run_until_complete(main())
25 changes: 25 additions & 0 deletions examples/chain_client/ibc/connection/query/6_ConnectionParams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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)

connection_id = "connection-0"
revision_number = 0
revision_height = 7379538

state = await client.fetch_ibc_connection_consensus_state(
connection_id=connection_id, revision_number=revision_number, revision_height=revision_height
)
print(state)


if __name__ == "__main__":
symbol_db = symbol_database.Default()
asyncio.get_event_loop().run_until_complete(main())
Comment on lines +1 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for potential file duplication or incorrect content.

This script appears identical to 5_ConnectionConsensusState.py. Please verify if this is intentional or if there has been a mistake in file naming or content duplication.

35 changes: 34 additions & 1 deletion pyinjective/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from pyinjective.client.model.pagination import PaginationOption
from pyinjective.composer import Composer
from pyinjective.core.ibc.channel.grpc.ibc_channel_grpc_api import IBCChannelGrpcApi
from pyinjective.core.ibc.client.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.ibc.connection.grpc.ibc_connection_grpc_api import IBCConnectionGrpcApi
from pyinjective.core.ibc.transfer.grpc.ibc_transfer_grpc_api import IBCTransferGrpcApi
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.network import Network
Expand Down Expand Up @@ -189,6 +190,10 @@ def __init__(
channel=self.chain_channel,
cookie_assistant=network.chain_cookie_assistant,
)
self.ibc_connection_api = IBCConnectionGrpcApi(
channel=self.chain_channel,
cookie_assistant=network.chain_cookie_assistant,
)
self.ibc_transfer_api = IBCTransferGrpcApi(
channel=self.chain_channel,
cookie_assistant=network.chain_cookie_assistant,
Expand Down Expand Up @@ -3173,6 +3178,34 @@ async def fetch_ibc_upgraded_consensus_state(self) -> Dict[str, Any]:

# endregion

# region IBC Connection module
async def fetch_ibc_connection(self, connection_id: str) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_connection(connection_id=connection_id)

async def fetch_ibc_connections(self, pagination: Optional[PaginationOption] = None) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_connections(pagination=pagination)

async def fetch_ibc_client_connections(self, client_id: str) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_client_connections(client_id=client_id)

async def fetch_ibc_connection_client_state(self, connection_id: str) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_connection_client_state(connection_id=connection_id)

async def fetch_ibc_connection_consensus_state(
self,
connection_id: str,
revision_number: int,
revision_height: int,
) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_connection_consensus_state(
connection_id=connection_id, revision_number=revision_number, revision_height=revision_height
)

async def fetch_ibc_connection_params(self) -> Dict[str, Any]:
return await self.ibc_connection_api.fetch_connection_params()

# endregion

async def composer(self):
return Composer(
network=self.network.string(),
Expand Down
Empty file.
67 changes: 67 additions & 0 deletions pyinjective/core/ibc/connection/grpc/ibc_connection_grpc_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Any, Callable, Dict, Optional

from grpc import Channel

from pyinjective.client.model.pagination import PaginationOption
from pyinjective.core.network import CookieAssistant
from pyinjective.proto.ibc.core.connection.v1 import (
query_pb2 as ibc_connection_query,
query_pb2_grpc as ibc_connection_query_grpc,
)
from pyinjective.utils.grpc_api_request_assistant import GrpcApiRequestAssistant


class IBCConnectionGrpcApi:
def __init__(self, channel: Channel, cookie_assistant: CookieAssistant):
self._stub = ibc_connection_query_grpc.QueryStub(channel)
self._assistant = GrpcApiRequestAssistant(cookie_assistant=cookie_assistant)

async def fetch_connection(self, connection_id: str) -> Dict[str, Any]:
request = ibc_connection_query.QueryConnectionRequest(connection_id=connection_id)
response = await self._execute_call(call=self._stub.Connection, request=request)

return response

async def fetch_connections(self, pagination: Optional[PaginationOption] = None) -> Dict[str, Any]:
if pagination is None:
pagination = PaginationOption()
request = ibc_connection_query.QueryConnectionsRequest(pagination=pagination.create_pagination_request())
response = await self._execute_call(call=self._stub.Connections, request=request)

return response

async def fetch_client_connections(self, client_id: str) -> Dict[str, Any]:
request = ibc_connection_query.QueryClientConnectionsRequest(client_id=client_id)
response = await self._execute_call(call=self._stub.ClientConnections, request=request)

return response

async def fetch_connection_client_state(self, connection_id: str) -> Dict[str, Any]:
request = ibc_connection_query.QueryConnectionClientStateRequest(connection_id=connection_id)
response = await self._execute_call(call=self._stub.ConnectionClientState, request=request)

return response

async def fetch_connection_consensus_state(
self,
connection_id: str,
revision_number: int,
revision_height: int,
) -> Dict[str, Any]:
request = ibc_connection_query.QueryConnectionConsensusStateRequest(
connection_id=connection_id,
revision_number=revision_number,
revision_height=revision_height,
)
response = await self._execute_call(call=self._stub.ConnectionConsensusState, request=request)

return response

async def fetch_connection_params(self) -> Dict[str, Any]:
request = ibc_connection_query.QueryConnectionParamsRequest()
response = await self._execute_call(call=self._stub.ConnectionParams, request=request)

return response

async def _execute_call(self, call: Callable, request) -> Dict[str, Any]:
return await self._assistant.execute_call(call=call, request=request)
2 changes: 1 addition & 1 deletion tests/core/ibc/client/grpc/test_ibc_client_grpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from google.protobuf import any_pb2

from pyinjective.client.model.pagination import PaginationOption
from pyinjective.core.ibc.client.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.ibc.client.grpc.ibc_client_grpc_api import IBCClientGrpcApi
from pyinjective.core.network import DisabledCookieAssistant, Network
from pyinjective.proto.cosmos.base.query.v1beta1 import pagination_pb2 as pagination_pb
from pyinjective.proto.cosmos.ics23.v1 import proofs_pb2 as ics23_proofs
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from collections import deque

from pyinjective.proto.ibc.core.connection.v1 import (
query_pb2 as ibc_connection_query,
query_pb2_grpc as ibc_connection_query_grpc,
)


class ConfigurableIBCConnectionQueryServicer(ibc_connection_query_grpc.QueryServicer):
def __init__(self):
super().__init__()
self.connection_responses = deque()
self.connections_responses = deque()
self.client_connections_responses = deque()
self.connection_client_state_responses = deque()
self.connection_consensus_state_responses = deque()
self.connection_params_responses = deque()

async def Connection(self, request: ibc_connection_query.QueryConnectionRequest, context=None, metadata=None):
return self.connection_responses.pop()

async def Connections(self, request: ibc_connection_query.QueryConnectionsRequest, context=None, metadata=None):
return self.connections_responses.pop()

async def ClientConnections(
self, request: ibc_connection_query.QueryClientConnectionsRequest, context=None, metadata=None
):
return self.client_connections_responses.pop()

async def ConnectionClientState(
self, request: ibc_connection_query.QueryConnectionClientStateRequest, context=None, metadata=None
):
return self.connection_client_state_responses.pop()

async def ConnectionConsensusState(
self, request: ibc_connection_query.QueryConnectionConsensusStateRequest, context=None, metadata=None
):
return self.connection_consensus_state_responses.pop()

async def ConnectionParams(
self, request: ibc_connection_query.QueryConnectionParamsRequest, context=None, metadata=None
):
return self.connection_params_responses.pop()
Comment on lines +1 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust handling of empty deques in servicer methods.

The methods in ConfigurableIBCConnectionQueryServicer pop items from deques without checking if they are empty. This could lead to IndexError if a deque is empty. Consider adding checks or default values to handle these cases safely.

Loading
Loading