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

# Farms

> Learn how to build with Kamino Farms, the rewards distribution layer for Kamino products

## Overview

Kamino Farms is the rewards distribution layer for Kamino products. Farms distribute incentive tokens to depositors, suppliers, and borrowers across the protocol, including KMNO emissions on vaults and lending positions. The TypeScript SDK provides access to farm and user state, along with instructions to stake, unstake, and harvest rewards.

<Note>
  Farms attached to lending positions and kvaults are **delegated**: position accounting lives in the parent program (klend or kvault), and unstake is a two-step flow gated by a cooldown. Read [Farm rewards](/curators/vaults/concepts/yield-and-fees#farm-rewards) for the conceptual model before integrating.
</Note>

## Key Components of Farms Integration

<CardGroup cols={3}>
  <Card title="Farm State" icon="database">
    Decode a farm's reward tokens, per-second emission rates, total staked amount, cooldown period, and scope-oracle binding.
  </Card>

  <Card title="User Positions" icon="user">
    Read a wallet's stake across one or many farms, pending rewards per reward token, and pending-unstake cooldown status.
  </Card>

  <Card title="Stake & Claim" icon="hand-holding-dollar">
    Initialize user state, stake / unstake (with cooldown handling), and harvest accrued rewards in one or all reward tokens.
  </Card>
</CardGroup>

## Locating a Farm Address

The SDK reads any farm given its pubkey. Three places that pubkey can come from:

| Source                   | Where to look                                                                                           |
| ------------------------ | ------------------------------------------------------------------------------------------------------- |
| **CDN**                  | `https://cdn.kamino.finance/resources.json`, under `mainnet-beta.extraFarms`.                           |
| **Klend reserve fields** | A reserve account's `state.farmCollateral` (supply side) or `state.farmDebt` (borrow side) field.       |
| **Kvault state**         | A kvault account's `state.vaultFarm` field. List vaults at `https://api.kamino.finance/kvaults/vaults`. |

## Integration Options

<CardGroup cols={2}>
  <Card title="TypeScript SDK" icon="code">
    On-chain reads and transaction building for stake, unstake, harvest, and user-state management. TS/JS only.
  </Card>
</CardGroup>

### Stake

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    import { createSolanaRpc, address } from '@solana/kit';
    import { Farms, FarmState } from '@kamino-finance/farms-sdk';
    import { getScopePricesFromFarm } from '@kamino-finance/farms-sdk/dist/utils/option';
    import Decimal from 'decimal.js';

    const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
    const farms = new Farms(rpc);

    const FARM = address('9wSacmF3KBr4HmgncxXeBhDdw4Shi2X9ETAFzWJS6pG6');
    const STAKE_MINT = address('3EmfhZ3KUaYdwKZ9CwfGoKRqQqUV1MhSCVL5Ch7szX7e');

    const farmState = await FarmState.fetch(rpc, FARM);
    if (!farmState) console.log(`Farm not found: ${FARM}`);

    const scopePrices = getScopePricesFromFarm(farmState);
    const amountLamports = new Decimal(0.02).mul(
      new Decimal(10).pow(farmState.token.decimals.toNumber()),
    );

    // Refresh the farm so reward accounting is current, then stake.
    const instructions = [
      await farms.refreshFarmIx(FARM, scopePrices),
      await farms.stakeIx(signer, FARM, amountLamports, STAKE_MINT, scopePrices),
    ];

    console.log('Stake Instructions:', instructions);
    ```

    <a href="https://github.com/Kamino-Finance/farms-sdk" target="_blank" rel="noopener noreferrer" class="github-link">
      <Icon icon="github" iconType="brands" size={16} />

      <span>View Code</span>
    </a>
  </Tab>
</Tabs>

<Note>
  For the full stake flow including first-time user-state creation and transaction confirmation, see [Stake](/build/developers/rewards/operations/stake).
</Note>

### Unstake

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    import { createSolanaRpc, address } from '@solana/kit';
    import { Farms, FarmState } from '@kamino-finance/farms-sdk';
    import { getScopePricesFromFarm } from '@kamino-finance/farms-sdk/dist/utils/option';
    import { WAD } from '@kamino-finance/farms-sdk/dist/utils/utils';
    import Decimal from 'decimal.js';

    const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
    const farms = new Farms(rpc);

    const FARM = address('9wSacmF3KBr4HmgncxXeBhDdw4Shi2X9ETAFzWJS6pG6');

    const farmState = await FarmState.fetch(rpc, FARM);
    if (!farmState) console.log(`Farm not found: ${FARM}`);

    const scopePrices = getScopePricesFromFarm(farmState);
    const { key: userStateKey } =
      await farms.getUserStateKeyForUndelegatedFarm(signer.address, FARM);

    // unstakeIx takes WAD-scaled shares. Refresh the farm and user, then unstake.
    const amountScaled = new Decimal(0.02)
      .mul(new Decimal(10).pow(farmState.token.decimals.toNumber()))
      .mul(WAD);

    const instructions = [
      await farms.refreshFarmIx(FARM, scopePrices),
      await farms.refreshUserIx(userStateKey, FARM, scopePrices),
      await farms.unstakeIx(signer, FARM, amountScaled, scopePrices),
    ];

    console.log('Unstake Instructions:', instructions);
    ```

    <a href="https://github.com/Kamino-Finance/farms-sdk" target="_blank" rel="noopener noreferrer" class="github-link">
      <Icon icon="github" iconType="brands" size={16} />

      <span>View Code</span>
    </a>
  </Tab>
</Tabs>

<Note>
  Unstaking starts the withdrawal cooldown. After it elapses, withdraw the tokens back to your wallet. See [Unstake](/build/developers/rewards/operations/unstake).
</Note>

## What's Covered

<CardGroup cols={2}>
  <Card title="Farms" icon="database" href="/build/developers/rewards/data/farm-state">
    Read farm state, discover farms, and compute or simulate incentive APY.
  </Card>

  <Card title="User Position Data" icon="user" href="/build/developers/rewards/data/user-position">
    Fetch a wallet's stake, lockup and cooldown status, and pending rewards.
  </Card>

  <Card title="Stake" icon="arrow-down-to-line" href="/build/developers/rewards/operations/stake">
    Initialize user state and stake share tokens into a farm.
  </Card>

  <Card title="Unstake" icon="arrow-up-from-line" href="/build/developers/rewards/operations/unstake">
    Unstake into pending withdrawal and start the cooldown.
  </Card>

  <Card title="Withdraw" icon="wallet" href="/build/developers/rewards/operations/withdraw-unstaked">
    Move unstaked tokens back to your wallet after the cooldown.
  </Card>

  <Card title="Harvest" icon="hand-holding-dollar" href="/build/developers/rewards/operations/harvest">
    Claim all pending reward tokens in one transaction.
  </Card>
</CardGroup>

## Relevant Links

<CardGroup cols={2}>
  <Card title="Farms SDK repository" icon="github" href="https://github.com/Kamino-Finance/farms-sdk">
    TypeScript SDK source, on-chain program docs, and example scripts.
  </Card>

  <Card title="KFarms program" icon="github" href="https://github.com/Kamino-Finance/kfarms">
    Rust source for the on-chain farms program.
  </Card>

  <Card title="NPM SDK package" icon="npm" href="https://www.npmjs.com/package/@kamino-finance/farms-sdk">
    Install the TypeScript SDK to integrate reward operations.
  </Card>

  <Card title="Farm Rewards Concepts" icon="book-open" href="/curators/vaults/concepts/yield-and-fees#farm-rewards">
    Vault Farm, Autocompound, Insurance Pool farm, and cooldowns explained.
  </Card>
</CardGroup>
