Skip to main content

Stake

Stake a farm’s share token to start earning rewards. Refresh the farm (and the user, if a position already exists) so reward accounting is current, then stake.
This flow requires a non-delegated farm. stakeIx needs a stakeTokenMint, which delegated farms (klend reserve and kvault farms) do not have. The wallet needs SOL for fees plus the farm’s stake token to stake.
1

Import Dependencies

Import the packages for Solana RPC communication, Kit transaction building, the Farms SDK, and the keypair loader.
2

Configure Constants and Initialize

Set the keypair path, RPC endpoints, target farm, stake token mint, and amount. Load the signer and initialize the farms client.
3

Load Farm State

Fetch the farm account, read its Scope prices for the refresh instructions, and convert the stake amount to raw token units.
stakeIx takes the raw lamport amount (the share amount times 10^decimals). The on-chain program scales it up internally.
4

Build Stake Instructions

Check whether the wallet already has a user-state account, then assemble the instruction list: create the user state if needed, refresh, and stake.
The Farms SDK exposes bare instruction builders that do not bundle a refresh, unlike klend. Prepend refreshFarmIx (and refreshUserIx for an existing position) so reward accounting is current before you stake. A first-time staker has no user-state account yet, so createNewUserIx creates it in the same transaction.
5

Build, Sign, and Send Transaction

Compose the v0 transaction, sign, verify it fits the size limit, then send and confirm.
The stake is complete. Your shares are staked, and rewards accrue once any deposit warmup period elapses.

Full Code Example