Configure the policy
Use a loadedvault, connection, and manager Keypair. Read farmState, underlyingMint, and rewardMint from the selected Kamino Farm configuration. Use its reward ordering to select the later rewardIndex.
Omitting a filter or passing an empty list removes its restriction. Include each reward mint you intend to collect.
Prepare the farm position
For a direct farm, initialize its user state withkaminoFarmAction.initializeUser({ farmState }) through the installed initialization permission. The default delegatee is vault.state.squadsVault. With manual sync construction, use autoManagePositions: false for this initialization, confirm it, refresh AUM, and reload before registration.
Register the existing user state before staking:
Stake and collect rewards
This stakes1_000_000 underlying-token units. Use the mint’s decimals when converting a human amount.
- Transaction builder
- Sync helpers
build() returns an execution plan: ordered transactionSets containing instructions, additional signers, and lookup table addresses. The builder schedules supported setup and refreshes; send() simulates and submits the sequence.Unstake, wait, and withdraw
unstake uses scaled shares from the farm user’s live state, rather than an underlying-token amount. Read UserState for activeStakeScaled, pendingWithdrawalUnstakeScaled, and pendingWithdrawalUnstakeTs; read FarmState for timeUnit and withdrawalCooldownPeriod. The Farms SDK’s public getUserStateDecoder() and getFarmStateDecoder() read the buffers returned by connection.getMultipleAccountsInfo([userState, farmState]).
Convert the selected scaled share amount to BN for stakeSharesScaled. For withdrawals, compare the stored deadline against Unix seconds when timeUnit is 0, or the current slot when it is 1: