Skip to main content
Follow this guide to withdraw the referral fees you’ve earned from referred users’ borrowing activity.

Withdraw Referrer Fees

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, KaminoAction, parseKeypairFile } from '@kamino-finance/klend-sdk';
2

Load Market and Initialize RPC

Load the keypair, initialize RPC connections, and load the Kamino market.
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 marketPubkey = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
const market = await KaminoMarket.load(rpc, marketPubkey, 400);

const usdcMint = address('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');
3

Check Accumulated Fees

Check all accumulated fees across all tokens.
const allFees = await market!.getAllReferrerFeesCumulative(signer.address);
for (const [mint, fees] of allFees.entries()) {
  console.log(`Token ${mint}: ${fees.toString()}`);
}
Check fees for specific reserve (USDC).
const usdcReserve = market!.getReserveByMint(usdcMint);
if (usdcReserve) {
  const cumulativeFees = await market!.getReferrerFeesCumulativeForReserve(signer.address, usdcReserve);
  const unclaimedFees = await market!.getReferrerFeesUnclaimedForReserve(signer.address, usdcReserve);
  console.log(`USDC Cumulative: ${cumulativeFees.toString()}`);
  console.log(`USDC Unclaimed: ${unclaimedFees.toString()}`);
}
4

Build Withdraw Instructions

Generate instructions to withdraw accumulated referral fees for USDC.
const withdrawAction = await KaminoAction.buildWithdrawReferrerFeeTxns(signer, usdcMint, market!);

const instructions = [...withdrawAction.setupIxs, ...withdrawAction.lendingIxs, ...withdrawAction.cleanupIxs];
5

Send Withdraw Transaction

Fetch the latest blockhash and build the transaction message.
const { value: withdrawBlockhash } = await rpc.getLatestBlockhash({ commitment: 'finalized' }).send();
const withdrawTx = pipe(
  createTransactionMessage({ version: 0 }),
  (tx) => setTransactionMessageFeePayerSigner(signer, tx),
  (tx) => setTransactionMessageLifetimeUsingBlockhash(withdrawBlockhash, tx),
  (tx) => appendTransactionMessageInstructions(instructions, tx)
);
Sign and send the transaction.
const withdrawSigned = await signTransactionMessageWithSigners(withdrawTx);
const withdrawSignature = getSignatureFromTransaction(withdrawSigned);

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

console.log(`Withdraw fees successful! Signature: ${withdrawSignature}`);
Fee withdrawal complete. The accumulated referral fees have been transferred to the referrer wallet.