Skip to content

Commit 79b39aa

Browse files
committed
Add method to unpack responses inside MsgExec response
1 parent d90af43 commit 79b39aa

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

examples/sync/chain_client/20_MsgExec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ async def main() -> None:
5858
print(sim_res)
5959
return
6060

61+
# We need to unpack 2 layers of response when using MsgExec
62+
# response bytes -> response msgs
63+
# exec msg response -> grantee msg response
64+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
65+
unpacked_msg_res = ProtoMsgComposer.UnpackMsgExecResponse(
66+
msg_type=msg0.__class__.__name__,
67+
data=sim_res_msg[0].grantee
68+
)
69+
print(unpacked_msg_res)
70+
6171
# build tx
6272
gas_price = 500000000
6373
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation

pyinjective/composer.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,10 @@ def MsgResponses(data, simulation=False):
457457
"/injective.exchange.v1beta1.MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePositionResponse,
458458
"/injective.exchange.v1beta1.MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMarginResponse,
459459
"/injective.auction.v1beta1.MsgBid": injective_auction_tx_pb.MsgBidResponse,
460-
"/cosmos.bank.v1beta1.MsgSend": cosmos_bank_tx_pb.MsgSendResponse
460+
"/cosmos.bank.v1beta1.MsgSend": cosmos_bank_tx_pb.MsgSendResponse,
461+
"/cosmos.authz.v1beta1.MsgGrant": cosmos_authz_tx_pb.MsgGrant,
462+
"/cosmos.authz.v1beta1.MsgExec": cosmos_authz_tx_pb.MsgExec,
463+
"/cosmos.authz.v1beta1.MsgRevoke": cosmos_authz_tx_pb.MsgRevoke
461464
}
462465

463466
response = tx_response_pb.TxResponseData.FromString(data)
@@ -466,3 +469,26 @@ def MsgResponses(data, simulation=False):
466469
msgs.append(header_map[msg.header].FromString(msg.data))
467470

468471
return msgs
472+
473+
def UnpackMsgExecResponse(msg_type, data):
474+
header_map = {
475+
"MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrderResponse,
476+
"MsgCreateSpotMarketOrder": injective_exchange_tx_pb.MsgCreateSpotMarketOrderResponse,
477+
"MsgCreateDerivativeLimitOrder": injective_exchange_tx_pb.MsgCreateDerivativeLimitOrderResponse,
478+
"MsgCreateDerivativeMarketOrder": injective_exchange_tx_pb.MsgCreateDerivativeMarketOrderResponse,
479+
"MsgCancelSpotOrder": injective_exchange_tx_pb.MsgCancelSpotOrderResponse,
480+
"MsgCancelDerivativeOrder": injective_exchange_tx_pb.MsgCancelDerivativeOrderResponse,
481+
"MsgBatchCancelSpotOrders": injective_exchange_tx_pb.MsgBatchCancelSpotOrdersResponse,
482+
"MsgBatchCancelDerivativeOrders": injective_exchange_tx_pb.MsgBatchCancelDerivativeOrdersResponse,
483+
"MsgBatchCreateSpotLimitOrders": injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrdersResponse,
484+
"MsgBatchCreateDerivativeLimitOrders": injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrdersResponse,
485+
"MsgBatchUpdateOrders": injective_exchange_tx_pb.MsgBatchUpdateOrdersResponse,
486+
"MsgDeposit": injective_exchange_tx_pb.MsgDepositResponse,
487+
"MsgWithdraw": injective_exchange_tx_pb.MsgWithdrawResponse,
488+
"MsgSubaccountTransfer": injective_exchange_tx_pb.MsgSubaccountTransferResponse,
489+
"MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePositionResponse,
490+
"MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMarginResponse,
491+
"MsgBid": injective_auction_tx_pb.MsgBidResponse,
492+
}
493+
494+
return header_map[msg_type].FromString(bytes(data, 'utf-8'))

0 commit comments

Comments
 (0)