From 0fff421b670677445873af7dc3f258b4d58899b3 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:13:18 +0800 Subject: [PATCH] add parse tx to examples --- examples/subscribe_event.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/examples/subscribe_event.py b/examples/subscribe_event.py index 179fb31..e5578a8 100644 --- a/examples/subscribe_event.py +++ b/examples/subscribe_event.py @@ -5,20 +5,14 @@ import yaml from fxsdk.client.websockets import WebsocketRpcClient, ReconnectingRpcWebsocket -from fxsdk.msg import event +from fxsdk.msg import event, new_tx_from_base64 +from fxsdk.x.cosmos.bank.v1beta1.tx_pb2 import MsgSend class BlockchainEvent(WebsocketRpcClient): @classmethod async def create(cls, endpoint_url: str, loop, callback=None): - """Create a WebsocketManagerBase instance - - :param endpoint_url: node endpoint url - :param loop: asyncio loop - :param callback: async callback function to receive messages - :return: - """ self = BlockchainEvent() loop = loop if loop else asyncio.get_event_loop() callback = callback if callback else self.receive @@ -63,7 +57,11 @@ def process_new_block(self, data: Dict): block = data.get('block', {}) height = block.get('header', {}).get('height', 0) block_time = block.get('header', {}).get('time', '') - txs = block.get('data', {}).get('txs', []) + base64_txs = block.get('data', {}).get('txs', []) + txs = [] + for base64_tx in base64_txs: + tx = new_tx_from_base64(base64_tx) + txs.append(tx) events = data.get('result_finalize_block', {}).get('events', []) self._log.info(f"new block: height={height}, block_time={block_time}, txs={len(txs)}, events={len(events)}") @@ -72,9 +70,15 @@ async def subscribe_tx(self): def process_tx(self, data: Dict): height = data.get('TxResult', {}).get('height', 0) - base64_tx = data.get('TxResult', {}).get('tx', '') events = data.get('TxResult', {}).get('result', {}).get('events') - self._log.info(f"new tx: height={height}, tx={base64_tx}, events={events}") + base64_tx = data.get('TxResult', {}).get('tx', '') + tx = new_tx_from_base64(base64_tx) + for msg in tx.body.messages: + if msg.type_url == '/cosmos.bank.v1beta1.MsgSend': + msg_send = MsgSend() + msg_send.ParseFromString(msg.value) + self._log.info(f"new tx: height={height}, tx={msg_send}, events={events}") + break global_loop = None