> ## 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 User Rewards

> Claim all pending rewards from lending farm positions

<Note>
  **Delegated vs Non-Delegated Farms**

  Different farm types use different delegation patterns. For the correct delegatees for your specific farm, check the user farm state returned by `getAllFarmsForUser` in the "Calculate Claimable User Rewards" tutorial.
</Note>

## Claim User Rewards

Claim all pending rewards from a user's farm 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 { KaminoMarket, parseKeypairFile, VanillaObligation, PROGRAM_ID } from '@kamino-finance/klend-sdk';
    import { Farms } from '@kamino-finance/farms-sdk';
    ```
  </Step>

  <Step>
    ### Load Signer and Initialize RPC

    Load your keypair from a file and initialize RPC connections.

    ```typescript theme={null}
    // Configuration - UPDATE THESE VALUES
    const KEYPAIR_FILE = '/path/to/your/keypair.json';

    // Load keypair from file
    const signer = await parseKeypairFile(KEYPAIR_FILE);

    // Initialize RPC and RPC Subscriptions
    const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
    const rpcSubscriptions = createSolanaRpcSubscriptions('wss://api.mainnet-beta.solana.com');
    ```
  </Step>

  <Step>
    ### Load Market and Get Obligation

    Load the Kamino market and get the user's obligation address.

    ```typescript theme={null}
    const marketPubkey = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
    const market = await KaminoMarket.load(rpc, marketPubkey, 400);

    // Get obligation address for delegatees array
    const kaminoObligation = await market!.getObligationByWallet(signer.address, new VanillaObligation(PROGRAM_ID));
    if (!kaminoObligation) {
      throw new Error('No obligation found');
    }

    console.log('Obligation:', kaminoObligation.obligationAddress.toString());
    ```
  </Step>

  <Step>
    ### Build Claim Instructions

    Use the Farms SDK to build claim instructions for all rewards on the farm.

    ```typescript theme={null}
    // Build claim instructions using farms-sdk
    const farm = new Farms(rpc);
    const claimIxs = await farm.claimForUserForFarmAllRewardsIx(
      signer, // payer
      signer.address, // user (wallet address = owner)
      address('Gsoc83qRQ5X1LfGGbPqU2FdxZnveqbkP2vMXq6BcmtMk'), // farm
      true, // isDelegated = true for lending farms
      [signer.address, kaminoObligation.obligationAddress] // delegatees array
    );
    ```

    <Warning>
      The `delegatees` array values depend on your specific farm position. To find the correct delegatee addresses for your farm, use the "Calculate Claimable User Rewards" tutorial - it shows the actual delegatee in the user farm state.
    </Warning>
  </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({ commitment: 'finalized' }).send();
    const claimTx = pipe(
      createTransactionMessage({ version: 0 }),
      (tx) => setTransactionMessageFeePayerSigner(signer, tx),
      (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
      (tx) => appendTransactionMessageInstructions(claimIxs, tx)
    );
    const claimSigned = await signTransactionMessageWithSigners(claimTx);
    const claimSignature = getSignatureFromTransaction(claimSigned);
    ```
  </Step>

  <Step>
    ### Send and Confirm Transaction

    Send the transaction to the network and wait for confirmation.

    ```typescript theme={null}
    await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(claimSigned, {
      commitment: 'confirmed',
      skipPreflight: true,
    });

    console.log(`Claim farm rewards successful! Signature: ${claimSignature}`);
    ```

    <Check>
      The rewards have been claimed and transferred to your wallet. You can verify the transaction using the signature on a Solana explorer.
    </Check>
  </Step>
</Steps>
