> For the complete documentation index, see [llms.txt](https://docs.agentipy.fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.agentipy.fun/tools/use-drift-protocol.md).

# Use Drift Protocol

The `Use Drift Protocol` tool, part of AgentiPy, allows users to interact with the Drift Protocol, a decentralized exchange for perpetuals, spot trading, borrowing/lending, and staking on Solana. This tool communicates with an Agentipy proxy service to perform various actions on Drift.

## Functionality

This tool is implemented in the `DriftManager` class. All its methods interact with an Agentipy proxy service (`agent.base_proxy_url`), sending encrypted private keys and potentially an OpenAI API key with each request.

### Key Methods:

The `DriftManager` class offers a comprehensive suite of methods, including but not limited to:

**Account Management:**

* `create_drift_user_account(agent: SolanaAgentKit, deposit_amount: float, deposit_symbol: str)`: Creates a new Drift user account with an initial deposit.
* `deposit_to_drift_user_account(agent: SolanaAgentKit, amount: float, symbol: str, ...)`: Deposits funds into an existing Drift account.
* `withdraw_from_drift_user_account(agent: SolanaAgentKit, amount: float, symbol: str, ...)`: Withdraws funds from a Drift account.
* `check_if_drift_account_exists(agent: SolanaAgentKit)`: Checks if the agent's wallet has a Drift account.
* `drift_user_account_info(agent: SolanaAgentKit)`: Fetches information about the user's Drift account.

**Trading:**

* `trade_using_drift_perp_account(agent: SolanaAgentKit, amount: float, symbol: str, action: str, trade_type: str, price: Optional[float] = None)`: Places a perpetual trade (long/short, market/limit).
* `drift_swap_spot_token(agent: SolanaAgentKit, from_symbol: str, to_symbol: str, ...)`: Swaps spot tokens.
* `get_drift_entry_quote_of_perp_trade(agent: SolanaAgentKit, amount: float, symbol: str, action: str)`: Gets a quote for a perpetual trade.

**Market & Protocol Info:**

* `get_available_drift_markets(agent: SolanaAgentKit)`: Retrieves available markets on Drift.
* `get_drift_perp_market_funding_rate(agent: SolanaAgentKit, symbol: str, ...)`: Gets the funding rate for a perpetual market.
* `get_drift_lend_borrow_apy(agent: SolanaAgentKit, symbol: str)`: Gets lending/borrowing APYs for a spot market.

**Staking & Insurance Fund:**

* `stake_to_drift_insurance_fund(agent: SolanaAgentKit, amount: float, symbol: str)`: Stakes tokens to Drift's insurance fund.
* `request_unstake_from_drift_insurance_fund(...)` and `unstake_from_drift_insurance_fund(...)`.

**Drift Vaults (Advanced):**

* `create_drift_vault(agent: SolanaAgentKit, name: str, ...)`: Creates a new Drift vault.
* `update_drift_vault_delegate(...)`, `update_drift_vault(...)`, `get_drift_vault_info(...)`.
* `deposit_into_drift_vault(...)`, `request_withdrawal_from_drift_vault(...)`, `withdraw_from_drift_vault(...)`.
* `derive_drift_vault_address(...)`.
* `trade_using_delegated_drift_vault(...)`: Executes trades using a delegated vault.

Each method typically returns a dictionary indicating success/failure and transaction details or requested data.

##

**Important Considerations:**

* **Proxy Service:** This tool relies on an Agentipy proxy service. The availability, authentication, and terms of use for this proxy are critical.
* **Financial Risk:** Trading perpetual contracts and interacting with DeFi protocols like Drift involves substantial financial risk, including market volatility, leverage risks (liquidation), and smart contract vulnerabilities. Use with extreme caution.
* **API Keys & Configuration:** An OpenAI API key is passed to the proxy; its role should be clear from proxy documentation. Correct RPC URL, `base_proxy_url`, and `api_version` in the `SolanaAgentKit` are essential.
* **On-Chain Costs:** All actions that modify state (creating accounts, depositing, trading, staking) are on-chain transactions that incur SOL fees and require the necessary collateral or tokens.
* **Drift Protocol Knowledge:** Users should have a good understanding of how Drift Protocol works, including its market structure, account system, margin requirements, and fee schedules, before using these tools with real assets.

## Source Code

You can find the source code for this tool on GitHub: <https://github.com/niceberginc/agentipy/blob/main/agentipy/tools/use_drift.py>
