Skip to main content

Claiming Rewards

Retrieve and claim all pending rewards from a vault 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 {
  KaminoManager,
  KaminoVault,
  getMedianSlotDurationInMsFromLastEpochs,
  parseKeypairFile,
} from '@kamino-finance/klend-sdk';
2

Load Signer and Initialize RPC

Load your keypair from a file and initialize RPC connections for both standard and subscription-based operations.
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');
Replace /path/to/your/keypair.json with the actual path to your keypair file. The rpcSubscriptions enables real-time transaction confirmation.
3

Initialize Manager and Vault

Initialize the Kamino manager with slot timing and create a vault instance.
const vaultAddress = address('HDsayqAsDWy3QvANGqh2yNraqcD8Fnjgh73Mhb3WRS5E'); // USDC vault

const slotDuration = await getMedianSlotDurationInMsFromLastEpochs();
const kaminoManager = new KaminoManager(rpc, slotDuration);
const vault = new KaminoVault(rpc, vaultAddress);
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.
4

Generate Claim Rewards Instructions

Request all claim reward instructions for the vault from the Kamino manager.
const claimRewardsIxs = await kaminoManager.getClaimAllRewardsForVaultIxs(signer, vault);

if (!claimRewardsIxs.length) {
  throw new Error('No instructions returned by Kamino SDK');
}
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.
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().send();

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

const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
Kit’s pipe function enables functional composition of transaction building steps. The signed transaction is ready to be sent to the network.
6

Send and Confirm Transaction

Send the transaction to the network and wait for confirmation using the subscription-based confirmation method.
const signature = getSignatureFromTransaction(signedTransaction);

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

console.log('Claim rewards successful! Signature:', signature);