Note
This is an experimental project to create Python bindings for the drift-rs crate. It is not yet ready for production use.
The idea here is to create python bindings for the drift-rs crate using pyo3 in python and slowly replace most of driftpy's internals with the rust sdk, giving us a sort of a single source of truth for the drift sdk, since the drift-rs crate uses the contract code thru drift-ffi-sys.
This project uses maturin to build the rust code and install it in the python environment, uv to manage the python environment, and cargo for the rust code.
Before running uvx maturin develop, make sure you have:
Install rust dependencies required for the driftrs:
rustup install 1.85.0-x86_64-apple-darwin 1.76.0-x86_64-apple-darwin --force-non-host
rustup override set 1.85.0-x86_64-apple-darwinThen install the Python dependencies:
uv syncTo build the project and install it in the python environment, run:
uvx maturin developSome Drift oracle functionality in drift-rs goes through the drift-ffi-sys dynamic library and requires libdrift_ffi_sys.dylib to be linkable at build time and loadable at runtime. On Apple Silicon, the easiest dev path right now is to run everything x86_64 under Rosetta (x86_64 Python + x86_64 extension + x86_64 libdrift_ffi_sys.dylib).
One-time setup (Rosetta shell):
arch -x86_64 zsh
# x86_64 OpenSSL for the x86_64 target build
/usr/local/bin/brew install openssl@3 pkg-config
# Build drift-ffi-sys (produces libdrift_ffi_sys.dylib)
git clone https://github.com/drift-labs/drift-ffi-sys /tmp/drift-ffi-sys
cd /tmp/drift-ffi-sys
cargo build --release
# Make it discoverable without env vars (default link path used by build.rs)
ln -sf /tmp/drift-ffi-sys/target/release/libdrift_ffi_sys.dylib /usr/local/lib/libdrift_ffi_sys.dylibAfter that, you can build with a single command:
./scripts/dev-rosetta.shNotes:
.cargo/config.tomlsets theX86_64_APPLE_DARWIN_OPENSSL_*env vars soopenssl-syscan find x86_64 OpenSSL automatically.build.rslinksdrift_ffi_sysand defaults to/usr/local/libifCARGO_DRIFT_FFI_PATHis not set (and hard-fails if the dylib is missing).scripts/dev-rosetta.shauto-selects the uv-managed x86_64 Python interpreter and builds--target x86_64-apple-darwin.
To add new python dependencies, run:
uv add <dependency>To add a new rust dependency, run:
cargo add <dependency>You have some async logs by enabling the observability feature. This will enable tracing and logging of the async operations. To add more, you'll need to add more tracing macros to the rust code.
maturin develop -F observability
RUST_LOG=info python examples/<SCRIPT_NAME>.pyYou can also install the tokio-console crate to get the console output for the async operations.
Make sure you build with the tokio-console feature enabled.
cargo install tokio-console
RUSTFLAGS="--cfg tokio_unstable" maturin develop -F tokio-consoleYou can check your current build info by running:
import driftpyrs
print(driftpyrs.build_info())To make sure you have the right flags enabled for development.
You can test the async operations by running the examples/test_async_bridge.py file.