Skip to main content

Refer User Without Deposit

Create a UserMetadata account with a referrer without requiring an initial deposit. This allows you to link a referrer to a user before they perform any lending operations.
The referrer must be set when the UserMetadata account is created and cannot be changed afterward.
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,
  some,
} from '@solana/kit';
import { KaminoMarket, getUserLutAddressAndSetupIxs, parseKeypairFile } from '@kamino-finance/klend-sdk';
2

Initialize RPC and Load Market

Set up configuration constants and initialize RPC connections.
const KEYPAIR_FILE = '/path/to/your/keypair.json';
const REFERRER = address('EZC9wzVCvihCsCHEMGADYdsRhcpdRYWzSCfqY');
const MARKET_PUBKEY = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
const RPC_ENDPOINT = 'https://api.mainnet-beta.solana.com';
const WS_ENDPOINT = 'wss://api.mainnet-beta.solana.com';

const signer = await parseKeypairFile(KEYPAIR_FILE);
const rpc = createSolanaRpc(RPC_ENDPOINT);
const rpcSubscriptions = createSolanaRpcSubscriptions(WS_ENDPOINT);
const market = await KaminoMarket.load(rpc, MARKET_PUBKEY, 400);
Update the REFERRER address with the wallet address of the referrer who will earn commission from the user’s borrowing activity.
3

Check Existing UserMetadata

Verify that UserMetadata does not already exist for the user.
const [, existingUserMetadata] = await market!.getUserMetadata(signer.address);
if (existingUserMetadata) {
  throw new Error('UserMetadata already created - referrer cannot be changed!');
}
Once UserMetadata is created with a referrer, the referrer cannot be changed. Always check if UserMetadata exists before attempting to create it.
4

Build User Metadata Setup Instructions

Generate instructions to create the UserMetadata account with the referrer.
const [, setupIxsArray] = await getUserLutAddressAndSetupIxs(
  market!,
  signer,
  some(REFERRER),
  false,
  [],
  []
);

const instructions = setupIxsArray.flat();
getUserLutAddressAndSetupIxs creates the necessary instructions to set up UserMetadata with a referrer without requiring a deposit operation.
5

Build and Sign Transaction

Build the transaction using Kit’s functional pipe pattern.
const { value: blockhash } = await rpc.getLatestBlockhash({ commitment: 'finalized' }).send();
const tx = pipe(
  createTransactionMessage({ version: 0 }),
  (tx) => setTransactionMessageFeePayerSigner(signer, tx),
  (tx) => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx),
  (tx) => appendTransactionMessageInstructions(instructions, tx)
);
const signed = await signTransactionMessageWithSigners(tx);
6

Send and Confirm Transaction

Send the transaction and confirm it using WebSocket.
const signature = getSignatureFromTransaction(signed);

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

console.log('UserMetadata created with referrer');
console.log(`Signature: ${signature}`);
UserMetadata creation complete. The referrer is now permanently linked to the user and will earn commission from the user’s future borrowing activity.
Use this method when:
  • You want to link a referrer to a user before they make their first deposit
  • You’re creating UserMetadata as a separate step from deposit operations
  • You have a simpler flow that doesn’t require immediate deposit
Use Deposit with Referral when:
  • You want to combine UserMetadata creation and deposit in a single transaction
  • The user is ready to deposit immediately
  • You want to minimize the number of transactions