Skip to content

Commit

Permalink
(feat) Added support for all IBC Core Client queries. Included exampl…
Browse files Browse the repository at this point in the history
…e scripts for all queries and unit tests
  • Loading branch information
abel committed Apr 22, 2024
1 parent d8192bb commit 3f777fb
Show file tree
Hide file tree
Showing 20 changed files with 1,035 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ All notable changes to this project will be documented in this file.

## [1.6.0] - 9999-99-99
### Added
- Added support for all queries in the chain 'tendermint' module
- Added support for all queries in the IBC Transfer module
- Support for all queries in the chain 'tendermint' module
- 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

### Changed
- Refactored cookies management logic to use all gRPC calls' responses to update the current cookies
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions examples/chain_client/ibc/client/query/1_ClientState.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"

state = await client.fetch_ibc_client_state(client_id=client_id)
print(state)


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/client/query/2_ClientStates.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)

states = await client.fetch_ibc_client_states(pagination=pagination)
print(states)


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/client/query/3_ConsensusState.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)

client_id = "07-tendermint-0"
revision_number = 0
revision_height = 7379538

state = await client.fetch_ibc_consensus_state(
client_id=client_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())
23 changes: 23 additions & 0 deletions examples/chain_client/ibc/client/query/4_ConsensusStates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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)

client_id = "07-tendermint-0"
pagination = PaginationOption(skip=2, limit=4)

states = await client.fetch_ibc_consensus_states(client_id=client_id, pagination=pagination)
print(states)


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

client_id = "07-tendermint-0"
pagination = PaginationOption(skip=2, limit=4)

states = await client.fetch_ibc_consensus_state_heights(client_id=client_id, pagination=pagination)
print(states)


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/client/query/6_ClientStatus.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"

state = await client.fetch_ibc_client_status(client_id=client_id)
print(state)


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

params = await client.fetch_ibc_client_params()
print(params)


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

state = await client.fetch_ibc_upgraded_client_state()
print(state)


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

state = await client.fetch_ibc_upgraded_consensus_state()
print(state)


if __name__ == "__main__":
symbol_db = symbol_database.Default()
asyncio.get_event_loop().run_until_complete(main())
Empty file.
54 changes: 54 additions & 0 deletions pyinjective/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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.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 @@ -184,6 +185,10 @@ def __init__(
channel=self.chain_channel,
cookie_assistant=network.chain_cookie_assistant,
)
self.ibc_client_api = IBCClientGrpcApi(
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 @@ -3119,6 +3124,55 @@ async def fetch_next_sequence_receive(self, port_id: str, channel_id: str) -> Di

# endregion

# region IBC Client module
async def fetch_ibc_client_state(self, client_id: str) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_client_state(client_id=client_id)

async def fetch_ibc_client_states(self, pagination: Optional[PaginationOption] = None) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_client_states(pagination=pagination)

async def fetch_ibc_consensus_state(
self,
client_id: str,
revision_number: int,
revision_height: int,
latest_height: Optional[bool] = None,
) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_consensus_state(
client_id=client_id,
revision_number=revision_number,
revision_height=revision_height,
latest_height=latest_height,
)

async def fetch_ibc_consensus_states(
self,
client_id: str,
pagination: Optional[PaginationOption] = None,
) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_consensus_states(client_id=client_id, pagination=pagination)

async def fetch_ibc_consensus_state_heights(
self,
client_id: str,
pagination: Optional[PaginationOption] = None,
) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_consensus_state_heights(client_id=client_id, pagination=pagination)

async def fetch_ibc_client_status(self, client_id: str) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_client_status(client_id=client_id)

async def fetch_ibc_client_params(self) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_client_params()

async def fetch_ibc_upgraded_client_state(self) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_upgraded_client_state()

async def fetch_ibc_upgraded_consensus_state(self) -> Dict[str, Any]:
return await self.ibc_client_api.fetch_upgraded_consensus_state()

# endregion

async def composer(self):
return Composer(
network=self.network.string(),
Expand Down
2 changes: 1 addition & 1 deletion pyinjective/core/ibc/channel/grpc/ibc_channel_grpc_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Callable, Dict, List, Optional

from grpc._cython.cygrpc import Channel
from grpc import Channel

from pyinjective.client.model.pagination import PaginationOption
from pyinjective.core.network import CookieAssistant
Expand Down
Empty file.
96 changes: 96 additions & 0 deletions pyinjective/core/ibc/client/ibc_client_grpc_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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.client.v1 import query_pb2 as ibc_client_query, query_pb2_grpc as ibc_client_query_grpc
from pyinjective.utils.grpc_api_request_assistant import GrpcApiRequestAssistant


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

async def fetch_client_state(self, client_id: str) -> Dict[str, Any]:
request = ibc_client_query.QueryClientStateRequest(client_id=client_id)
response = await self._execute_call(call=self._stub.ClientState, request=request)

return response

async def fetch_client_states(self, pagination: Optional[PaginationOption] = None) -> Dict[str, Any]:
if pagination is None:
pagination = PaginationOption()
request = ibc_client_query.QueryClientStatesRequest(pagination=pagination.create_pagination_request())
response = await self._execute_call(call=self._stub.ClientStates, request=request)

return response

async def fetch_consensus_state(
self,
client_id: str,
revision_number: int,
revision_height: int,
latest_height: Optional[bool] = None,
) -> Dict[str, Any]:
request = ibc_client_query.QueryConsensusStateRequest(
client_id=client_id,
revision_number=revision_number,
revision_height=revision_height,
latest_height=latest_height,
)
response = await self._execute_call(call=self._stub.ConsensusState, request=request)

return response

async def fetch_consensus_states(
self, client_id: str, pagination: Optional[PaginationOption] = None
) -> Dict[str, Any]:
if pagination is None:
pagination = PaginationOption()
request = ibc_client_query.QueryConsensusStatesRequest(
client_id=client_id, pagination=pagination.create_pagination_request()
)
response = await self._execute_call(call=self._stub.ConsensusStates, request=request)

return response

async def fetch_consensus_state_heights(
self, client_id: str, pagination: Optional[PaginationOption] = None
) -> Dict[str, Any]:
if pagination is None:
pagination = PaginationOption()
request = ibc_client_query.QueryConsensusStateHeightsRequest(
client_id=client_id, pagination=pagination.create_pagination_request()
)
response = await self._execute_call(call=self._stub.ConsensusStateHeights, request=request)

return response

async def fetch_client_status(self, client_id: str) -> Dict[str, Any]:
request = ibc_client_query.QueryClientStatusRequest(client_id=client_id)
response = await self._execute_call(call=self._stub.ClientStatus, request=request)

return response

async def fetch_client_params(self) -> Dict[str, Any]:
request = ibc_client_query.QueryClientParamsRequest()
response = await self._execute_call(call=self._stub.ClientParams, request=request)

return response

async def fetch_upgraded_client_state(self) -> Dict[str, Any]:
request = ibc_client_query.QueryUpgradedClientStateRequest()
response = await self._execute_call(call=self._stub.UpgradedClientState, request=request)

return response

async def fetch_upgraded_consensus_state(self) -> Dict[str, Any]:
request = ibc_client_query.QueryUpgradedConsensusStateRequest()
response = await self._execute_call(call=self._stub.UpgradedConsensusState, 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)
Empty file.
Empty file.
Loading

0 comments on commit 3f777fb

Please sign in to comment.