Skip to content

Commit 2e59498

Browse files
Support multi market orderbooks, stream trades, async stream position (#89)
* support spot/deriv multi market orderbooks + multi market stream trades * add sync example for spot/deriv orderbooks, trades + regen * fixed: regen * devnet > testnet * turn off insecure * added: async example, client minor fixes * trivial: reverted: unrelated changes * add stream position market_ids arg * fix kw arg * fix: set fields as optional arguments * fix kwarg market id Co-authored-by: Achilleas Kalantzis <[email protected]>
1 parent 114c559 commit 2e59498

File tree

10 files changed

+207
-21
lines changed

10 files changed

+207
-21
lines changed

examples/async/exchange_client/derivative_exchange_rpc/12_StreamTrades.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
async def main() -> None:
2323
network = Network.testnet()
2424
client = AsyncClient(network, insecure=False)
25-
market_id = "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce"
25+
market_ids = [
26+
"0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce",
27+
"0x1f73e21972972c69c03fb105a5864592ac2b47996ffea3c500d1ea2d20138717"
28+
]
2629
subaccount_id = "0xc6fe5d33615a1c52c08018c47e8bc53646a0e101000000000000000000000000"
30+
# stream_derivative_trades use market_id if market_id is provided
31+
# otherwise, use market_ids
2732
trades = await client.stream_derivative_trades(
28-
market_id=market_id,
33+
market_id=market_ids[0],
34+
# market_ids=market_ids,
2935
subaccount_id=subaccount_id
3036
)
3137
async for trade in trades:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.async_client import AsyncClient
20+
from pyinjective.constant import Network
21+
22+
23+
async def main() -> None:
24+
network = Network.testnet()
25+
client = AsyncClient(network, insecure=False)
26+
market_ids = [
27+
"0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce",
28+
"0x1f73e21972972c69c03fb105a5864592ac2b47996ffea3c500d1ea2d20138717"
29+
]
30+
markets = await client.get_derivative_orderbooks(market_ids=market_ids)
31+
print(markets)
32+
33+
if __name__ == '__main__':
34+
logging.basicConfig(level=logging.INFO)
35+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.async_client import AsyncClient
20+
from pyinjective.constant import Network
21+
22+
23+
async def main() -> None:
24+
network = Network.testnet()
25+
client = AsyncClient(network, insecure=False)
26+
market_ids = [
27+
"0x74b17b0d6855feba39f1f7ab1e8bad0363bd510ee1dcc74e40c2adfe1502f781",
28+
"0x26413a70c9b78a495023e5ab8003c9cf963ef963f6755f8b57255feb5744bf31"
29+
]
30+
31+
markets = await client.get_spot_orderbooks(market_ids=market_ids)
32+
print(markets)
33+
34+
35+
if __name__ == '__main__':
36+
logging.basicConfig(level=logging.INFO)
37+
asyncio.get_event_loop().run_until_complete(main())

examples/async/exchange_client/spot_exchange_rpc/9_StreamTrades.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
async def main() -> None:
2323
network = Network.testnet()
2424
client = AsyncClient(network, insecure=False)
25-
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25+
market_ids = [
26+
"0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0",
27+
"0x26413a70c9b78a495023e5ab8003c9cf963ef963f6755f8b57255feb5744bf31"
28+
]
2629
execution_side = "maker" # maker or taker
2730
direction = "sell" # sell or buy
2831
subaccount_id = "0xc6fe5d33615a1c52c08018c47e8bc53646a0e101000000000000000000000000"
2932
trades = await client.stream_spot_trades(
30-
market_id=market_id,
33+
market_id=market_ids[0],
34+
# market_ids=market_ids,
3135
execution_side=execution_side,
3236
direction=direction,
3337
subaccount_id=subaccount_id

examples/sync/exchange_client/derivative_exchange_rpc/12_StreamTrades.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
async def main() -> None:
2424
network = Network.testnet()
2525
client = Client(network, insecure=False)
26-
market_id = "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce"
26+
market_ids = [
27+
"0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce",
28+
"0x1f73e21972972c69c03fb105a5864592ac2b47996ffea3c500d1ea2d20138717"
29+
]
2730
subaccount_id = "0xc6fe5d33615a1c52c08018c47e8bc53646a0e101000000000000000000000000"
31+
# stream_derivative_trades use market_id if market_id is provided
32+
# otherwise, use market_ids
2833
trades = client.stream_derivative_trades(
29-
market_id=market_id,
34+
market_id=market_ids[0],
35+
# market_ids=market_ids,
3036
subaccount_id=subaccount_id
3137
)
3238
for trade in trades:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.client import Client
20+
from pyinjective.constant import Network
21+
22+
23+
async def main() -> None:
24+
network = Network.testnet()
25+
client = Client(network, insecure=False)
26+
market_ids = [
27+
"0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce",
28+
"0x1f73e21972972c69c03fb105a5864592ac2b47996ffea3c500d1ea2d20138717"
29+
]
30+
markets = client.get_derivative_orderbooks(market_ids=market_ids)
31+
print(markets)
32+
33+
if __name__ == '__main__':
34+
logging.basicConfig(level=logging.INFO)
35+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.client import Client
20+
from pyinjective.constant import Network
21+
22+
23+
async def main() -> None:
24+
network = Network.testnet()
25+
client = Client(network, insecure=False)
26+
market_ids = [
27+
"0x74b17b0d6855feba39f1f7ab1e8bad0363bd510ee1dcc74e40c2adfe1502f781",
28+
"0x26413a70c9b78a495023e5ab8003c9cf963ef963f6755f8b57255feb5744bf31"
29+
]
30+
31+
markets = client.get_spot_orderbooks(market_ids=market_ids)
32+
print(markets)
33+
34+
35+
if __name__ == '__main__':
36+
logging.basicConfig(level=logging.INFO)
37+
asyncio.get_event_loop().run_until_complete(main())

examples/sync/exchange_client/spot_exchange_rpc/9_StreamTrades.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
async def main() -> None:
2323
network = Network.testnet()
2424
client = Client(network, insecure=False)
25-
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25+
market_ids = [
26+
"0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0",
27+
"0x26413a70c9b78a495023e5ab8003c9cf963ef963f6755f8b57255feb5744bf31"
28+
]
2629
execution_side = "maker" # maker or taker
2730
direction = "sell" # sell or buy
2831
subaccount_id = "0xc6fe5d33615a1c52c08018c47e8bc53646a0e101000000000000000000000000"
2932
trades = client.stream_spot_trades(
30-
market_id=market_id,
33+
market_id=market_ids[0],
34+
# market_ids=market_ids,
3135
execution_side=execution_side,
3236
direction=direction,
3337
subaccount_id=subaccount_id

pyinjective/async_client.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ async def get_spot_orderbook(self, market_id: str):
361361
req = spot_exchange_rpc_pb.OrderbookRequest(market_id=market_id)
362362
return await self.stubSpotExchange.Orderbook(req)
363363

364+
async def get_spot_orderbooks(self, market_ids: List):
365+
req = spot_exchange_rpc_pb.OrderbooksRequest(market_ids=market_ids)
366+
return await self.stubSpotExchange.Orderbooks(req)
367+
364368
async def get_spot_orders(self, market_id: str, **kwargs):
365369
req = spot_exchange_rpc_pb.OrdersRequest(
366370
market_id=market_id,
@@ -396,9 +400,10 @@ async def stream_spot_orders(self, market_id: str, **kwargs):
396400
)
397401
return self.stubSpotExchange.StreamOrders(req)
398402

399-
async def stream_spot_trades(self, market_id: str, **kwargs):
403+
async def stream_spot_trades(self, **kwargs):
400404
req = spot_exchange_rpc_pb.StreamTradesRequest(
401-
market_id=market_id,
405+
market_id=kwargs.get("market_id"),
406+
market_ids=kwargs.get("market_ids"),
402407
execution_side=kwargs.get("execution_side"),
403408
direction=kwargs.get("direction"),
404409
subaccount_id=kwargs.get("subaccount_id"),
@@ -442,6 +447,10 @@ async def stream_derivative_markets(self):
442447
async def get_derivative_orderbook(self, market_id: str):
443448
req = derivative_exchange_rpc_pb.OrderbookRequest(market_id=market_id)
444449
return await self.stubDerivativeExchange.Orderbook(req)
450+
451+
async def get_derivative_orderbooks(self, market_ids: List):
452+
req = derivative_exchange_rpc_pb.OrderbooksRequest(market_ids=market_ids)
453+
return await self.stubDerivativeExchange.Orderbooks(req)
445454

446455
async def get_derivative_orders(self, market_id: str, **kwargs):
447456
req = derivative_exchange_rpc_pb.OrdersRequest(
@@ -478,9 +487,10 @@ async def stream_derivative_orders(self, market_id: str, **kwargs):
478487
)
479488
return self.stubDerivativeExchange.StreamOrders(req)
480489

481-
async def stream_derivative_trades(self, market_id: str, **kwargs):
490+
async def stream_derivative_trades(self, **kwargs):
482491
req = derivative_exchange_rpc_pb.StreamTradesRequest(
483-
market_id=market_id,
492+
market_id=kwargs.get("market_id"),
493+
market_ids=kwargs.get("market_ids"),
484494
subaccount_id=kwargs.get("subaccount_id"),
485495
execution_side=kwargs.get("execution_side"),
486496
direction=kwargs.get("direction"),
@@ -495,9 +505,11 @@ async def get_derivative_positions(self, market_id: str, **kwargs):
495505
)
496506
return await self.stubDerivativeExchange.Positions(req)
497507

498-
async def stream_derivative_positions(self, market_id: str, **kwargs):
508+
async def stream_derivative_positions(self, **kwargs):
499509
req = derivative_exchange_rpc_pb.StreamPositionsRequest(
500-
market_id=market_id, subaccount_id=kwargs.get("subaccount_id")
510+
market_id=kwargs.get("market_id"),
511+
market_ids=kwargs.get("market_ids"),
512+
subaccount_id=kwargs.get("subaccount_id")
501513
)
502514
return self.stubDerivativeExchange.StreamPositions(req)
503515

pyinjective/client.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ def stream_spot_markets(self):
391391
def get_spot_orderbook(self, market_id: str):
392392
req = spot_exchange_rpc_pb.OrderbookRequest(market_id=market_id)
393393
return self.stubSpotExchange.Orderbook(req)
394+
395+
def get_spot_orderbooks(self, market_ids: List):
396+
req = spot_exchange_rpc_pb.OrderbooksRequest(market_ids=market_ids)
397+
return self.stubSpotExchange.Order(req)
394398

395399
def get_spot_orders(self, market_id: str, **kwargs):
396400
req = spot_exchange_rpc_pb.OrdersRequest(
@@ -436,9 +440,10 @@ def stream_spot_orders(self, market_id: str, **kwargs):
436440
self.set_cookie(res,type="exchange")
437441
return res
438442

439-
def stream_spot_trades(self, market_id: str, **kwargs):
443+
def stream_spot_trades(self, **kwargs):
440444
req = spot_exchange_rpc_pb.StreamTradesRequest(
441-
market_id=market_id,
445+
market_id=kwargs.get("market_id"),
446+
market_ids=kwargs.get("market_ids"),
442447
execution_side=kwargs.get("execution_side"),
443448
direction=kwargs.get("direction"),
444449
subaccount_id=kwargs.get("subaccount_id"),
@@ -489,6 +494,10 @@ def get_derivative_orderbook(self, market_id: str):
489494
req = derivative_exchange_rpc_pb.OrderbookRequest(market_id=market_id)
490495
return self.stubDerivativeExchange.Orderbook(req)
491496

497+
def get_derivative_orderbooks(self, market_ids: List):
498+
req = derivative_exchange_rpc_pb.OrderbooksRequest(market_ids=market_ids)
499+
return self.stubDerivativeExchange.Orderbooks(req)
500+
492501
def get_derivative_orders(self, market_id: str, **kwargs):
493502
req = derivative_exchange_rpc_pb.OrdersRequest(
494503
market_id=market_id,
@@ -533,9 +542,10 @@ def stream_derivative_orders(self, market_id: str, **kwargs):
533542
self.set_cookie(res,type="exchange")
534543
return res
535544

536-
def stream_derivative_trades(self, market_id: str, **kwargs):
545+
def stream_derivative_trades(self, **kwargs):
537546
req = derivative_exchange_rpc_pb.StreamTradesRequest(
538-
market_id=market_id,
547+
market_id=kwargs.get("market_id"),
548+
market_ids=kwargs.get("market_ids"),
539549
subaccount_id=kwargs.get("subaccount_id"),
540550
execution_side=kwargs.get("execution_side"),
541551
direction=kwargs.get("direction"),
@@ -553,10 +563,10 @@ def get_derivative_positions(self, market_id: str, **kwargs):
553563
)
554564
return self.stubDerivativeExchange.Positions(req)
555565

556-
def stream_derivative_positions(self, market_id: str = None, market_ids: list = [], **kwargs):
566+
def stream_derivative_positions(self, **kwargs):
557567
req = derivative_exchange_rpc_pb.StreamPositionsRequest(
558-
market_id=market_id,
559-
market_ids=market_ids,
568+
market_id=kwargs.get("market_id"),
569+
market_ids=kwargs.get("market_ids"),
560570
subaccount_id=kwargs.get("subaccount_id")
561571
)
562572
metadata = self.get_cookie(type="exchange")

0 commit comments

Comments
 (0)