Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Native SOL Staking Support (Stake, Unstake, Withdraw)
Overview
This PR implements complete native SOL staking functionality adding support for stake, unstake, and withdraw operations through a new
useStakeReact hook.Summary of Changes
🎯 Core Features
Video demo
Google Drive link
New Files:
Modified Files:
packages/client/src/features/stake.ts- Complete staking implementationStakePrepareConfig,StakeSendOptionstypesUnstakePrepareConfig,UnstakeSendOptionstypesWithdrawPrepareConfig,WithdrawSendOptionstypesPreparedStake,PreparedUnstake,PreparedWithdrawtypescreateStakeHelper()factory with 11 methods:prepareStake()- Creates stake account and delegation instructionssendPreparedStake()- Executes stake transactionsendStake()- Convenience wrapperprepareUnstake()- Creates deactivate instructionsendPreparedUnstake()- Executes unstake transactionsendUnstake()- Convenience wrapperprepareWithdraw()- Creates withdraw instructionsendPreparedWithdraw()- Executes withdraw transactionsendWithdraw()- Convenience wrappergetStakeAccounts()- Queries stake accounts with optional validator filterpackages/client/src/controllers/stakeController.ts- State management controllerStakeControllertype with separate state management for stake/unstake/withdrawStakeInput,UnstakeInput,WithdrawInputtypescreateStakeController()with:packages/client/src/client/createClientHelpers.ts- Client wrapper integrationwrapStakeHelper()to apply default commitment levelspackages/client/src/index.ts- Export all new types and functionsStakeController,StakeInput,UnstakeInput,WithdrawInputStakeAccounttypeDependencies Added:
@solana-program/stake@^0.9.0- Official Solana stake program instruction builders@solana/react-hooksPackageModified Files:
packages/react-hooks/src/hooks.ts- React hook implementationuseStake(validatorId)hook with complete state managementstake()- Function to stake SOLunstake()- Function to unstake/deactivatewithdraw()- Function to withdraw from deactivated accountsgetStakeAccounts()- Function to query stake accountssignature,unstakeSignature,withdrawSignature- Transaction signaturesstatus,unstakeStatus,withdrawStatus- Operation stateserror,unstakeError,withdrawError- Error detailsisStaking,isUnstaking,isWithdrawing- Loading flagsreset(),resetUnstake(),resetWithdraw()- State reset functionshelper- Direct access to StakeHelpervalidatorId- Normalized validator addressgetStakeAccountsavailability with helpful error message🎨 Example Application Updates
Modified Files:
examples/react-hooks/src/components/StakePanel.tsx- Comprehensive staking UIexamples/react-hooks/src/App.tsx- Configuration updatesTechnical Implementation Details
Stake Transaction Flow
getCreateAccountInstructiongetInitializeInstructiongetDelegateStakeInstructionUnstake Transaction Flow
getDeactivateInstructionSYSVAR_CLOCKfor timingWithdraw Transaction Flow
getWithdrawInstructionSYSVAR_CLOCKandSYSVAR_STAKE_HISTORYsysvarsQuery Implementation
getProgramAccountswithmemcmpfilter at offset 44 (withdrawer authority)Architecture Patterns
Separation of Concerns
stake.ts) - Pure transaction building and RPC interactionstakeController.ts) - State management and lifecyclehooks.ts) - React integration with subscription managementStakePanel.tsx) - User interface and interactionState Management
Error Handling
Testing Considerations
Manual Testing Checklist
Breaking Changes
None - This is a purely additive feature.
Migration Guide
Not applicable - New feature with no breaking changes.
Usage Example
Screenshots
The StakePanel component provides a comprehensive UI with:
Dependencies
@solana-program/stake@^0.9.0- Official stake program instruction builders@solana-program/system@^0.9.0- System program instructions (existing)@solana/kitdependenciesDocumentation
Additional Notes