Skip to content

Commit 70ff008

Browse files
committed
chore: added support for the new positions in market exchange query
1 parent afacb99 commit 70ff008

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
import json
3+
4+
from pyinjective.async_client_v2 import AsyncClient
5+
from pyinjective.core.network import Network
6+
7+
8+
async def main() -> None:
9+
# select network: local, testnet, mainnet
10+
network = Network.testnet()
11+
12+
# initialize grpc client
13+
client = AsyncClient(network)
14+
15+
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
16+
positions = await client.fetch_chain_positions_in_market(market_id=market_id)
17+
print(json.dumps(positions, indent=2))
18+
19+
20+
if __name__ == "__main__":
21+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/async_client_v2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,9 @@ async def fetch_chain_derivative_market(
734734
async def fetch_chain_positions(self) -> Dict[str, Any]:
735735
return await self.chain_exchange_v2_api.fetch_positions()
736736

737+
async def fetch_chain_positions_in_market(self, market_id: str) -> Dict[str, Any]:
738+
return await self.chain_exchange_v2_api.fetch_positions_in_market(market_id=market_id)
739+
737740
async def fetch_chain_subaccount_positions(self, subaccount_id: str) -> Dict[str, Any]:
738741
return await self.chain_exchange_v2_api.fetch_subaccount_positions(subaccount_id=subaccount_id)
739742

pyinjective/client/chain/grpc/chain_grpc_exchange_v2_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ async def fetch_positions(self) -> Dict[str, Any]:
378378

379379
return response
380380

381+
async def fetch_positions_in_market(self, market_id: str) -> Dict[str, Any]:
382+
request = exchange_query_pb.QueryPositionsInMarketRequest(market_id=market_id)
383+
response = await self._execute_call(call=self._stub.PositionsInMarket, request=request)
384+
385+
return response
386+
381387
async def fetch_subaccount_positions(self, subaccount_id: str) -> Dict[str, Any]:
382388
request = exchange_query_pb.QuerySubaccountPositionsRequest(subaccount_id=subaccount_id)
383389
response = await self._execute_call(call=self._stub.SubaccountPositions, request=request)

tests/client/chain/grpc/configurable_exchange_v2_query_servicer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self):
4141
self.derivative_market_address_responses = deque()
4242
self.subaccount_trade_nonce_responses = deque()
4343
self.positions_responses = deque()
44+
self.positions_in_market_responses = deque()
4445
self.subaccount_positions_responses = deque()
4546
self.subaccount_position_in_market_responses = deque()
4647
self.subaccount_effective_position_in_market_responses = deque()
@@ -221,6 +222,11 @@ async def SubaccountTradeNonce(
221222
async def Positions(self, request: exchange_query_pb.QueryPositionsRequest, context=None, metadata=None):
222223
return self.positions_responses.pop()
223224

225+
async def PositionsInMarket(
226+
self, request: exchange_query_pb.QueryPositionsInMarketRequest, context=None, metadata=None
227+
):
228+
return self.positions_in_market_responses.pop()
229+
224230
async def SubaccountPositions(
225231
self, request: exchange_query_pb.QuerySubaccountPositionsRequest, context=None, metadata=None
226232
):

tests/client/chain/grpc/test_chain_grpc_exchange_v2_api.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def exchange_servicer():
2323
return ConfigurableExchangeV2QueryServicer()
2424

2525

26-
class TestChainGrpcBankApi:
26+
class TestChainGrpcExchangeV2Api:
2727
@pytest.mark.asyncio
2828
async def test_fetch_exchange_params(
2929
self,
@@ -1554,6 +1554,50 @@ async def test_fetch_positions(
15541554

15551555
assert positions == expected_positions
15561556

1557+
@pytest.mark.asyncio
1558+
async def test_fetch_positions_in_market(
1559+
self,
1560+
exchange_servicer,
1561+
):
1562+
position = exchange_pb.Position(
1563+
isLong=True,
1564+
quantity="1000000000000000",
1565+
entry_price="2000000000000000000",
1566+
margin="2000000000000000000000000000000000",
1567+
cumulative_funding_entry="4000000",
1568+
)
1569+
derivative_position = exchange_pb.DerivativePosition(
1570+
subaccount_id="0x17ef48032cb24375ba7c2e39f384e56433bcab20000000000000000000000000",
1571+
market_id="0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6",
1572+
position=position,
1573+
)
1574+
exchange_servicer.positions_in_market_responses.append(
1575+
exchange_query_pb.QueryPositionsInMarketResponse(state=[derivative_position])
1576+
)
1577+
1578+
api = self._api_instance(servicer=exchange_servicer)
1579+
1580+
positions = await api.fetch_positions_in_market(
1581+
market_id="0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6",
1582+
)
1583+
expected_positions = {
1584+
"state": [
1585+
{
1586+
"subaccountId": derivative_position.subaccount_id,
1587+
"marketId": derivative_position.market_id,
1588+
"position": {
1589+
"isLong": position.isLong,
1590+
"quantity": position.quantity,
1591+
"entryPrice": position.entry_price,
1592+
"margin": position.margin,
1593+
"cumulativeFundingEntry": position.cumulative_funding_entry,
1594+
},
1595+
},
1596+
],
1597+
}
1598+
1599+
assert positions == expected_positions
1600+
15571601
@pytest.mark.asyncio
15581602
async def test_fetch_subaccount_positions(
15591603
self,

0 commit comments

Comments
 (0)