Skip to content

Commit 6475e54

Browse files
committed
feat: add tm endpoint to network obj, add stream event example
1 parent 3a05191 commit 6475e54

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2021 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Injective Exchange API client for Python. Example only."""
15+
16+
import sys
17+
sys.path.insert(0, '/Users/nam/desktop/injective/sdk-python/')
18+
19+
import asyncio
20+
import logging
21+
import json
22+
import websockets
23+
24+
from pyinjective.async_client import AsyncClient
25+
from pyinjective.constant import Network
26+
27+
async def main() -> None:
28+
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'"
32+
query = json.dumps({
33+
"jsonrpc": "2.0",
34+
"method": "subscribe",
35+
"id": "0",
36+
"params": {
37+
"query": event_filter
38+
},
39+
})
40+
41+
async with websockets.connect(network.tm_websocket_endpoint) as ws:
42+
await ws.send(query)
43+
while True:
44+
events = await ws.recv()
45+
print(events)
46+
await asyncio.sleep(1)
47+
48+
if __name__ == '__main__':
49+
logging.basicConfig(level=logging.INFO)
50+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/constant.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ class Network:
6363
def __init__(
6464
self,
6565
lcd_endpoint: str ,
66+
tm_websocket_endpoint: str,
6667
grpc_endpoint: str ,
6768
grpc_exchange_endpoint: str ,
6869
chain_id: str ,
6970
fee_denom: str ,
7071
env: str
7172
):
7273
self.lcd_endpoint = lcd_endpoint
74+
self.tm_websocket_endpoint = tm_websocket_endpoint
7375
self.grpc_endpoint = grpc_endpoint
7476
self.grpc_exchange_endpoint = grpc_exchange_endpoint
7577
self.chain_id = chain_id
@@ -80,6 +82,7 @@ def __init__(
8082
def devnet(cls):
8183
return cls(
8284
lcd_endpoint='https://devnet.lcd.injective.dev',
85+
tm_websocket_endpoint='wss://devnet.tm.injective.dev/websocket',
8386
grpc_endpoint='devnet.injective.dev:9900',
8487
grpc_exchange_endpoint='devnet.injective.dev:9910',
8588
chain_id='injective-777',
@@ -91,6 +94,7 @@ def devnet(cls):
9194
def testnet(cls):
9295
return cls(
9396
lcd_endpoint='https://k8s.testnet.lcd.injective.network',
97+
tm_websocket_endpoint='wss://k8s.testnet.tm.injective.network/websocket',
9498
grpc_endpoint='k8s.testnet.chain.grpc.injective.network:443',
9599
grpc_exchange_endpoint='k8s.testnet.exchange.grpc.injective.network:443',
96100
chain_id='injective-888',
@@ -102,7 +106,6 @@ def testnet(cls):
102106
def mainnet(cls, node='k8s'):
103107
nodes = [
104108
'k8s',
105-
'lb',
106109
'sentry0', # us, prod
107110
'sentry1', # us, prod
108111
'sentry2', # us, staging
@@ -111,17 +114,20 @@ def mainnet(cls, node='k8s'):
111114
if node not in nodes:
112115
raise ValueError('Must be one of {}'.format(nodes))
113116

114-
if node == 'lb' or node == 'k8s':
117+
if node == 'k8s':
115118
lcd_endpoint='https://k8s.mainnet.lcd.injective.network'
119+
tm_websocket_endpoint='wss://k8s.mainnet.tm.injective.network/websocket'
116120
grpc_endpoint='k8s.mainnet.chain.grpc.injective.network:443'
117121
grpc_exchange_endpoint='k8s.mainnet.exchange.grpc.injective.network:443'
118122
else:
119123
lcd_endpoint='https://lcd.injective.network'
124+
tm_websocket_endpoint=f'ws://{node}.injective.network:26657/websocket'
120125
grpc_endpoint=f'{node}.injective.network:9900'
121126
grpc_exchange_endpoint=f'{node}.injective.network:9910'
122127

123128
return cls(
124129
lcd_endpoint=lcd_endpoint,
130+
tm_websocket_endpoint=tm_websocket_endpoint,
125131
grpc_endpoint=grpc_endpoint,
126132
grpc_exchange_endpoint=grpc_exchange_endpoint,
127133
chain_id='injective-1',
@@ -133,6 +139,7 @@ def mainnet(cls, node='k8s'):
133139
def local(cls):
134140
return cls(
135141
lcd_endpoint='http://localhost:10337',
142+
tm_websocket_endpoint='ws://localost:26657/websocket',
136143
grpc_endpoint='localhost:9900',
137144
grpc_exchange_endpoint='localhost:9910',
138145
chain_id='injective-1',

0 commit comments

Comments
 (0)