# Protocol Integrations

The ksUSD vault interacts with five external Solana protocols via manual CPI:

* No external SDK dependencies at the Rust level
* Instructions and account structures constructed directly
* All CPIs signed by the vault PDA

***

## Dependencies

| Protocol                | Purpose                                                                                                 | Where used                                                                                                                             |
| ----------------------- | ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| **Drift Perps v2**      | SOL-PERP cross-margin position; jitoSOL collateral; real bidirectional funding                          | `open_position`, `close_position`, `open_reverse`, `close_reverse`, `settle`, `emergency_close`, `enable_drift`                        |
| **Kamino KLend**        | USDC ↔ jitoSOL collateral/borrow for the reverse-basis leg, and USDC lending for idle + reserve capital | `enable_reverse`, `open_reverse`, `close_reverse`, `enable_lending`, `lend_idle_usdc`, `unlend_usdc`, `lend_reserve`, `unlend_reserve` |
| **Jupiter V6**          | All token swaps (USDC ↔ jitoSOL); off-chain quote, on-chain execution                                   | Every open/close path that crosses tokens                                                                                              |
| **Pyth (pull oracles)** | SOL/USD + jitoSOL/USD for NAV math, drawdown guard, LTV refreshes, depeg check                          | `settle`, `open_*`, `close_*`, `set_oracles`                                                                                           |
| **Jito (jitoSOL)**      | Liquid staking; held on Drift as cross-margin collateral, borrowed from Kamino on reverse               | Implicit — staking yield is embedded in the jitoSOL/SOL exchange rate                                                                  |
| **Marginfi**            | Reserved as a second USDC lending venue (v1.1)                                                          | Vault fields pinned at init; not yet CPI-wired                                                                                         |

***

## Verified mainnet addresses

| Item                            | Address                                        |
| ------------------------------- | ---------------------------------------------- |
| Drift Program                   | `dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH`  |
| Drift SOL-PERP market index     | `0`                                            |
| Drift USDC spot market index    | `0`                                            |
| Drift jitoSOL spot market index | `6`                                            |
| Kamino KLend Program            | `KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD`  |
| Kamino Main Market              | `7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF` |
| Kamino USDC Reserve             | `D6q6wuQSrifJKZYpR1M8R4YawnLDtDsMmWM1NbBmgJ59` |
| Kamino USDT Reserve             | `H3t6qZ1JkguCNTi9uzVKqQ7dvt2cum4XiXWom6Gn5e5S` |
| Jupiter V6 Program              | `JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4`  |
| jitoSOL mint                    | `J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn` |
| USDC mint                       | `EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v` |

***

## CPI surface (Rust)

| Module                | Helpers                                                                                                                                                                                                                                 |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `utils::drift_cpi`    | `initialize_user_stats`, `initialize_user`, `deposit`, `withdraw`, `place_perp_order`, `cancel_orders`, `settle_pnl`, `read_drift_funding_rate`                                                                                         |
| `utils::kamino`       | `init_kamino_obligation`, `refresh_obligation`, `deposit_collateral_to_kamino`, `borrow_from_kamino`, `repay_to_kamino`, `withdraw_collateral_from_kamino`, `deposit_reserve_liquidity`, `redeem_reserve_collateral`, `refresh_reserve` |
| `utils::jupiter_swap` | `route_swap`, `require_min_received`                                                                                                                                                                                                    |
| `utils::oracle`       | Pyth pull-oracle staleness + confidence wrappers, jitoSOL/SOL depeg check                                                                                                                                                               |

* Each helper threads the vault PDA signer seeds (`[b"vault", &[bump]]`)
* Minimal Borsh validation on returned account data
* Offsets are hard-coded against the verified Drift / Kamino layouts

***

## Off-chain account assembly

Strategy instructions take 3–5 groups of `remaining_accounts` plus a per-group count. The keeper builds these off-chain using:

* **Drift** — the Drift TypeScript client (or hand-assembled per the v2 IDL) gives the canonical account ordering for `deposit`, `withdraw`, `place_perp_order`, `settle_pnl`.
* **Kamino** — same pattern; the `refresh_obligation` + collateral instructions take a fixed pattern documented in the Kamino IDL.
* **Jupiter** — Jupiter's `/swap-instructions` endpoint returns the swap data buffer (passed as `jupiter_swap_data`) plus the matching account list (passed in the corresponding remaining-accounts group).

See [Keeper bot](/for-operators/keeper-bot.md) for the off-chain orchestration pattern.

***

## Related

* [Whitepaper](/reference/whitepaper.md) — single-page overview of the system
* [Instructions reference](/for-developers/instructions.md) — per-instruction account-context groups
* [Quick start](/for-developers/quick-start.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keystonefi.xyz/reference/protocols.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
