### Setup AccountSubscriptionConfig("demo") for DriftClient Source: https://github.com/drift-labs/driftpy/blob/master/README.md Example setup for `AccountSubscriptionConfig("demo")` which is required for QuickNode free plan users. This configuration listens to specific perp and spot markets. Ensure `get_markets_and_oracles` is called to retrieve necessary market and oracle information. ```python # This example will listen to perp markets 0 & 1 and spot market 0 # If you are listening to any perp markets, you must listen to spot market 0 or the SDK will break perp_markets = [0, 1] spot_market_oracle_infos, perp_market_oracle_infos, spot_market_indexes = get_markets_and_oracles(perp_markets = perp_markets) oracle_infos = spot_market_oracle_infos + perp_market_oracle_infos drift_client = DriftClient( connection, wallet, "mainnet", perp_market_indexes = perp_markets, spot_market_indexes = spot_market_indexes, oracle_infos = oracle_infos, account_subscription = AccountSubscriptionConfig("demo"), ) await drift_client.subscribe() ``` -------------------------------- ### Setup Development Environment Source: https://github.com/drift-labs/driftpy/blob/master/README.md Steps to set up the development environment for DriftPy, including installing Python, setting up a virtual environment with Poetry, and installing dependencies. ```bash pyenv install 3.10.11 pyenv global 3.10.11 poetry env use $(pyenv which python) ``` ```bash poetry install ``` -------------------------------- ### Launch helloworld_geyser Example Source: https://github.com/drift-labs/driftpy/blob/master/src/driftpy/accounts/grpc/geyser_codegen/README.md Launches the example Python script to fetch the latest Solana slot number. Requires RPC endpoint and authentication token. ```bash (venv) $ python helloworld_geyser.py --rpc-fqdn 'index.rpcpool.com' --x-token '2625ae71-0823-41b3-b3bc-4ff89d762d52' ``` -------------------------------- ### Drift SDK Example Usage Source: https://github.com/drift-labs/driftpy/blob/master/docs/index.md Demonstrates initializing DriftClient and DriftUser, opening a position, adding liquidity, and inspecting user metrics like leverage and unrealized PNL. Caching can be enabled for DriftUser to reduce RPC calls. ```python from solana.keypair import Keypair from driftpy.drift_client import DriftClient from driftpy.drift_user import DriftUser from driftpy.constants.numeric_constants import BASE_PRECISION, AMM_RESERVE_PRECISION from anchorpy import Provider, Wallet from solana.rpc.async_api import AsyncClient import json from solana.publickey import PublicKey from driftpy.constants.config import configs from driftpy.constants.types import PositionDirection # load keypair from file KEYPATH = '../your-keypair-secret.json' with open(KEYPATH, 'r') as f: secret = json.load(f) kp = Keypair.from_secret_key(bytes(secret)) # create clearing house for mainnet ENV = 'mainnet' config = configs[ENV] wallet = Wallet(kp) connection = AsyncClient(config.default_http) provider = Provider(connection, wallet) drift_client = DriftClient.from_config(config, provider) drift_user = DriftUser(drift_client) # open a 10 SOL long position sig = await drift_client.open_position( PositionDirection.LONG(), # long int(10 * BASE_PRECISION), # 10 in base precision 0, # sol market index ) # mint 100 LP shares on the SOL market await drift_client.add_liquidity( int(100 * AMM_RESERVE_PRECISION), 0, ) # inspect user's leverage leverage = await drift_user.get_leverage() print('current leverage:', leverage / 10_000) # you can also inspect other accounts information using the (authority=) flag bigz_acc = DriftUser(drift_client, user_public_key=PublicKey('bigZ')) leverage = await bigz_acc.get_leverage() print('bigZs leverage:', leverage / 10_000) # clearing house user calls can be expensive on the rpc so we can cache them drift_user = DriftUser(drift_client, use_cache=True) await drift_user.set_cache() # works without any rpc calls (uses the cached data) upnl = await drift_user.get_unrealized_pnl(with_funding=True) print('upnl:', upnl) ``` -------------------------------- ### Install Python Dependencies Source: https://github.com/drift-labs/driftpy/blob/master/src/driftpy/accounts/grpc/geyser_codegen/README.md Installs project dependencies including grpcio. Ensure you are in a virtual environment. ```bash python -m venv venv . venv/bin/activate (venv) $ python -m pip install -U pip (venv) $ python -m pip install -r requirements.txt ``` -------------------------------- ### Fetch and Inspect User Leverage Source: https://github.com/drift-labs/driftpy/blob/master/docs/clearing_house_user.md Instantiate the DriftClient and User objects to fetch and print the current user's leverage. This example also shows how to inspect leverage for a different authority and demonstrates caching user data for efficiency. ```python drift_client = DriftClient.from_config(config, provider) drift_user = User(drift_client) # inspect user's leverage leverage = await drift_user.get_leverage() print('current leverage:', leverage / 10_000) # you can also inspect other accounts information using the (authority=) flag bigz_acc = User(drift_client, authority=PublicKey('bigZ')) leverage = await bigz_acc.get_leverage() print('bigZs leverage:', leverage / 10_000) # user calls can be expensive on the rpc so we can cache them drift_user = User(drift_client, use_cache=True) await drift_user.set_cache() # works without any rpc calls (uses the cached data) upnl = await drift_user.get_unrealized_pnl(with_funding=True) print('upnl:', upnl) ``` -------------------------------- ### Install DriftPy Source: https://github.com/drift-labs/driftpy/blob/master/docs/index.md Install the DriftPy SDK using pip. Requires Python version 3.10 or higher. ```bash pip install driftpy ``` -------------------------------- ### Initialize Drift Client and Open Position Source: https://github.com/drift-labs/driftpy/blob/master/docs/clearing_house.md Initializes the Drift Client from configuration and opens a long position. Ensure the config and provider are correctly set up before use. ```python drift_client = DriftClient.from_config(config,provider) # open a 10 SOL long position sig = await drift_client.open_position( PositionDirection.LONG(), # long int(10 * BASE_PRECISION), # 10 in base precision 0, # sol market index ) ``` -------------------------------- ### Run DriftPy Tests Source: https://github.com/drift-labs/driftpy/blob/master/README.md Instructions for running tests after setting up the RPC endpoint. Ensure `MAINNET_RPC_ENDPOINT` and `DEVNET_RPC_ENDPOINT` are exported. ```bash export MAINNET_RPC_ENDPOINT="" export DEVNET_RPC_ENDPOINT="https://api.devnet.solana.com" # or your own RPC poetry run pytest -v -s -x tests/ci/*.py poetry run pytest -v -s tests/math/*.py ``` -------------------------------- ### Retrieve Perp and Spot Market Accounts Source: https://github.com/drift-labs/driftpy/blob/master/docs/accounts.md Fetches and prints information about a specific PERP market and a specific SPOT market. Ensure DriftClient is initialized with configuration and a provider. ```python drift_client= DriftClient.from_config(config, provider) # get sol market info sol_market_index = 0 sol_market = await get_perp_market_account(drift_client.program, sol_market_index) print( sol_market.amm.sqrt_k, sol_market.amm.base_asset_amount_long, sol_market.amm.base_asset_amount_short, ) # get usdc spot market info usdc_spot_market_index = 0 usdc_market = await get_spot_market_account(drift_client.program, usdc_spot_market_index) print( usdc.market_index, usdc.deposit_balance, usdc.borrow_balance, ) ``` -------------------------------- ### Add Liquidity to SOL Market Source: https://github.com/drift-labs/driftpy/blob/master/docs/clearing_house.md Mints LP shares on the SOL market. The amount should be specified in AMM reserve precision. ```python # mint 100 LP shares on the SOL market await drift_client.add_liquidity( int(100 * AMM_RESERVE_PRECISION), 0, ) ``` -------------------------------- ### Run Insurance Fund Staking Script Source: https://github.com/drift-labs/driftpy/blob/master/examples/readme.md Execute the `if_stake.py` script to add funds to the insurance fund. Ensure you provide the correct keypair path, amount, market, operation, and environment. ```bash python if_stake.py \ --keypath ../keypairs/x19.json # this should be the keypair path --amount 100 # USD amount --market 0 # USD spot market --operation add --env mainnet ``` -------------------------------- ### Generate gRPC Stubs Source: https://github.com/drift-labs/driftpy/blob/master/src/driftpy/accounts/grpc/geyser_codegen/README.md Generates Python gRPC stubs and data types from protobuf definitions. Ensure you are in the correct directory. ```bash python -m grpc_tools.protoc -I../../yellowstone-grpc-proto/proto/ --python_out=. --pyi_out=. --grpc_python_out=. ../../yellowstone-grpc-proto/proto/* ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.