> ## Documentation Index
> Fetch the complete documentation index at: https://docs.exponent.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Allocate and exit

> Deposit into a Kamino Vault, then redeem its shares in a separate run.

This script allocates tokens to a Kamino Vault and later redeems a chosen amount of shares. It uses the transaction builder for policy matching, supported tracking, refresh scheduling, and ordered submission.

## Set up

Install the constrained deposit/withdraw policy and configure the share-price source described in [Kamino Vaults](https://docs.exponent.finance/developer-strategy-vaults/kamino-vault-instructions.md). Track the underlying token, fund its Squads account, and keep the required reserve allocation. The signer must have the permissions needed for any first-use tracking.

```bash theme={null}
npm install @exponent-labs/exponent-sdk @solana/web3.js@1.98.4 bn.js
npm install --save-dev typescript tsx @types/node @types/bn.js
export RPC_URL="https://your-mainnet-rpc-endpoint"
export SIGNER_KEYPAIR_PATH="/absolute/path/to/manager.json"
export VAULT_ADDRESS="<strategy-vault>"
export KAMINO_VAULT_ADDRESS="<selected-kamino-vault>"
```

## Script

Save as `allocate-and-exit.mts`. Running it simulates and **submits** its execution plan.

```typescript allocate-and-exit.mts theme={null}
import BN from "bn.js";
import {
  ExponentVault, VaultTransactionBuilder, kaminoVaultAction,
  decodeKaminoVaultState, KAMINO_VAULT_PROGRAM_ID,
} from "@exponent-labs/exponent-sdk";
import { readFileSync } from "node:fs";
import {
  Connection, Keypair, PublicKey,
} from "@solana/web3.js";

function requiredEnv(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`Set ${name}`);
  return value;
}
const connection = new Connection(requiredEnv("RPC_URL"), "confirmed");
const signer = Keypair.fromSecretKey(Uint8Array.from(
  JSON.parse(readFileSync(requiredEnv("SIGNER_KEYPAIR_PATH"), "utf8")),
));

const phase = process.argv[2];
if (phase !== "allocate" && phase !== "exit") {
  throw new Error("Usage: npx tsx allocate-and-exit.mts allocate|exit");
}
const vault = await ExponentVault.load({
  connection, address: new PublicKey(requiredEnv("VAULT_ADDRESS")),
});
const kaminoVault = new PublicKey(requiredEnv("KAMINO_VAULT_ADDRESS"));
const account = await connection.getAccountInfo(kaminoVault);
if (!account || !account.owner.equals(KAMINO_VAULT_PROGRAM_ID)) {
  throw new Error("Invalid Kamino Vault account");
}
const { tokenMint, sharesMint } = decodeKaminoVaultState(account.data);
const rawAmount = requiredEnv(phase === "allocate" ? "DEPOSIT_ATOMIC" : "SHARES_ATOMIC");
if (!/^[1-9][0-9]*$/.test(rawAmount)) throw new Error("Amount must be a positive atomic integer");
const amount = new BN(rawAmount);
const action = phase === "allocate"
  ? kaminoVaultAction.deposit({ vault: kaminoVault, amount })
  : kaminoVaultAction.withdraw({ vault: kaminoVault, sharesAmount: amount });

const plan = await VaultTransactionBuilder
  .create({ vault, connection, signer: signer.publicKey })
  .addActions([action], { label: phase })
  .build();
console.log({
  depositMint: tokenMint.toBase58(), sharesMint: sharesMint.toBase58(),
  orderedSets: plan.labels,
});
const result = await plan.send({ signers: [signer] });
console.log(result.signatures);

```

## Run each phase

```bash theme={null}
export DEPOSIT_ATOMIC="1000000"
npx tsx allocate-and-exit.mts allocate
# Later, choose an amount in share-mint atomic units:
export SHARES_ATOMIC="500000"
npx tsx allocate-and-exit.mts exit
```

Deposit amounts use underlying-token units; exits use share units. Omitting a reserve lets withdrawal planning use several ordered reserve legs when required. All returned sets must execute in order. Resolve an uncertain send status before retrying; an allocation is not safe to replay automatically.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.