Skip to main content
Delegated vs Non-Delegated FarmsDifferent 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.

Claim User Rewards

Claim all pending rewards from a user’s farm position.
1

Import Dependencies

Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.
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';
2

Load Signer and Initialize RPC

Load your keypair from a file and initialize RPC connections.
// 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');
3

Load Market and Get Obligation

Load the Kamino market and get the user’s obligation address.
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());
4

Build Claim Instructions

Use the Farms SDK to build claim instructions for all rewards on the farm.
// 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
);
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.
5

Build and Sign Transaction

Use Kit’s functional pipe pattern to build and sign the transaction with a fresh blockhash.
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);
6

Send and Confirm Transaction

Send the transaction to the network and wait for confirmation.
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(claimSigned, {
  commitment: 'confirmed',
  skipPreflight: true,
});

console.log(`Claim farm rewards successful! Signature: ${claimSignature}`);
The rewards have been claimed and transferred to your wallet. You can verify the transaction using the signature on a Solana explorer.