Registry / http-networking / driftpy

driftpy

JSON →
library0.8.89pypypi✓ verified 86d ago

DriftPy is the official Python client for the Drift Protocol, a decentralized exchange on the Solana blockchain. It enables users to programmatically trade, fetch data, and interact with the Drift DEX. The library is actively maintained with frequent updates, currently at version 0.8.89.

pip install driftpy
INSTALL
IMPORT
SIG · DRIFTPY
D
driftpy
http-networkingpythonv0.8.89
Install
18.2s avg
Import
1435ms
Disk
180MB
Pass rate
2/ 10
Env Coverage2 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.89 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 19.45s
py 3.11
✕ build_error
✕ build_error
py 3.12
✕ build_error
✕ build_error
py 3.13
✕ build_error
✕ build_error
py 3.9
✕ build_error
✓ 16.9s
180MB installed
● package 180MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DriftClient
from driftpy.drift_client import DriftClient
DriftUser
from driftpy.drift_user import DriftUser
BASE_PRECISION, AMM_RESERVE_PRECISION
from driftpy.constants.numeric_constants import BASE_PRECISION, AMM_RESERVE_PRECISION
AccountSubscriptionConfig
from driftpy.account_subscription_config import AccountSubscriptionConfig
load_keypair
from driftpy.keypair import load_keypair
AsyncClient
from solana.rpc.async_api import AsyncClient
Keypair
from solders.keypair import Keypair
from solana.keypair import Keypair
While `solana.keypair.Keypair` might work in older versions, `solders.keypair.Keypair` is the recommended and re-exported symbol from the `solana` library.
Wallet
from anchorpy import Wallet

This quickstart demonstrates how to initialize the Drift client, connect to a Solana RPC, and subscribe to market data. It assumes `PRIVATE_KEY` (as a comma-separated list of integers) and `RPC_URL` are set in a `.env` file. Proper subscription is critical before making data requests.

import os import asyncio from dotenv import load_dotenv from solders.keypair import Keypair from solana.rpc.async_api import AsyncClient from anchorpy import Wallet from driftpy.drift_client import DriftClient from driftpy.keypair import load_keypair async def main(): load_dotenv() # Load .env file for environment variables # It's safer to use os.environ.get with a default empty string for sensitive info # and then handle the case where it's missing. secret_key_str = os.environ.get("PRIVATE_KEY", "") rpc_url = os.environ.get("RPC_URL", "https://api.devnet.solana.com") if not secret_key_str: print("Error: PRIVATE_KEY environment variable not set.") return if not rpc_url: print("Error: RPC_URL environment variable not set.") return # Ensure the secret key is in the correct format (list of integers) try: secret_key_bytes = bytes(list(map(int, secret_key_str.strip('[]').split(',')))) keypair = Keypair.from_secret_key(secret_key_bytes) except Exception as e: print(f"Error loading keypair: {e}. Ensure PRIVATE_KEY is a comma-separated list of integers in your .env file.") return wallet = Wallet(keypair) connection = AsyncClient(rpc_url) # Initialize DriftClient for 'devnet' # For mainnet, use 'mainnet' drift_client = DriftClient( connection, wallet, "devnet", ) print("Subscribing to Drift Client...") await drift_client.subscribe() # Crucial: Must subscribe before fetching data print("Drift Client subscribed.") # Optional: Register a default sub-account if not already done # await drift_client.add_user(0) # Example: Fetch user stats (after subscription) user_stats = await drift_client.get_user_stats_account() print(f"User Stats: {user_stats}") await drift_client.unsubscribe() await connection.close() if __name__ == "__main__": asyncio.run(main())
Debug
Known issues
breakingFailing to call `await drift_client.subscribe()` after `DriftClient` initialization will cause `IndexError: list index out of range` or similar data access errors when attempting to fetch market or user data.
fix
Always ensure `await drift_client.subscribe()` is called and awaited after instantiating `DriftClient` before attempting to interact with the protocol.
affects: All versions
gotchaUsers of QuickNode's free RPC plan must use `AccountSubscriptionConfig('demo')` and are limited to subscribing to only one perp market and one spot market at a time. Exceeding this limit or using other configurations will result in connection issues (e.g., 'quiknode 400 error').
fix
Initialize `DriftClient` with `account_subscription=AccountSubscriptionConfig('demo')` and ensure `perp_market_indexes` and `spot_market_indexes` parameters each contain at most one index. Call `get_markets_and_oracles()` to retrieve necessary market info for 'demo' config.
affects: All versions
gotchaWhen running on Python 3.13, older versions of `cffi` (specifically below 2.0.0) might not be fully compatible, potentially leading to build or runtime issues, especially with free-threaded Python builds.
fix
Upgrade `driftpy` to version `0.8.86` or higher, which includes a bump to `cffi>=1.17.1` (though `cffi>=2.0.0` is recommended for Python 3.14+). If issues persist, manually ensure `pip install cffi>=2.0.0` is used.
affects: <0.8.86
Errors
Common errors & fixes
IndexError: list index out of range
Attempting to access market or user data from `DriftClient` before `await drift_client.subscribe()` has been successfully called and completed.
fix
Add `await drift_client.subscribe()` immediately after `DriftClient` instantiation to properly initialize data caches. Ensure this is awaited in an `async` context.
AttributeError: transaction has not been signed correctly
This error typically occurs when trying to perform transactions (e.g., cancelling an order) on a delegated account where the signing keypair is not correctly configured or does not have the necessary permissions for the action.
fix
Verify that the `Keypair` used to initialize the `Wallet` and `DriftClient` corresponds to the actual authority or a correctly configured delegate with appropriate permissions. Double-check your `PRIVATE_KEY` and ensure it matches the expected account.
quiknode 400 error
Using a QuickNode free plan RPC endpoint with an `AccountSubscriptionConfig` other than 'demo', or subscribing to more than one perp/spot market.
fix
Switch to `AccountSubscriptionConfig('demo')` for free QuickNode plans. Limit `perp_market_indexes` and `spot_market_indexes` in `DriftClient` initialization to at most one index each. Consider using a paid RPC for higher limits and more flexibility.
Upgrade
Version history
0.8.89latest on PyPI · released Feb 18, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer, but less than 4.0.
solanarequiredFundamental for Solana RPC communication and keypair management.
anchorpyrequiredUsed for Solana wallet integration, commonly alongside driftpy.
python-dotenvoptionalOften used in examples for managing environment variables (e.g., RPC URL, private key).
Agent activity
28 hits · last 30 days
node
26
Amazon
1
OpenAI (training)
1
Resources
driftpy — pip install driftpy · libregistry