Skip to content

Commit 25adca0

Browse files
author
abel
committed
(feat) Added low level API components for the exchange explorer module, with unit tests. Included new functions in AsyncClient to use the low level API components and marked the old functions as deprecated. Updated the examples to use the new AsyncClient functions.
1 parent cfb507e commit 25adca0

File tree

14 files changed

+2390
-74
lines changed

14 files changed

+2390
-74
lines changed

examples/exchange_client/explorer_rpc/10_GetIBCTransfers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from pyinjective.async_client import AsyncClient
4+
from pyinjective.client.model.pagination import PaginationOption
45
from pyinjective.core.network import Network
56

67

@@ -16,15 +17,15 @@ async def main() -> None:
1617
dest_port = "transfer"
1718
limit = 1
1819
skip = 10
19-
ibc_transfers = await client.get_ibc_transfers(
20+
pagination = PaginationOption(limit=limit, skip=skip)
21+
ibc_transfers = await client.fetch_ibc_transfer_txs(
2022
sender=sender,
2123
receiver=receiver,
2224
src_channel=src_channel,
2325
src_port=src_port,
24-
destination_channel=destination_channel,
26+
dest_channel=destination_channel,
2527
dest_port=dest_port,
26-
limit=limit,
27-
skip=skip,
28+
pagination=pagination,
2829
)
2930
print(ibc_transfers)
3031

examples/exchange_client/explorer_rpc/1_GetTxByHash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ async def main() -> None:
1111
client = AsyncClient(network)
1212
composer = Composer(network=network.string())
1313
tx_hash = "0F3EBEC1882E1EEAC5B7BDD836E976250F1CD072B79485877CEACCB92ACDDF52"
14-
transaction_response = await client.get_tx_by_hash(tx_hash=tx_hash)
14+
transaction_response = await client.fetch_tx_by_tx_hash(tx_hash=tx_hash)
1515
print(transaction_response)
1616

17-
transaction_messages = composer.UnpackTransactionMessages(transaction=transaction_response.data)
17+
transaction_messages = composer.unpack_transaction_messages(transaction_data=transaction_response["data"])
1818
print(transaction_messages)
1919
first_message = transaction_messages[0]
2020
print(first_message)

examples/exchange_client/explorer_rpc/2_AccountTxs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from pyinjective.async_client import AsyncClient
4+
from pyinjective.client.model.pagination import PaginationOption
45
from pyinjective.composer import Composer
56
from pyinjective.core.network import Network
67

@@ -13,9 +14,14 @@ async def main() -> None:
1314
address = "inj1phd706jqzd9wznkk5hgsfkrc8jqxv0kmlj0kex"
1415
message_type = "cosmos.bank.v1beta1.MsgSend"
1516
limit = 2
16-
transactions_response = await client.get_account_txs(address=address, type=message_type, limit=limit)
17+
pagination = PaginationOption(limit=limit)
18+
transactions_response = await client.fetch_account_txs(
19+
address=address,
20+
message_type=message_type,
21+
pagination=pagination,
22+
)
1723
print(transactions_response)
18-
first_transaction_messages = composer.UnpackTransactionMessages(transaction=transactions_response.data[0])
24+
first_transaction_messages = composer.unpack_transaction_messages(transaction_data=transactions_response["data"][0])
1925
print(first_transaction_messages)
2026
first_message = first_transaction_messages[0]
2127
print(first_message)

examples/exchange_client/explorer_rpc/3_Blocks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from pyinjective.async_client import AsyncClient
4+
from pyinjective.client.model.pagination import PaginationOption
45
from pyinjective.core.network import Network
56

67

@@ -9,8 +10,9 @@ async def main() -> None:
910
network = Network.testnet()
1011
client = AsyncClient(network)
1112
limit = 2
12-
block = await client.get_blocks(limit=limit)
13-
print(block)
13+
pagination = PaginationOption(limit=limit)
14+
blocks = await client.fetch_blocks(pagination=pagination)
15+
print(blocks)
1416

1517

1618
if __name__ == "__main__":

examples/exchange_client/explorer_rpc/4_Block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def main() -> None:
99
network = Network.testnet()
1010
client = AsyncClient(network)
1111
block_height = "5825046"
12-
block = await client.get_block(block_height=block_height)
12+
block = await client.fetch_block(block_id=block_height)
1313
print(block)
1414

1515

examples/exchange_client/explorer_rpc/5_TxsRequest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from pyinjective.async_client import AsyncClient
4+
from pyinjective.client.model.pagination import PaginationOption
45
from pyinjective.core.network import Network
56

67

@@ -9,7 +10,8 @@ async def main() -> None:
910
network = Network.testnet()
1011
client = AsyncClient(network)
1112
limit = 2
12-
txs = await client.get_txs(limit=limit)
13+
pagination = PaginationOption(limit=limit)
14+
txs = await client.fetch_txs(pagination=pagination)
1315
print(txs)
1416

1517

examples/exchange_client/explorer_rpc/8_GetPeggyDeposits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def main() -> None:
99
network = Network.testnet()
1010
client = AsyncClient(network)
1111
receiver = "inj1phd706jqzd9wznkk5hgsfkrc8jqxv0kmlj0kex"
12-
peggy_deposits = await client.get_peggy_deposits(receiver=receiver)
12+
peggy_deposits = await client.fetch_peggy_deposit_txs(receiver=receiver)
1313
print(peggy_deposits)
1414

1515

examples/exchange_client/explorer_rpc/9_GetPeggyWithdrawals.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from pyinjective.async_client import AsyncClient
4+
from pyinjective.client.model.pagination import PaginationOption
45
from pyinjective.core.network import Network
56

67

@@ -10,7 +11,8 @@ async def main() -> None:
1011
client = AsyncClient(network)
1112
sender = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
1213
limit = 2
13-
peggy_deposits = await client.get_peggy_withdrawals(sender=sender, limit=limit)
14+
pagination = PaginationOption(limit=limit)
15+
peggy_deposits = await client.fetch_peggy_withdrawal_txs(sender=sender, pagination=pagination)
1416
print(peggy_deposits)
1517

1618

pyinjective/async_client.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pyinjective.client.indexer.grpc.indexer_grpc_account_api import IndexerGrpcAccountApi
1616
from pyinjective.client.indexer.grpc.indexer_grpc_auction_api import IndexerGrpcAuctionApi
1717
from pyinjective.client.indexer.grpc.indexer_grpc_derivative_api import IndexerGrpcDerivativeApi
18+
from pyinjective.client.indexer.grpc.indexer_grpc_explorer_api import IndexerGrpcExplorerApi
1819
from pyinjective.client.indexer.grpc.indexer_grpc_insurance_api import IndexerGrpcInsuranceApi
1920
from pyinjective.client.indexer.grpc.indexer_grpc_meta_api import IndexerGrpcMetaApi
2021
from pyinjective.client.indexer.grpc.indexer_grpc_oracle_api import IndexerGrpcOracleApi
@@ -271,6 +272,13 @@ def __init__(
271272
),
272273
)
273274

