Skip to main content

Unstake

Unstaking moves stake from active into pending withdrawal and starts the farm’s withdrawal cooldown. After the cooldown elapses, call Withdraw to move the tokens back to your wallet. Refresh first, then unstake.
This flow requires an existing position on a non-delegated farm. The wallet needs SOL for fees.
1

Import Dependencies

Import the packages for Solana RPC communication, Kit transaction building, and the Farms SDK. UserState and WAD are used to scale the unstake amount.
2

Configure Constants and Initialize

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

Load State and Scale the Amount

Fetch the farm and the user state, then convert the unstake amount to WAD-scaled shares and clamp it to the on-chain active stake.
unstakeIx takes WAD-scaled shares, not raw lamports like stakeIx. Scale with UNSTAKE_AMOUNT * 10^decimals * WAD, then clamp to userState.activeStakeScaled so you never request more than is staked. The clamp also handles dust and an “unstake all” request.
4

Build Unstake Instructions

Refresh the farm and user so reward accounting is current, then unstake.
5

Build, Sign, and Send Transaction

Compose the v0 transaction, sign, verify it fits the size limit, then send and confirm.
The unstake is complete. Your stake is now pending withdrawal and cooling down. Once the farm’s withdrawal cooldown elapses, withdraw it back to your wallet.

Full Code Example