Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions nado_protocol/engine_client/query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Optional
import requests

Expand Down Expand Up @@ -182,7 +183,10 @@ def get_order(self, product_id: int, digest: str) -> OrderData:
)

def get_subaccount_info(
self, subaccount: str, txs: Optional[list[QuerySubaccountInfoTx]] = None
self,
subaccount: str,
txs: Optional[list[QuerySubaccountInfoTx]] = None,
pre_state: Optional[bool] = None,
) -> SubaccountInfoData:
"""
Query the engine for the state of a subaccount, including balances.
Expand All @@ -193,11 +197,26 @@ def get_subaccount_info(
txs (list[QuerySubaccountInfoTx], optional): You can optionally provide a list of txs, to get an estimated view
of what the subaccount state would look like if the transactions were applied.

pre_state (bool, optional): When True and txs are provided, returns the subaccount state before the
transactions were applied in the pre_state field. Defaults to False.

Returns:
SubaccountInfoData: Information about the specified subaccount.
"""
txns_str = None
if txs is not None:
txns_str = json.dumps([tx.dict() for tx in txs])

pre_state_str = None
if pre_state is not None:
pre_state_str = str(pre_state).lower()

return ensure_data_type(
self.query(QuerySubaccountInfoParams(subaccount=subaccount, txs=txs)).data,
self.query(
QuerySubaccountInfoParams(
subaccount=subaccount, txns=txns_str, pre_state=pre_state_str
)
).data,
SubaccountInfoData,
)

Expand Down
1 change: 1 addition & 0 deletions nado_protocol/engine_client/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class EngineClientOpts(NadoClientOpts):
"ContractsData",
"NoncesData",
"OrderData",
"PreState",
"SubaccountInfoData",
"SubaccountOpenOrdersData",
"MarketLiquidityData",
Expand Down
15 changes: 14 additions & 1 deletion nado_protocol/engine_client/types/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class QuerySubaccountInfoParams(NadoBaseModel):

type = EngineQueryType.SUBACCOUNT_INFO.value
subaccount: str
txs: Optional[list[QuerySubaccountInfoTx]]
txns: Optional[str]
pre_state: Optional[str]


class QuerySubaccountOpenOrdersParams(NadoBaseModel):
Expand Down Expand Up @@ -299,6 +300,17 @@ class OrderData(NadoBaseModel):
placed_at: str


class PreState(NadoBaseModel):
"""
Model for subaccount state before simulated transactions were applied.
"""

healths: list[SubaccountHealth]
health_contributions: list[list[str]]
spot_balances: list[SpotProductBalance]
perp_balances: list[PerpProductBalance]


class SubaccountInfoData(NadoBaseModel):
"""
Model for detailed info about a subaccount, including balances.
Expand All @@ -314,6 +326,7 @@ class SubaccountInfoData(NadoBaseModel):
perp_balances: list[PerpProductBalance]
spot_products: list[SpotProduct]
perp_products: list[PerpProduct]
pre_state: Optional[PreState]

def parse_subaccount_balance(
self, product_id: int
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nado-protocol"
version = "0.2.7"
version = "0.2.8"
description = "Nado Protocol SDK"
authors = ["Jeury Mejia <jeury@inkfnd.com>"]
homepage = "https://nado.xyz"
Expand Down
26 changes: 25 additions & 1 deletion sanity/engine_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from nado_protocol.engine_client.types.query import (
QueryMaxOrderSizeParams,
)
from nado_protocol.engine_client.types.models import ApplyDelta, ApplyDeltaTx
from nado_protocol.utils.bytes32 import (
bytes32_to_hex,
str_to_hex,
Expand Down Expand Up @@ -79,7 +80,7 @@ def run():
pprint(spots_apr)

print("querying orderbook for BTC-PERP pair...")
btc_perp_book = client.get_orderbook("BTC-PERP_USDT", 10)
btc_perp_book = client.get_orderbook("BTC-PERP_USDT0", 10)
pprint(btc_perp_book)

order_price = 100_000
Expand Down Expand Up @@ -132,6 +133,29 @@ def run():
subaccount_info = client.get_subaccount_info(sender)
print("subaccount info:", subaccount_info.json(indent=2))

print("querying subaccount info with simulated transaction and pre_state...")
# Simulate applying a delta to a perp product
simulated_tx = ApplyDeltaTx(
apply_delta=ApplyDelta(
product_id=2,
subaccount=sender,
amount_delta="100000000000000000",
v_quote_delta="3033500000000000000000",
)
)
subaccount_info_with_pre_state = client.get_subaccount_info(
sender, txs=[simulated_tx], pre_state=True
)
print(
"subaccount info with simulation:",
subaccount_info_with_pre_state.json(indent=2),
)
if subaccount_info_with_pre_state.pre_state:
print("✓ pre_state returned successfully")
print("pre_state healths:", subaccount_info_with_pre_state.pre_state.healths)
else:
print("✗ Warning: pre_state was not returned")

print("querying subaccount open orders...")
subaccount_open_orders = client.get_subaccount_open_orders(product_id, sender)
print("subaccount open orders:", subaccount_open_orders.json(indent=2))
Expand Down