> ## Documentation Index
> Fetch the complete documentation index at: https://kamino.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Refer User Without Deposit

> Create UserMetadata with a referrer without requiring an initial deposit

## 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.

<Note>
  The referrer must be set when the UserMetadata account is created and cannot be changed afterward.
</Note>

<Tabs>
  <Tab title="TypeScript">
    <Steps>
      <Step>
        ### Import Dependencies

        Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.

        ```typescript theme={null}
        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';
        ```
      </Step>

      <Step>
        ### Initialize RPC and Load Market

        Set up configuration constants and initialize RPC connections.

        ```typescript theme={null}
        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);
        ```

        <Note>
          Update the `REFERRER` address with the wallet address of the referrer who will earn commission from the user's borrowing activity.
        </Note>
      </Step>

      <Step>
        ### Check Existing UserMetadata

        Verify that UserMetadata does not already exist for the user.

        ```typescript theme={null}
        const [, existingUserMetadata] = await market!.getUserMetadata(signer.address);
        if (existingUserMetadata) {
          throw new Error('UserMetadata already created - referrer cannot be changed!');
        }
        ```

        <Warning>
          Once UserMetadata is created with a referrer, the referrer cannot be changed. Always check if UserMetadata exists before attempting to create it.
        </Warning>
      </Step>

      <Step>
        ### Build User Metadata Setup Instructions

        Generate instructions to create the UserMetadata account with the referrer.

        ```typescript theme={null}
        const [, setupIxsArray] = await getUserLutAddressAndSetupIxs(
          market!,
          signer,
          some(REFERRER),
          false,
          [],
          []
        );

        const instructions = setupIxsArray.flat();
        ```

        <Info>
          `getUserLutAddressAndSetupIxs` creates the necessary instructions to set up UserMetadata with a referrer without requiring a deposit operation.
        </Info>
      </Step>

      <Step>
        ### Build and Sign Transaction

        Build the transaction using Kit's functional pipe pattern.

        ```typescript theme={null}
        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);
        ```
      </Step>

      <Step>
        ### Send and Confirm Transaction

        Send the transaction and confirm it using WebSocket.

        ```typescript theme={null}
        const signature = getSignatureFromTransaction(signed);

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

        console.log('UserMetadata created with referrer');
        console.log(`Signature: ${signature}`);
        ```

        <Check>
          UserMetadata creation complete. The referrer is now permanently linked to the user and will earn commission from the user's future borrowing activity.
        </Check>
      </Step>
    </Steps>
  </Tab>

  <Tab title="Rust">
    <Steps>
      <Step>
        ### Add Dependencies

        ```toml theme={null}
        [dependencies]
        klend-interface = "0.1.0"
        solana-pubkey = "2.1"
        solana-instruction = "2.1"
        solana-sdk = "~2.3"
        solana-client = "~2.3"
        spl-token = "7"
        spl-associated-token-account = "6"
        ```

        <Note>
          `klend-interface` is a lightweight Rust instruction builder that creates `Vec<Instruction>` with required refresh instructions prepended automatically.
        </Note>
      </Step>

      <Step>
        ### Set Up RPC Client

        ```rust theme={null}
        use solana_client::rpc_client::RpcClient;
        use solana_sdk::signer::keypair::read_keypair_file;
        use solana_sdk::signer::Signer;

        let rpc_client = RpcClient::new("https://api.mainnet-beta.solana.com");
        let signer = read_keypair_file("/path/to/your/keypair.json")
            .expect("Failed to read keypair file");
        let owner = signer.pubkey();
        ```
      </Step>

      <Step>
        ### Derive PDAs and Build Instruction

        Create the `init_user_metadata` instruction linking the user to a referrer without requiring a deposit.

        ```rust theme={null}
        use klend_interface::instructions::referrer::{
            init_user_metadata,
            InitUserMetadataAccounts,
        };
        use klend_interface::pda;
        use klend_interface::KLEND_PROGRAM_ID;
        use solana_pubkey::Pubkey;
        use std::str::FromStr;

        let referrer = Pubkey::from_str("EZC9wzVCvihCsCHEMGADYdsRhcpdRYWzSCfqY").unwrap();

        // Derive PDAs
        let (user_metadata, _) = pda::user_metadata(&KLEND_PROGRAM_ID, &owner);
        let (referrer_user_metadata, _) = pda::user_metadata(&KLEND_PROGRAM_ID, &referrer);

        // Your user's address lookup table (created during onboarding)
        let user_lookup_table = Pubkey::from_str("YOUR_USER_LOOKUP_TABLE_PUBKEY").unwrap();

        let ix = init_user_metadata(
            InitUserMetadataAccounts {
                owner,
                fee_payer: owner,
                user_metadata,
                referrer_user_metadata: Some(referrer_user_metadata),
            },
            user_lookup_table,
        );
        ```

        <Warning>
          Once UserMetadata is created with a referrer, the referrer cannot be changed. Always check if UserMetadata exists before attempting to create it.
        </Warning>
      </Step>

      <Step>
        ### Send Transaction

        ```rust theme={null}
        use solana_sdk::transaction::Transaction;
        use solana_sdk::message::Message;

        let message = Message::new(&[ix], Some(&owner));
        let recent_blockhash = rpc_client.get_latest_blockhash()?;
        let tx = Transaction::new(&[&signer], message, recent_blockhash);
        let signature = rpc_client.send_and_confirm_transaction(&tx)?;
        println!("UserMetadata created with referrer! Signature: {signature}");
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

<Accordion title="When to Use This vs Deposit with Referral">
  **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
</Accordion>
