Skip to main content

How Referral Codes Work

Kamino’s referral system allows referrers to earn commission from the borrowing activity of referred users. Each referrer creates a unique referral code that can be shared publicly without revealing a wallet address, providing a user-friendly way to distribute referral codes.
In the SDK, the shortUrl field represents the referral code.
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 {
  getInitReferrerStateAndShortUrlIxs,
  getReferrerForShortUrl,
  PROGRAM_ID,
} from '@kamino-finance/klend-sdk';
import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer.js';
2

Initialize RPC

Load the keypair and initialize RPC connections.
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');
3

Create Referrer State with Short URL

Generate a unique short URL and create the referrer state initialization instruction.
const shortUrl = `ref-${Date.now()}`;

const initReferrerIx = await getInitReferrerStateAndShortUrlIxs({
  referrer: signer,
  shortUrl,
  programId: PROGRAM_ID,
});
The short URL must be unique, contain a maximum of 32 characters, and use only ASCII alphanumeric characters plus underscore and hyphen.
4

Send Referrer Setup Transaction

Build and send the transaction to initialize the referrer state on-chain.
const { value: setupBlockhash } = await rpc
  .getLatestBlockhash({ commitment: 'finalized' })
  .send();

const setupTx = pipe(
  createTransactionMessage({ version: 0 }),
  (tx) => setTransactionMessageFeePayerSigner(signer, tx),
  (tx) => setTransactionMessageLifetimeUsingBlockhash(setupBlockhash, tx),
  (tx) => appendTransactionMessageInstructions([initReferrerIx], tx)
);

const setupSigned = await signTransactionMessageWithSigners(setupTx);
const setupSignature = getSignatureFromTransaction(setupSigned);

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

console.log(`Referrer setup successful! Signature: ${setupSignature}`);
5

Verify Short URL Resolution

Confirm that the short URL correctly resolves to the referrer wallet address.
const lookedUpReferrer = await getReferrerForShortUrl(rpc, shortUrl, PROGRAM_ID);

console.log(`Short URL "${shortUrl}" resolves to: ${lookedUpReferrer}`);
Referrer setup complete. The short URL is active and can be shared with users to establish referral relationships.