275+
self.exchange_explorer_api = IndexerGrpcExplorerApi(
276+
channel=self.explorer_channel,
277+
metadata_provider=lambda: self.network.exchange_metadata(
278+
metadata_query_provider=self._explorer_cookie_metadata_requestor
279+
),
280+
)
281+
274282
async def all_tokens(self) -> Dict[str, Token]:
275283
if self._tokens is None:
276284
async with self._tokens_and_markets_initialization_lock:
@@ -601,10 +609,22 @@ async def listen_keepalive(
601609
# Explorer RPC
602610

603611
async def get_tx_by_hash(self, tx_hash: str):
612+
"""
613+
This method is deprecated and will be removed soon. Please use `fetch_tx_by_tx_hash` instead
614+
"""
615+
warn("This method is deprecated. Use fetch_tx_by_tx_hash instead", DeprecationWarning, stacklevel=2)
616+
604617
req = explorer_rpc_pb.GetTxByTxHashRequest(hash=tx_hash)
605618
return await self.stubExplorer.GetTxByTxHash(req)
606619

620+
async def fetch_tx_by_tx_hash(self, tx_hash: str) -> Dict[str, Any]:
621+
return await self.exchange_explorer_api.fetch_tx_by_tx_hash(tx_hash=tx_hash)
622+
607623
async def get_account_txs(self, address: str, **kwargs):
624+
"""
625+
This method is deprecated and will be removed soon. Please use `fetch_account_txs` instead
626+
"""
627+
warn("This method is deprecated. Use fetch_account_txs instead", DeprecationWarning, stacklevel=2)
608628
req = explorer_rpc_pb.GetAccountTxsRequest(
609629
address=address,
610630
before=kwargs.get("before"),
@@ -616,19 +636,66 @@ async def get_account_txs(self, address: str, **kwargs):
616636
)
617637
return await self.stubExplorer.GetAccountTxs(req)
618638

639+
async def fetch_account_txs(
640+
self,
641+
address: str,
642+
before: Optional[int] = None,
643+
after: Optional[int] = None,
644+
message_type: Optional[str] = None,
645+
module: Optional[str] = None,
646+
from_number: Optional[int] = None,
647+
to_number: Optional[int] = None,
648+
status: Optional[str] = None,
649+
pagination: Optional[PaginationOption] = None,
650+
) -> Dict[str, Any]:
651+
return await self.exchange_explorer_api.fetch_account_txs(
652+
address=address,
653+
before=before,
654+
after=after,
655+
message_type=message_type,
656+
module=module,
657+
from_number=from_number,
658+
to_number=to_number,
659+
status=status,
660+
pagination=pagination,
661+
)
662+
619663
async def get_blocks(self, **kwargs):
664+
"""
665+
This method is deprecated and will be removed soon. Please use `fetch_blocks` instead
666+
"""
667+
warn("This method is deprecated. Use fetch_blocks instead", DeprecationWarning, stacklevel=2)
620668
req = explorer_rpc_pb.GetBlocksRequest(
621669
before=kwargs.get("before"),
622670
after=kwargs.get("after"),
623671
limit=kwargs.get("limit"),
624672
)
625673
return await self.stubExplorer.GetBlocks(req)
626674

675+
async def fetch_blocks(
676+
self,
677+
before: Optional[int] = None,
678+
after: Optional[int] = None,
679+
pagination: Optional[PaginationOption] = None,
680+
) -> Dict[str, Any]:
681+
return await self.exchange_explorer_api.fetch_blocks(before=before, after=after, pagination=pagination)
682+
627683
async def get_block(self, block_height: str):
684+
"""
685+
This method is deprecated and will be removed soon. Please use `fetch_block` instead
686+
"""
687+
warn("This method is deprecated. Use fetch_block instead", DeprecationWarning, stacklevel=2)
628688
req = explorer_rpc_pb.GetBlockRequest(id=block_height)
629689
return await self.stubExplorer.GetBlock(req)
630690

691+
async def fetch_block(self, block_id: str) -> Dict[str, Any]:
692+
return await self.exchange_explorer_api.fetch_block(block_id=block_id)
693+
631694
async def get_txs(self, **kwargs):
695+
"""
696+
This method is deprecated and will be removed soon. Please use `fetch_txs` instead
697+
"""
698+
warn("This method is deprecated. Use fetch_txs instead", DeprecationWarning, stacklevel=2)
632699
req = explorer_rpc_pb.GetTxsRequest(
633700
before=kwargs.get("before"),
634701
after=kwargs.get("after"),
@@ -639,6 +706,28 @@ async def get_txs(self, **kwargs):
639706
)
640707
return await self.stubExplorer.GetTxs(req)
641708

709+
async def fetch_txs(
710+
self,
711+
before: Optional[int] = None,
712+
after: Optional[int] = None,
713+
message_type: Optional[str] = None,
714+
module: Optional[str] = None,
715+
from_number: Optional[int] = None,
716+
to_number: Optional[int] = None,
717+
status: Optional[str] = None,
718+
pagination: Optional[PaginationOption] = None,
719+
) -> Dict[str, Any]:
720+
return await self.exchange_explorer_api.fetch_txs(
721+
before=before,
722+
after=after,
723+
message_type=message_type,
724+
module=module,
725+
from_number=from_number,
726+
to_number=to_number,
727+
status=status,
728+
pagination=pagination,
729+
)
730+
642731
async def stream_txs(self):
643732
req = explorer_rpc_pb.StreamTxsRequest()
644733
return self.stubExplorer.StreamTxs(req)
@@ -648,6 +737,10 @@ async def stream_blocks(self):
648737
return self.stubExplorer.StreamBlocks(req)
649738

650739
async def get_peggy_deposits(self, **kwargs):
740+
"""
741+
This method is deprecated and will be removed soon. Please use `fetch_peggy_deposit_txs` instead
742+
"""
743+
warn("This method is deprecated. Use fetch_peggy_deposit_txs instead", DeprecationWarning, stacklevel=2)
651744
req = explorer_rpc_pb.GetPeggyDepositTxsRequest(
652745
sender=kwargs.get("sender"),
653746
receiver=kwargs.get("receiver"),
@@ -656,7 +749,23 @@ async def get_peggy_deposits(self, **kwargs):
656749
)
657750
return await self.stubExplorer.GetPeggyDepositTxs(req)
658751

752+
async def fetch_peggy_deposit_txs(
753+
self,
754+
sender: Optional[str] = None,
755+
receiver: Optional[str] = None,
756+
pagination: Optional[PaginationOption] = None,
757+
) -> Dict[str, Any]:
758+
return await self.exchange_explorer_api.fetch_peggy_deposit_txs(
759+
sender=sender,
760+
receiver=receiver,
761+
pagination=pagination,
762+
)
763+
659764
async def get_peggy_withdrawals(self, **kwargs):
765+
"""
766+
This method is deprecated and will be removed soon. Please use `fetch_peggy_withdrawal_txs` instead
767+
"""
768+
warn("This method is deprecated. Use fetch_peggy_withdrawal_txs instead", DeprecationWarning, stacklevel=2)
660769
req = explorer_rpc_pb.GetPeggyWithdrawalTxsRequest(
661770
sender=kwargs.get("sender"),
662771
receiver=kwargs.get("receiver"),
@@ -665,7 +774,23 @@ async def get_peggy_withdrawals(self, **kwargs):
665774
)
666775
return await self.stubExplorer.GetPeggyWithdrawalTxs(req)
667776

777+
async def fetch_peggy_withdrawal_txs(
778+
self,
779+
sender: Optional[str] = None,
780+
receiver: Optional[str] = None,
781+
pagination: Optional[PaginationOption] = None,
782+
) -> Dict[str, Any]:
783+
return await self.exchange_explorer_api.fetch_peggy_withdrawal_txs(
784+
sender=sender,
785+
receiver=receiver,
786+
pagination=pagination,
787+
)
788+
668789
async def get_ibc_transfers(self, **kwargs):
790+
"""
791+
This method is deprecated and will be removed soon. Please use `fetch_ibc_transfer_txs` instead
792+
"""
793+
warn("This method is deprecated. Use fetch_ibc_transfer_txs instead", DeprecationWarning, stacklevel=2)
669794
req = explorer_rpc_pb.GetIBCTransferTxsRequest(
670795
sender=kwargs.get("sender"),
671796
receiver=kwargs.get("receiver"),
@@ -678,6 +803,26 @@ async def get_ibc_transfers(self, **kwargs):
678803
)
679804
return await self.stubExplorer.GetIBCTransferTxs(req)
680805

806+
async def fetch_ibc_transfer_txs(
807+
self,
808+
sender: Optional[str] = None,
809+
receiver: Optional[str] = None,
810+
src_channel: Optional[str] = None,
811+
src_port: Optional[str] = None,
812+
dest_channel: Optional[str] = None,
813+
dest_port: Optional[str] = None,
814+
pagination: Optional[PaginationOption] = None,
815+
) -> Dict[str, Any]:
816+
return await self.exchange_explorer_api.fetch_ibc_transfer_txs(
817+
sender=sender,
818+
receiver=receiver,
819+
src_channel=src_channel,
820+
src_port=src_port,
821+
dest_channel=dest_channel,
822+
dest_port=dest_port,
823+
pagination=pagination,
824+
)
825+
681826
# AccountsRPC
682827

683828
async def stream_subaccount_balance(self, subaccount_id: str, **kwargs):
@@ -2390,6 +2535,10 @@ def _exchange_cookie_metadata_requestor(self) -> Coroutine:
23902535
request = exchange_meta_rpc_pb.VersionRequest()
23912536
return self.stubMeta.Version(request).initial_metadata()
23922537

2538+
def _explorer_cookie_metadata_requestor(self) -> Coroutine:
2539+
request = explorer_rpc_pb.GetBlocksRequest()
2540+
return self.stubExplorer.GetBlocks(request).initial_metadata()
2541+
23932542
def _initialize_timeout_height_sync_task(self):
23942543
self._cancel_timeout_height_sync_task()
23952544
self._timeout_height_sync_task = asyncio.get_event_loop().create_task(self._timeout_height_sync_process())

0 commit comments

Comments
 (0)