From 5f4a42f2360a24f4beac551d5655045c0e2e89d1 Mon Sep 17 00:00:00 2001 From: abel Date: Fri, 9 Jun 2023 17:59:29 -0400 Subject: [PATCH 1/2] (fix) Added logic in Composer class to parse the transaction response messages from the Explorer. The transaction response message structure changed in the last chain upgrade. The messages in the transaction are now included in the messages attribute instead of the data attribute --- Pipfile | 2 +- Pipfile.lock | 40 ++++------------- .../explorer_rpc/1_GetTxByHash.py | 11 ++++- .../explorer_rpc/2_AccountTxs.py | 12 +++-- pyinjective/composer.py | 45 ++++++++++++++++++- pyinjective/transaction.py | 2 +- 6 files changed, 72 insertions(+), 40 deletions(-) diff --git a/Pipfile b/Pipfile index cc9330d8..956c5a05 100644 --- a/Pipfile +++ b/Pipfile @@ -24,4 +24,4 @@ urllib3 = "<2" [dev-packages] [requires] -python_version = "3.8" +python_version = "3.9" diff --git a/Pipfile.lock b/Pipfile.lock index 411f834d..8f821441 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -158,28 +158,6 @@ "markers": "python_version >= '3.7'", "version": "==23.1.0" }, - "backports.zoneinfo": { - "hashes": [ - "sha256:17746bd546106fa389c51dbea67c8b7c8f0d14b5526a579ca6ccf5ed72c526cf", - "sha256:1b13e654a55cd45672cb54ed12148cd33628f672548f373963b0bff67b217328", - "sha256:1c5742112073a563c81f786e77514969acb58649bcdf6cdf0b4ed31a348d4546", - "sha256:4a0f800587060bf8880f954dbef70de6c11bbe59c673c3d818921f042f9954a6", - "sha256:5c144945a7752ca544b4b78c8c41544cdfaf9786f25fe5ffb10e838e19a27570", - "sha256:7b0a64cda4145548fed9efc10322770f929b944ce5cee6c0dfe0c87bf4c0c8c9", - "sha256:8439c030a11780786a2002261569bdf362264f605dfa4d65090b64b05c9f79a7", - "sha256:8961c0f32cd0336fb8e8ead11a1f8cd99ec07145ec2931122faaac1c8f7fd987", - "sha256:89a48c0d158a3cc3f654da4c2de1ceba85263fafb861b98b59040a5086259722", - "sha256:a76b38c52400b762e48131494ba26be363491ac4f9a04c1b7e92483d169f6582", - "sha256:da6013fd84a690242c310d77ddb8441a559e9cb3d3d59ebac9aca1a57b2e18bc", - "sha256:e55b384612d93be96506932a786bbcde5a2db7a9e6a4bb4bffe8b733f5b9036b", - "sha256:e81b76cace8eda1fca50e345242ba977f9be6ae3945af8d46326d776b4cf78d1", - "sha256:e8236383a20872c0cdf5a62b554b27538db7fa1bbec52429d8d106effbaeca08", - "sha256:f04e857b59d9d1ccc39ce2da1021d196e47234873820cbeaad210724b1ee28ac", - "sha256:fadbfe37f74051d024037f223b8e001611eac868b5c5b06144ef4d8b799862f2" - ], - "markers": "python_version < '3.9'", - "version": "==0.2.1" - }, "base58": { "hashes": [ "sha256:11a36f4d3ce51dfc1043f3218591ac4eb1ceb172919cebe05b52a5bcc8d245c2", @@ -539,27 +517,27 @@ }, "eth-hash": { "hashes": [ - "sha256:4d992e885f3ae3901abbe98bd776ba62d0f6335f98c6e9fc60a39b9d114dfb5a", - "sha256:9805075f653e114a31a99678e93b257fb4082337696f4eff7b4371fe65158409" + "sha256:1b5f10eca7765cc385e1430eefc5ced6e2e463bb18d1365510e2e539c1a6fe4e", + "sha256:251f62f6579a1e247561679d78df37548bd5f59908da0b159982bf8293ad32f0" ], "markers": "python_version >= '3.7' and python_version < '4'", - "version": "==0.5.1" + "version": "==0.5.2" }, "eth-typing": { "hashes": [ - "sha256:323111b3b76c8ceaff01619367aa52806f0264ca0ec1a70d4b9a42e44360f554", - "sha256:e9535e9d524d4c7a0cbd3d9832093cc5001a3e31869e72645674d24c6376d196" + "sha256:347d50713dd58ab50063b228d8271624ab2de3071bfa32d467b05f0ea31ab4c5", + "sha256:7f49610469811ee97ac43eaf6baa294778ce74042d41e61ecf22e5ebe385590f" ], "markers": "python_version < '4' and python_full_version >= '3.7.2'", - "version": "==3.3.0" + "version": "==3.4.0" }, "eth-utils": { "hashes": [ - "sha256:63901e54ec9e4ac16ae0a0d28e1dc48b968c20184d22f2727e5f3ca24b6250bc", - "sha256:fcb4c3c1b32947ba92970963f9aaf40da73b04ea1034964ff8c0e70595127138" + "sha256:4938ab742f91cdf19bae024261af090664f63ccf83bdb1213e7146c14209e899", + "sha256:7cccfb0b0749431d0d001e327e9a7289bf07308316a73850ae3895020e5682f4" ], "markers": "python_version >= '3.7' and python_version < '4'", - "version": "==2.1.0" + "version": "==2.1.1" }, "frozenlist": { "hashes": [ diff --git a/examples/exchange_client/explorer_rpc/1_GetTxByHash.py b/examples/exchange_client/explorer_rpc/1_GetTxByHash.py index 4222c82b..0a95f2ea 100644 --- a/examples/exchange_client/explorer_rpc/1_GetTxByHash.py +++ b/examples/exchange_client/explorer_rpc/1_GetTxByHash.py @@ -2,15 +2,22 @@ import logging from pyinjective.async_client import AsyncClient +from pyinjective.composer import Composer from pyinjective.constant import Network async def main() -> None: # select network: local, testnet, mainnet network = Network.testnet() client = AsyncClient(network, insecure=False) + composer = Composer(network=network.string()) tx_hash = "0F3EBEC1882E1EEAC5B7BDD836E976250F1CD072B79485877CEACCB92ACDDF52" - account = await client.get_tx_by_hash(tx_hash=tx_hash) - print(account) + transaction_response = await client.get_tx_by_hash(tx_hash=tx_hash) + print(transaction_response) + + transaction_messages = composer.UnpackTransactionMessages(transaction=transaction_response.data) + print(transaction_messages) + first_message = transaction_messages[0] + print(first_message) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) diff --git a/examples/exchange_client/explorer_rpc/2_AccountTxs.py b/examples/exchange_client/explorer_rpc/2_AccountTxs.py index 4f0027cf..29c6a513 100644 --- a/examples/exchange_client/explorer_rpc/2_AccountTxs.py +++ b/examples/exchange_client/explorer_rpc/2_AccountTxs.py @@ -2,17 +2,23 @@ import logging from pyinjective.async_client import AsyncClient +from pyinjective.composer import Composer from pyinjective.constant import Network async def main() -> None: # select network: local, testnet, mainnet network = Network.testnet() client = AsyncClient(network, insecure=False) + composer = Composer(network=network.string()) address = "inj1phd706jqzd9wznkk5hgsfkrc8jqxv0kmlj0kex" - type = "cosmos.bank.v1beta1.MsgSend" + message_type = "cosmos.bank.v1beta1.MsgSend" limit = 2 - account = await client.get_account_txs(address=address, type=type, limit=limit) - print(account) + transactions_response = await client.get_account_txs(address=address, type=message_type, limit=limit) + print(transactions_response) + first_transaction_messages = composer.UnpackTransactionMessages(transaction=transactions_response.data[0]) + print(first_transaction_messages) + first_message = first_transaction_messages[0] + print(first_message) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) diff --git a/pyinjective/composer.py b/pyinjective/composer.py index 3b6a3c6d..92858868 100644 --- a/pyinjective/composer.py +++ b/pyinjective/composer.py @@ -2,7 +2,7 @@ import json import logging -from google.protobuf import any_pb2, message, timestamp_pb2 +from google.protobuf import any_pb2, timestamp_pb2, json_format from .proto.cosmos.authz.v1beta1 import authz_pb2 as cosmos_authz_pb from .proto.cosmos.authz.v1beta1 import tx_pb2 as cosmos_authz_tx_pb @@ -13,7 +13,6 @@ from .proto.injective.exchange.v1beta1 import tx_pb2 as injective_exchange_tx_pb from pyinjective.proto.injective.exchange.v1beta1 import exchange_pb2 as injective_dot_exchange_dot_v1beta1_dot_exchange__pb2 -from .proto.injective.types.v1beta1 import tx_response_pb2 as tx_response_pb from .proto.injective.auction.v1beta1 import tx_pb2 as injective_auction_tx_pb @@ -969,3 +968,45 @@ def UnpackMsgExecResponse(msg_type, data): responses = [header_map[msg_type].FromString(result) for result in data.results] return responses + + @staticmethod + def UnpackTransactionMessages(transaction): + meta_messages = json.loads(transaction.messages.decode()) + + header_map = { + "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrderResponse, + "/injective.exchange.v1beta1.MsgCreateSpotMarketOrder": injective_exchange_tx_pb.MsgCreateSpotMarketOrderResponse, + "/injective.exchange.v1beta1.MsgCreateDerivativeLimitOrder": injective_exchange_tx_pb.MsgCreateDerivativeLimitOrderResponse, + "/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder": injective_exchange_tx_pb.MsgCreateDerivativeMarketOrderResponse, + "/injective.exchange.v1beta1.MsgCancelSpotOrder": injective_exchange_tx_pb.MsgCancelSpotOrderResponse, + "/injective.exchange.v1beta1.MsgCancelDerivativeOrder": injective_exchange_tx_pb.MsgCancelDerivativeOrderResponse, + "/injective.exchange.v1beta1.MsgBatchCancelSpotOrders": injective_exchange_tx_pb.MsgBatchCancelSpotOrdersResponse, + "/injective.exchange.v1beta1.MsgBatchCancelDerivativeOrders": injective_exchange_tx_pb.MsgBatchCancelDerivativeOrdersResponse, + "/injective.exchange.v1beta1.MsgBatchCreateSpotLimitOrders": injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrders, + "/injective.exchange.v1beta1.MsgBatchCreateDerivativeLimitOrders": injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrders, + "/injective.exchange.v1beta1.MsgBatchUpdateOrders": injective_exchange_tx_pb.MsgBatchUpdateOrders, + "/injective.exchange.v1beta1.MsgDeposit": injective_exchange_tx_pb.MsgDeposit, + "/injective.exchange.v1beta1.MsgWithdraw": injective_exchange_tx_pb.MsgWithdraw, + "/injective.exchange.v1beta1.MsgSubaccountTransfer": injective_exchange_tx_pb.MsgSubaccountTransfer, + "/injective.exchange.v1beta1.MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePosition, + "/injective.exchange.v1beta1.MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMargin, + "/injective.auction.v1beta1.MsgBid": injective_auction_tx_pb.MsgBid, + "/injective.exchange.v1beta1.MsgCreateBinaryOptionsLimitOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsLimitOrder, + "/injective.exchange.v1beta1.MsgCreateBinaryOptionsMarketOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsMarketOrder, + "/injective.exchange.v1beta1.MsgCancelBinaryOptionsOrder": injective_exchange_tx_pb.MsgCancelBinaryOptionsOrder, + "/injective.exchange.v1beta1.MsgAdminUpdateBinaryOptionsMarket": injective_exchange_tx_pb.MsgAdminUpdateBinaryOptionsMarket, + "/injective.exchange.v1beta1.MsgInstantBinaryOptionsMarketLaunch": injective_exchange_tx_pb.MsgInstantBinaryOptionsMarketLaunch, + "/cosmos.bank.v1beta1.MsgSend": cosmos_bank_tx_pb.MsgSend, + "/cosmos.authz.v1beta1.MsgGrant": cosmos_authz_tx_pb.MsgGrant, + "/cosmos.authz.v1beta1.MsgExec": cosmos_authz_tx_pb.MsgExec, + "/cosmos.authz.v1beta1.MsgRevoke": cosmos_authz_tx_pb.MsgRevoke, + "/injective.oracle.v1beta1.MsgRelayPriceFeedPrice": injective_oracle_tx_pb.MsgRelayPriceFeedPrice, + "/injective.oracle.v1beta1.MsgRelayProviderPrices": injective_oracle_tx_pb.MsgRelayProviderPrices, + } + + msgs = [] + for msg in meta_messages: + msg_as_string_dict = json.dumps(msg["value"]) + msgs.append(json_format.Parse(msg_as_string_dict, header_map[msg["type"]]())) + + return msgs diff --git a/pyinjective/transaction.py b/pyinjective/transaction.py index 0a83ec9e..7a02228f 100644 --- a/pyinjective/transaction.py +++ b/pyinjective/transaction.py @@ -46,7 +46,7 @@ def with_messages(self, *msgs: message.Message) -> "Transaction": def with_sender(self, client: Client, sender: str) -> "Transaction": if len(self.msgs) == 0: - raise EmptyMsgError("messsage is empty, please use with_messages at least 1 message") + raise EmptyMsgError("message is empty, please use with_messages at least 1 message") account = client.get_account(sender) if account: self.account_num = account.account_number From 81ca1ef8356ddd56cc789c565462844cb7bc2b29 Mon Sep 17 00:00:00 2001 From: abel Date: Fri, 9 Jun 2023 18:12:27 -0400 Subject: [PATCH 2/2] (fix) Updating README and the version number to prepare the PR to be released to master --- README.md | 3 +++ setup.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 93122e4d..27359f20 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,9 @@ Note that the [sync client](https://github.com/InjectiveLabs/sdk-python/blob/mas ### Changelogs +**0.6.3.3** +* Update the code to the new structure of transaction responses + **0.6.3.1** * Update the code to the new structure of transaction simulation responses diff --git a/setup.py b/setup.py index ce3c94df..2f589c3b 100755 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ EMAIL = "achilleas@injectivelabs.com" AUTHOR = "Injective Labs" REQUIRES_PYTHON = ">=3.7.0" -VERSION = "0.6.3.2" +VERSION = "0.6.3.3" REQUIRED = [ "protobuf",