# Program

Program Code Repository: <https://github.com/castle-finance/castle-vault>

Functions in the program are created by bundling one or more instructions into a transaction. This document will first explain what each instruction does on its own and then how they are bundled together into various user-facing functions in the SDK.

### Instructions

#### Initialize

Creates a new vault

#### Deposit

Exchanges reserve tokens for vault LP tokens

#### Withdraw

Exchanges vault LP tokens for reserve tokens

#### Rebalance

Calculates and stores the desired allocations to integrated lending markets based on the vault strategy type

Also emits and anchor event containing the new allocations

Required before any reconcile IX is called

#### Refresh \[Jet | Port | Solend]

Refreshes the reserves of the given lending reserve, which is required before any deposit or withdrawal

Calculates the value of the vault held in the given lending reserve, denominated in the reserve token

#### Consolidate Refresh

Calculates the total value of the vault, denominated in the reserve token. This is required before any deposit or withdrawal from the vault

#### Reconcile \[Jet | Port | Solend]

Deposits and withdraws from the given lending market according to the allocations stored by the rebalance IX

### Functions

#### Initialize

IXs

* Initialize

#### Deposit

IXs

* (if reserve token is SOL) Wrap SOL into SPL token
* (if doesn't exist already) Create account for LP token
* Refresh
* Deposit
* (if reserve token is SOL) Close intermediate account created during wrapping

#### Withdraw

IXs

* Refresh
* (if withdraw amount is less than in vault reserves) Reconcile&#x20;
* (if doesn't exist already) Create account for reserve token
* Withdraw
* (if reserve token is SOL) Unwrap from SPL token to SOL

#### Rebalance

IXs

* Refresh
* Rebalance
* Reconcile IXs


---

# 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.castle.finance/technical-guides/program.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.
