Learn how to stake tokens and earn rewards using the Saros SDK with this comprehensive guide.
This tutorial will guide you through the process of staking LP tokens in farms and claiming rewards using the Saros SDK. Staking allows you to earn additional tokens as rewards for supporting the protocol.
Before starting this tutorial, ensure you have the following:
// Install the required packages
npm install @saros-finance/sdk @solana/web3.js
// Import the necessary modules
import sarosSdk, { genConnectionSolana } from '@saros-finance/sdk';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
// Extract the services we need
const { SarosFarmService, SarosStakeServices } = sarosSdk;
First, we need to establish a connection to the Solana network and configure our wallet.
// Generate a connection to the Solana network
const connection = genConnectionSolana();
// Define your wallet address (in a real app, this would come from a connected wallet)
const accountSol = 'YOUR_WALLET_PUBLIC_KEY';
// Define the payer account object
const payerAccount = { publicKey: new PublicKey(accountSol) };
Set up the farm parameters for staking operations. You'll need to know the farm address and reward information.
// Configure the farm parameters
const SAROS_FARM_ADDRESS = new PublicKey('SFarmWM5wLFNEw1q5ofqL7CrwBMwdcqQgK6oQuoBGZJ');
const farmParam = {
lpAddress: 'HVUeNVH93PAFwJ67ENJwPWFU9cWcM57HEAmkFLFTcZkj',
poolAddress: 'FW9hgAiUsFYpqjHaGCGw4nAvejz4tAp9qU7kFpYr1fQZ',
poolLpAddress: '2wUvdZA8ZsY714Y5wUL9fkFmupJGGwzui2N74zqJWgty',
rewards: [
{
address: 'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
poolRewardAddress: 'AC3FyChJwuU7EY9h4BqzjcN8CtGD7YRrAbeRdjcqe1AW',
rewardPerBlock: 6600000,
rewardTokenAccount: 'F6aHSR3ChwCXD67wrX2ZBHMkmmU9Gfm9QQmiTBrKvsmJ',
id: 'coin98'
},
],
token0: 'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
token1: 'EPjFWdd5AufqSSqeM2qN1zzybapC8G4wEGGkZwyTDt1v',
token0Id: 'coin98',
token1Id: 'usd-coin'
};
Retrieve a list of available farms to see what staking opportunities are available.
// Get a list of available farms
try {
const response = await SarosFarmService.getListPool({page: 1, size: 10});
console.log('Available Farms:', response);
return response;
} catch(err) {
console.error('Error fetching farms:', err);
return [];
}
Stake your LP tokens in a farm to begin earning rewards.
// Stake LP tokens in a farm
const stakeAmount = new BN(100 * 1e6); // 100 tokens (6 decimals)
const hash = await SarosFarmService.stakePool(
connection,
payerAccount,
new PublicKey(farmParam.poolAddress),
stakeAmount,
SAROS_FARM_ADDRESS,
farmParam.rewards,
new PublicKey(farmParam.lpAddress)
);
console.log(`Staking successful! Transaction hash: ${hash}`);
SarosFarmService.stakePool
to stake tokens in the farm.Unstake your LP tokens from a farm when you want to stop earning rewards.
// Unstake LP tokens from a farm
const unStakeAmount = new BN(50 * 1e6); // 50 tokens (6 decimals)
const hash = await SarosFarmService.unStakePool(
connection,
payerAccount,
new PublicKey(farmParam.poolAddress),
new PublicKey(farmParam.lpAddress),
unStakeAmount,
SAROS_FARM_ADDRESS,
farmParam.rewards,
false // Set to true if want to unstake full balance
);
console.log(`Unstaking successful! Transaction hash: ${hash}`);
SarosFarmService.unStakePool
to unstake tokens from the farm.Claim the rewards you've earned from staking your LP tokens.
// Claim rewards from a farm
const poolRewardAddress = farmParam.rewards[0].poolRewardAddress;
const mintAddress = farmParam.rewards[0].address;
const hash = await SarosFarmService.claimReward(
connection,
payerAccount,
new PublicKey(poolRewardAddress),
new PublicKey(SAROS_FARM_ADDRESS),
new PublicKey(mintAddress)
);
console.log(`Rewards claimed! Transaction hash: ${hash}`);
SarosFarmService.claimReward
to claim your rewards.