How drift-labs/drift-vaults works
Project Type and Purpose
• This is a blockchain-based DeFi program built with Solana + Anchor.
• Core on-chain logic lives in programs/drift_vaults, implementing “vaults” functionality (deposit, withdraw, profit share, rebase, insurance, etc.).
• Off-chain SDK & CLI in ts/sdk provide programmatic and CLI access to the on-chain program.
• Tests and CI/CD scripts automate local validator tests and devnet/mainnet deployments.
File Structure
• Top-level:
• programs/drift_vaults: Rust/Anchor smart contract
• ts/sdk: TypeScript SDK + CLI
• tests: end-to-end/integration tests (JS/TS, using a local validator or devnet)
• deploy-scripts, build.sh, GitHub Actions (.github/workflows): CI/CD & deployment
• Config: Anchor.toml, Cargo.toml/rust-toolchain.toml, TS configs, GitHub CI, Husky, ESLint, Prettier.
• Notice the “instructions” and “state” subdivisions under the Rust program—this is your business logic vs. account/model definitions.
• The CLI layer under ts/sdk/cli/commands maps 1:1 to on-chain instructions.
Key README Insights
• Anchor v0.29.0, Rust 1.70.0, Solana 1.16.27.
• Local development uses anchor test or the provided test.sh.
• Off-chain usage documented in ts/sdk/README.md.
Main Components & Interactions
a. On-chain Program (Rust/Anchor)
– Entrypoint: Anchor-generated IDL (drift_vaults.json in SDK/IDL)
– Modules: instructions (business logic), state (account schemas), CPIs (calls to other on-chain programs, e.g., Drift core, SPL Token).
– State accounts: Vault, VaultDepositor, InsuranceFundStake, WithdrawRequest, etc.
b. Off-chain SDK & CLI (TypeScript)
– vaultClient.ts: high-level client wrapping web3 calls using the IDL
– cli/commands: scripts to submit transactions
– Subscribers: polling for account changes
c. Solana Cluster
– Local Validator (for tests), Devnet/Mainnet (for deployments)
– Solana RPC node interface
d. CI/CD & Deployment
– GitHub Actions: runs lint, build, test, and optionally triggers deploy-scripts/devnet-deploy.sh
– build.sh / deploy-scripts: shell scripts to build and deploy the Anchor program.
Architectural Patterns & Design Principles
• Separation of concerns: business logic (instructions) vs. data definitions (state).
• CPI pattern: on-chain program invokes other programs for composability.
• SDK/CLI layering: IDL-driven client, command wrappers, subscriber utilities.
• Test harness: run local validator, fixture loading, bulk account loader for performance testing.
Essential Elements
• Clear component labels (e.g., “Drift Vaults Program”, “VaultClient”, “withdraw.ts CLI”).