Skip to content

Commit 675d71e

Browse files
Merge pull request #145 from InjectiveLabs/f/add_event_order_fail
feat: add stream event order fail
2 parents 717cead + 175d23f commit 675d71e

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

examples/chain_client/38_StreamEvent.py renamed to examples/chain_client/38_StreamEventOrderFail.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,17 @@
1313
# limitations under the License.
1414
"""Injective Exchange API client for Python. Example only."""
1515

16-
import sys
17-
sys.path.insert(0, '/Users/nam/desktop/injective/sdk-python/')
18-
1916
import asyncio
2017
import logging
2118
import json
2219
import websockets
20+
import base64
2321

24-
from pyinjective.async_client import AsyncClient
2522
from pyinjective.constant import Network
2623

2724
async def main() -> None:
2825
network = Network.mainnet()
29-
client = AsyncClient(network, insecure=False)
30-
31-
event_filter = "tm.event='Tx' AND message.sender='inj1rwv4zn3jptsqs7l8lpa3uvzhs57y8duemete9e' AND message.action='/injective.exchange.v1beta1.MsgBatchUpdateOrders'"
26+
event_filter = "tm.event='Tx' AND message.sender='inj1rwv4zn3jptsqs7l8lpa3uvzhs57y8duemete9e' AND message.action='/injective.exchange.v1beta1.MsgBatchUpdateOrders' AND injective.exchange.v1beta1.EventOrderFail.flags EXISTS"
3227
query = json.dumps({
3328
"jsonrpc": "2.0",
3429
"method": "subscribe",
@@ -41,9 +36,21 @@ async def main() -> None:
4136
async with websockets.connect(network.tm_websocket_endpoint) as ws:
4237
await ws.send(query)
4338
while True:
44-
events = await ws.recv()
45-
print(events)
46-
await asyncio.sleep(1)
39+
res = await ws.recv()
40+
res = json.loads(res)
41+
result = res["result"]
42+
if result == {}:
43+
continue
44+
45+
failed_order_hashes = result["events"]["injective.exchange.v1beta1.EventOrderFail.hashes"]
46+
failed_order_codes = json.loads(result["events"]["injective.exchange.v1beta1.EventOrderFail.flags"][0])
47+
48+
dict = {}
49+
for i, order_hash in enumerate(failed_order_hashes):
50+
hash = "0x" + base64.b64decode(order_hash).hex()
51+
dict[hash] = failed_order_codes[i]
52+
53+
print(dict)
4754

4855
if __name__ == '__main__':
4956
logging.basicConfig(level=logging.INFO)

0 commit comments

Comments
 (0)