Skip to content

Commit

Permalink
add parse tx to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Oct 16, 2023
1 parent cc1807f commit 0fff421
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions examples/subscribe_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}")

Expand All @@ -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
Expand Down

0 comments on commit 0fff421

Please sign in to comment.