From 8d54a3f5bbe9e08c6ce5e1ade87d51e256c712ee Mon Sep 17 00:00:00 2001 From: sina <20732540+SinaKhalili@users.noreply.github.com> Date: Fri, 15 Nov 2024 22:33:40 -0800 Subject: [PATCH] Add some examples for getting high leverage mode + devnet oracles --- examples/get_oracle_devnet.py | 48 +++++++++++++++++++++++++ examples/high_leverage_users.py | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 examples/get_oracle_devnet.py create mode 100644 examples/high_leverage_users.py diff --git a/examples/get_oracle_devnet.py b/examples/get_oracle_devnet.py new file mode 100644 index 00000000..5b78f39e --- /dev/null +++ b/examples/get_oracle_devnet.py @@ -0,0 +1,48 @@ +import asyncio +import os + +from anchorpy import Wallet +from dotenv import load_dotenv +from driftpy.account_subscription_config import AccountSubscriptionConfig +from driftpy.drift_client import DriftClient +from driftpy.keypair import load_keypair +from driftpy.types import OrderParams +from driftpy.types import OrderType +from driftpy.types import PositionDirection +from driftpy.types import PostOnlyParams +from driftpy.types import TxParams +from solana.rpc.async_api import AsyncClient + + +async def main(): + load_dotenv() + url = os.getenv("DEVNET_RPC_ENDPOINT") + connection = AsyncClient(url) + print("RPC URL:", url) + + print("Checking devnet constants") + drift_client = DriftClient( + connection, + Wallet.dummy(), + env="devnet", + account_subscription=AccountSubscriptionConfig("cached"), + ) + + print("Subscribing to Drift Client") + + await drift_client.subscribe() + received_perp_markets = sorted( + drift_client.get_perp_market_accounts(), key=lambda market: market.market_index + ) + for market in received_perp_markets: + print(market.market_index, market.amm.oracle) + + print("Subscribed to Drift Client") + + # Ensure proper cleanup + await drift_client.unsubscribe() + await connection.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/high_leverage_users.py b/examples/high_leverage_users.py new file mode 100644 index 00000000..1727f772 --- /dev/null +++ b/examples/high_leverage_users.py @@ -0,0 +1,62 @@ +import asyncio +from asyncio import create_task +from asyncio import gather +import os + +from anchorpy import Wallet +from dotenv import load_dotenv +from driftpy.account_subscription_config import AccountSubscriptionConfig +from driftpy.drift_client import DriftClient +from driftpy.keypair import load_keypair +from driftpy.market_map.market_map import MarketMap +from driftpy.market_map.market_map import MarketMapConfig +from driftpy.market_map.market_map_config import ( + WebsocketConfig as MarketMapWebsocketConfig, +) +from driftpy.market_map.market_map_config import MarketMapConfig +from driftpy.pickle.vat import Vat +from driftpy.types import MarketType +from driftpy.types import OrderParams +from driftpy.types import OrderType +from driftpy.types import PositionDirection +from driftpy.types import PostOnlyParams +from driftpy.types import TxParams +from driftpy.user_map.user_map import UserMap +from driftpy.user_map.user_map_config import ( + WebsocketConfig as UserMapWebsocketConfig, +) +from driftpy.user_map.user_map_config import UserMapConfig +from driftpy.user_map.user_map_config import UserStatsMapConfig +from driftpy.user_map.userstats_map import UserStatsMap +from driftpy.user_map.userstats_map import UserStatsMapConfig +from solana.rpc.async_api import AsyncClient + + +async def main(): + load_dotenv() + url = os.getenv("RPC_URL") + connection = AsyncClient(url) + dc = DriftClient( + connection, + Wallet.dummy(), + "mainnet", + ) + await dc.subscribe() + user = UserMap(UserMapConfig(dc, UserMapWebsocketConfig())) + await user.subscribe() + + high_leverage_users = [] + keys = [] + for key, user in user.user_map.items(): + if user.is_high_leverage_mode(): + high_leverage_users.append(user) + keys.append(key) + return high_leverage_users, keys + + +if __name__ == "__main__": + high_leverage_users, keys = asyncio.run(main()) + keys.sort() + # with open("high_leverage_users.txt", "w") as f: + # for key in keys: + # f.write(f"{key}\n")