diff --git a/examples/chain_client/38_StreamEvent.py b/examples/chain_client/38_StreamEventOrderFail.py similarity index 65% rename from examples/chain_client/38_StreamEvent.py rename to examples/chain_client/38_StreamEventOrderFail.py index 10e8f1cd..30265366 100644 --- a/examples/chain_client/38_StreamEvent.py +++ b/examples/chain_client/38_StreamEventOrderFail.py @@ -13,22 +13,17 @@ # limitations under the License. """Injective Exchange API client for Python. Example only.""" -import sys -sys.path.insert(0, '/Users/nam/desktop/injective/sdk-python/') - import asyncio import logging import json import websockets +import base64 -from pyinjective.async_client import AsyncClient from pyinjective.constant import Network async def main() -> None: network = Network.mainnet() - client = AsyncClient(network, insecure=False) - - event_filter = "tm.event='Tx' AND message.sender='inj1rwv4zn3jptsqs7l8lpa3uvzhs57y8duemete9e' AND message.action='/injective.exchange.v1beta1.MsgBatchUpdateOrders'" + event_filter = "tm.event='Tx' AND message.sender='inj1rwv4zn3jptsqs7l8lpa3uvzhs57y8duemete9e' AND message.action='/injective.exchange.v1beta1.MsgBatchUpdateOrders' AND injective.exchange.v1beta1.EventOrderFail.flags EXISTS" query = json.dumps({ "jsonrpc": "2.0", "method": "subscribe", @@ -41,9 +36,21 @@ async def main() -> None: async with websockets.connect(network.tm_websocket_endpoint) as ws: await ws.send(query) while True: - events = await ws.recv() - print(events) - await asyncio.sleep(1) + res = await ws.recv() + res = json.loads(res) + result = res["result"] + if result == {}: + continue + + failed_order_hashes = result["events"]["injective.exchange.v1beta1.EventOrderFail.hashes"] + failed_order_codes = json.loads(result["events"]["injective.exchange.v1beta1.EventOrderFail.flags"][0]) + + dict = {} + for i, order_hash in enumerate(failed_order_hashes): + hash = "0x" + base64.b64decode(order_hash).hex() + dict[hash] = failed_order_codes[i] + + print(dict) if __name__ == '__main__': logging.basicConfig(level=logging.INFO)