Marinade Native Staking SDK
This is SDK for interaction with Marinade Native Staking product.
Staking through this product is done by changing the stake authority
of user's stake account(s) for example through this SDK.
Unstaking can be always done by user manually through Solana CLI - but that would be tedious as Marinade splits the user stake accounts into 100+ stake accounts. Marinade accepts requests from users to unstake part or all stake accounts and merge them into a single one, so the user flow is simplified (but Marinade charges a small fee). This is also handled by the SDK.
Overview
Install and intialize the SDK
Install the package:
pnpm i @marinade.finance/native-staking-sdk
Initialize the SDK:
import { Connection } from '@solana/web3.js'
import { NativeStakingConfig, NativeStakingSDK } from '@marinade.finance/native-staking-sdk'
const config = new NativeStakingConfig({ connection: new Connection('...') })
const sdk = new NativeStakingSDK(config)
Stake SOL through Marinade Native Staking
const { connection } = useConnection()
const { sendTransaction, signTransaction, publicKey } = useWallet()
const amount = new BN('1000000000')
const { createAuthorizedStake, stakeKeypair } = sdk.buildCreateAuthorizedStakeInstructions(publicKey, amount)
const { blockhash } = await connection.getLatestBlockhash()
const versionedMessage = ;
const tx = new VersionedTransaction(new TransactionMessage({
payerKey: publicKey,
recentBlockhash: blockhash,
instructions: createAuthorizedStake,
}).compileToV0Message())
tx.sign([stakeKeypair])
await signTransaction(tx)
await sendTransaction(tx, connection)
Migrate Stake accounts to Marinade Native Staking
const { connection } = useConnection()
const { sendTransaction, signTransaction, publicKey } = useWallet()
const stakeAccount = new PublicKey('...')
const authorizeInstructions = sdk.buildAuthorizeInstructions(publicKey, [stakeAccount])
const { blockhash } = await connection.getLatestBlockhash()
const versionedMessage = ;
const tx = new VersionedTransaction(new TransactionMessage({
payerKey: publicKey,
recentBlockhash: blockhash,
instructions: authorizeInstructions,
}).compileToV0Message())
await signTransaction(tx)
await sendTransaction(tx, connection)
Prepare for unstake
const { connection } = useConnection()
const { sendTransaction, signTransaction, publicKey } = useWallet()
const amount = new BN('1000000000')
const { payFees, onPaid } = await sdk.initPrepareForRevoke(publicKey, amount)
const { blockhash } = await connection.getLatestBlockhash()
const tx = new VersionedTransaction(new TransactionMessage({
payerKey: publicKey,
recentBlockhash: blockhash,
instructions: payFees,
}).compileToV0Message())
await signTransaction(tx)
const signature = await sendTransaction(tx, connection)
await connection.confirmTransaction(signature, 'finalized')
await onPaid(signature)
Fetch user's stake accounts in Marinade Native Staking
const { publicKey } = useWallet()
await sdk.getStakeAccounts(publicKey)
Show user's rewards
const { publicKey } = useWallet()
await sdk.fetchRewards(publicKey)