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

# Earn and Withdraw

> Deposit assets into Kamino Earn Vaults and withdraw your shares

## Deposit to Earn Vault

Deposit assets to start earning yield.

<Steps>
  <Step>
    ### Import Dependencies

    Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.

    ```typescript theme={null}
    import {
      createSolanaRpc,
      createSolanaRpcSubscriptions,
      address,
      pipe,
      createTransactionMessage,
      setTransactionMessageFeePayerSigner,
      setTransactionMessageLifetimeUsingBlockhash,
      appendTransactionMessageInstructions,
      signTransactionMessageWithSigners,
      sendAndConfirmTransactionFactory,
      getSignatureFromTransaction,
    } from '@solana/kit';
    import { KaminoVault } from '@kamino-finance/klend-sdk';
    import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer.js';
    import { Decimal } from 'decimal.js';
    ```
  </Step>

  <Step>
    ### Load Signer and Initialize Vault

    Load your keypair from a file and initialize RPC connections and the vault instance.

    ```typescript theme={null}
    const KEYPAIR_FILE = '/path/to/your/keypair.json';
    const signer = await parseKeypairFile(KEYPAIR_FILE);

    const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
    const rpcSubscriptions = createSolanaRpcSubscriptions('wss://api.mainnet-beta.solana.com');
    const vault = new KaminoVault(
      rpc,
      address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E')
    );
    ```

    <Note>
      Replace `/path/to/your/keypair.json` with the actual path to your keypair file. The `rpcSubscriptions` enables real-time transaction confirmation.
    </Note>
  </Step>

  <Step>
    ### Build Deposit Instructions

    Generate deposit instructions for the vault.

    ```typescript theme={null}
    const depositAmount = new Decimal(1.0);
    const bundle = await vault.depositIxs(signer, depositAmount);
    const instructions = [...(bundle.depositIxs || [])];

    if (!instructions.length) {
      throw new Error('No instructions returned by Kamino SDK');
    }
    ```

    <Info>
      The `depositIxs` method returns deposit instructions to add your assets to the vault and mint vault shares.
    </Info>
  </Step>

  <Step>
    ### Build and Sign Transaction

    Use Kit's functional pipe pattern to build and sign the transaction with a fresh blockhash.

    ```typescript theme={null}
    const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();

    const transactionMessage = pipe(
      createTransactionMessage({ version: 0 }),
      (tx) => setTransactionMessageFeePayerSigner(signer, tx),
      (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
      (tx) => appendTransactionMessageInstructions(instructions, tx)
    );

    const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
    ```
  </Step>

  <Step>
    ### Send and Confirm Transaction

    Send the deposit transaction and wait for confirmation.

    ```typescript theme={null}
    const signature = getSignatureFromTransaction(signedTransaction);

    await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction, {
      commitment: 'confirmed',
      skipPreflight: true,
    });

    console.log('Deposit successful! Signature:', signature);
    ```

    <Check>
      Once the deposit transaction is confirmed, your assets are deposited into the vault and you start earning yield. Vault shares are minted to represent your position.
    </Check>
  </Step>
</Steps>

## Withdraw from Earn Vault

Withdraw your vault shares to receive the underlying tokens plus accrued interest.

<Steps>
  <Step>
    ### Import Dependencies

    Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.

    ```typescript theme={null}
    import {
      createSolanaRpc,
      createSolanaRpcSubscriptions,
      address,
      pipe,
      createTransactionMessage,
      setTransactionMessageFeePayerSigner,
      setTransactionMessageLifetimeUsingBlockhash,
      appendTransactionMessageInstructions,
      signTransactionMessageWithSigners,
      sendAndConfirmTransactionFactory,
      getSignatureFromTransaction,
    } from '@solana/kit';
    import { KaminoVault } from '@kamino-finance/klend-sdk';
    import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer.js';
    import { Decimal } from 'decimal.js';
    ```
  </Step>

  <Step>
    ### Load Signer and Initialize Vault

    Load your keypair from a file and initialize RPC connections and the vault instance.

    ```typescript theme={null}
    const KEYPAIR_FILE = '/path/to/your/keypair.json';
    const signer = await parseKeypairFile(KEYPAIR_FILE);

    const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
    const rpcSubscriptions = createSolanaRpcSubscriptions('wss://api.mainnet-beta.solana.com');
    const vault = new KaminoVault(
      rpc,
      address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E')
    );
    ```

    <Note>
      Replace `/path/to/your/keypair.json` with the actual path to your keypair file. The `rpcSubscriptions` enables real-time transaction confirmation.
    </Note>
  </Step>

  <Step>
    ### Build Withdraw Instructions

    Generate withdraw instructions including optional unstaking instructions.

    ```typescript theme={null}
    const withdrawAmount = new Decimal(1.0);
    const bundle = await vault.withdrawIxs(signer, withdrawAmount);
    const instructions = [...(bundle.unstakeFromFarmIfNeededIxs || []), ...(bundle.withdrawIxs || [])];

    if (!instructions.length) {
      throw new Error('No instructions returned by Kamino SDK');
    }
    ```

    <Info>
      The `withdrawIxs` method returns both unstaking and withdraw instructions. The amount represents **vault shares** to redeem, not the underlying token amount.
    </Info>
  </Step>

  <Step>
    ### Build and Sign Transaction

    Use Kit's functional pipe pattern to build and sign the transaction with a fresh blockhash.

    ```typescript theme={null}
    const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();

    const transactionMessage = pipe(
      createTransactionMessage({ version: 0 }),
      (tx) => setTransactionMessageFeePayerSigner(signer, tx),
      (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
      (tx) => appendTransactionMessageInstructions(instructions, tx)
    );

    const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
    ```
  </Step>

  <Step>
    ### Send and Confirm Transaction

    Send the withdraw transaction and wait for confirmation.

    ```typescript theme={null}
    const signature = getSignatureFromTransaction(signedTransaction);

    await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction, {
      commitment: 'confirmed',
      skipPreflight: true,
    });

    console.log('Withdraw successful! Signature:', signature);
    ```

    <Check>
      Once the withdraw transaction is confirmed, your vault shares are burned and you receive the underlying tokens plus any accrued interest.
    </Check>
  </Step>
</Steps>

<AccordionGroup>
  <Accordion title="What Happens When You Deposit?">
    1. You deposit USDC (or another token) into the vault
    2. You receive vault share tokens (like kVUSDC or similar) in your wallet
    3. These shares represent your proportional ownership of the vault
    4. The shares appreciate in value as the vault earns yield
  </Accordion>

  <Accordion title="What Happens When You Withdraw?">
    1. You burn the vault share tokens from your wallet
    2. You receive back the underlying tokens (USDC) based on the current exchange rate
  </Accordion>
</AccordionGroup>
