Yield Farming Example

Learn how to participate in yield farming programs using the Saros SDK with this complete example.

Farming Operations Implementation
Complete code example for yield farming
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 farmTokens() {
  try {
    const farmAmount = new BN(100 * 1e6); // 100 LP tokens

    const hash = await SarosFarmService.stakePool(
      connection,
      payerAccount,
      new PublicKey(farmParams.poolAddress),
      farmAmount,
      SAROS_FARM_ADDRESS,
      farmParams.rewards,
      new PublicKey(farmParams.lpAddress)
    );

    console.log('Farming started! Transaction hash:', hash);
    return { success: true, hash };
  } catch (error) {
    console.error('Farming failed:', error);
    return { success: false, message: error.message };
  }
}

async function unStakeTokens() {
  try {
    const unStakeAmount = new BN(50 * 1e6); // 50 LP tokens

    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('Tokens unstaked! 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
farmTokens().then(result => {
  if (result.success) {
    console.log("Farming started with hash:", result.hash);
  } else {
    console.log("Farming 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);
  }
});
Run Example
Simulate the farming operations

This simulation demonstrates the expected behavior of farming, unstaking, and claiming rewards. In a real application, this would connect to the Solana network.

Output
Results of the simulation
Click 'Run Farming Simulation' to see the output here...
Key Components

SarosFarmService

The main service for interacting with Saros farms and yield farming programs.

Stake Tokens

SarosFarmService.stakePool stakes LP tokens in a farming pool to begin earning rewards.

Unstake Tokens

SarosFarmService.unStakePool unstakes LP tokens from a farming pool.

Claim Rewards

SarosFarmService.claimReward claims accumulated rewards from a farming pool.