> ## 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.

# Claim Rewards

> Claim all rewards earned from Kamino vaults

## Claiming Rewards

Retrieve and claim all pending rewards from a vault position.

<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 {
      KaminoManager,
      KaminoVault,
      getMedianSlotDurationInMsFromLastEpochs,
      parseKeypairFile,
    } from '@kamino-finance/klend-sdk';
    ```
  </Step>

  <Step>
    ### Load Signer and Initialize RPC

    Load your keypair from a file and initialize RPC connections for both standard and subscription-based operations.

    ```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');
    ```

    <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>
    ### Initialize Manager and Vault

    Initialize the Kamino manager with slot timing and create a vault instance.

    ```typescript theme={null}
    const vaultAddress = address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E'); // USDC vault

    const slotDuration = await getMedianSlotDurationInMsFromLastEpochs();
    const kaminoManager = new KaminoManager(rpc, slotDuration);
    const vault = new KaminoVault(rpc, vaultAddress);
    ```

    <Info>
      The `getMedianSlotDurationInMsFromLastEpochs` function calculates the median slot duration from recent epochs for accurate timing. This vault instance represents a specific Kamino vault you want to claim rewards from.
    </Info>
  </Step>

  <Step>
    ### Generate Claim Rewards Instructions

    Request all claim reward instructions for the vault from the Kamino manager.

    ```typescript theme={null}
    const claimRewardsIxs = await kaminoManager.getClaimAllRewardsForVaultIxs(signer, vault);

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

    <Note>
      The `getClaimAllRewardsForVaultIxs` method generates instructions to claim all available rewards for your position in the specified vault. If no instructions are returned, there may be no claimable rewards available.
    </Note>
  </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(claimRewardsIxs, tx)
    );

    const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
    ```

    <Note>
      Kit's `pipe` function enables functional composition of transaction building steps. The signed transaction is ready to be sent to the network.
    </Note>
  </Step>

  <Step>
    ### Send and Confirm Transaction

    Send the transaction to the network and wait for confirmation using the subscription-based confirmation method.

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

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

    console.log('Claim rewards successful! Signature:', signature);
    ```
  </Step>
</Steps>
