Learn how to stake tokens and claim rewards using the Saros SDK with this complete example.
import sarosSdk, { genConnectionSolana } from '@saros-finance/sdk';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
const { SarosFarmService } = sarosSdk;
const connection = genConnectionSolana();
const payerAccount = { publicKey: new PublicKey('YOUR_WALLET_PUBLIC_KEY') };
const SAROS_FARM_ADDRESS = new PublicKey('SFarmWM5wLFNEw1q5ofqL7CrwBMwdcqQgK6oQuoBGZJ');
const farmParams = {
poolAddress: 'FW9hgAiUsFYpqjHaGCGw4nAvejz4tAp9qU7kFpYr1fQZ',
lpAddress: 'HVUeNVH93PAFwJ67ENJwPWFU9cWcM57HEAmkFLFTcZkj',
rewards: [{
address: 'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
poolRewardAddress: 'AC3FyChJwuU7EY9h4BqzjcN8CtGD7YRrAbeRdjcqe1AW',
rewardPerBlock: 6600000,
rewardTokenAccount: 'F6aHSR3ChwCXD67wrX2ZBHMkmmU9Gfm9QQmiTBrKvsmJ',
}]
};
async function stakeTokens() {
try {
const stakeAmount = new BN(100 * 1e6); // 100 tokens (6 decimals)
const hash = await SarosFarmService.stakePool(
connection,
payerAccount,
new PublicKey(farmParams.poolAddress),
stakeAmount,
SAROS_FARM_ADDRESS,
farmParams.rewards,
new PublicKey(farmParams.lpAddress)
);
console.log('Staking successful! Transaction hash:', hash);
return { success: true, hash };
} catch (error) {
console.error('Staking failed:', error);
return { success: false, message: error.message };
}
}
async function unStakeTokens() {
try {
const unStakeAmount = new BN(50 * 1e6); // 50 tokens (6 decimals)
const hash = await SarosFarmService.unStakePool(
connection,
payerAccount,
new PublicKey(farmParams.poolAddress),
new PublicKey(farmParams.lpAddress),
unStakeAmount,
SAROS_FARM_ADDRESS,
farmParams.rewards,
false // Set to true if want to unstake full balance
);
console.log('Unstaking successful! Transaction hash:', hash);
return { success: true, hash };
} catch (error) {
console.error('Unstaking failed:', error);
return { success: false, message: error.message };
}
}
async function claimRewards() {
try {
const poolRewardAddress = farmParams.rewards[0].poolRewardAddress;
const mintAddress = farmParams.rewards[0].address;
const hash = await SarosFarmService.claimReward(
connection,
payerAccount,
new PublicKey(poolRewardAddress),
SAROS_FARM_ADDRESS,
new PublicKey(mintAddress)
);
console.log('Rewards claimed! Transaction hash:', hash);
return { success: true, hash };
} catch (error) {
console.error('Claim failed:', error);
return { success: false, message: error.message };
}
}
// Execute the operations
stakeTokens().then(result => {
if (result.success) {
console.log("Tokens staked with hash:", result.hash);
} else {
console.log("Staking failed:", result.message);
}
});
unStakeTokens().then(result => {
if (result.success) {
console.log("Tokens unstaked with hash:", result.hash);
} else {
console.log("Unstaking failed:", result.message);
}
});
claimRewards().then(result => {
if (result.success) {
console.log("Rewards claimed with hash:", result.hash);
} else {
console.log("Claim failed:", result.message);
}
});
This simulation demonstrates the expected behavior of staking, unstaking, and claiming rewards. In a real application, this would connect to the Solana network.
Click 'Run Staking Simulation' to see the output here...
The main service for interacting with Saros farms and staking pools.
SarosFarmService.stakePool
stakes LP tokens in a farm.
SarosFarmService.unStakePool
unstakes LP tokens from a farm.
SarosFarmService.claimReward
claims accumulated rewards.