Learn how to participate in yield farming programs using the Saros SDK with this comprehensive guide.
This tutorial will guide you through the process of participating in yield farming programs using the Saros SDK. Yield farming allows you to earn additional tokens as rewards for staking your LP tokens in farming pools.
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 } = 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 farming 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 farming 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 to begin farming
const farmAmount = new BN(100 * 1e6); // 100 tokens (6 decimals)
const hash = await SarosFarmService.stakePool(
connection,
payerAccount,
new PublicKey(farmParam.poolAddress),
farmAmount,
SAROS_FARM_ADDRESS,
farmParam.rewards,
new PublicKey(farmParam.lpAddress)
);
console.log(`Farming started! 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(`Tokens unstaked! Transaction hash: ${hash}`);
SarosFarmService.unStakePool
to unstake tokens from the farm.Claim the rewards you've earned from farming 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.The annual rate of return you can expect from farming, taking into account compounding.
Additional tokens distributed to farmers as incentives for providing liquidity.
Some farms may have lock-up periods during which you cannot unstake your tokens.
The potential loss of value when providing liquidity compared to holding tokens, which also affects farming.