Skip to main content

Implementing Staking

This guide walks you through implementing staking functionality using the Steer Protocol SDK.

Prerequisites

Before you begin, make sure you have:

  • Installed the required packages:
npm install @steerprotocol/sdk viem
# or
yarn add @steerprotocol/sdk viem

Implementation Steps

1. Initialize the SDK Client

First, set up the required clients for the SDK:

import { SteerClient, SteerConfig } from '@steerprotocol/sdk';
import { createPublicClient, createWalletClient, http } from 'viem';
import { polygon } from 'viem/chains';

// Create public client for blockchain interaction
const publicClient = createPublicClient({
chain: polygon,
transport: http()
});

// Create wallet client for transactions
const walletClient = createWalletClient({
chain: polygon,
transport: http()
});

// Initialize the Steer client
const steerConfig: SteerConfig = {
environment: 'production',
client: publicClient,
walletClient
};

const steerClient = new SteerClient(steerConfig);

2. Fetching Staking Pools

You can fetch staking pools with various filters:

// Get all active staking pools
const activePools = await steerClient.staking.getLiveStakingPools();

// Get pools for specific chain
const chainPools = await steerClient.staking.getStakingPools({
chainId: 137 // Polygon
});

// Get dual rewards pools
const dualRewardPools = await steerClient.staking.getDualRewardsStakingPools();

// Get high yield pools
const highYieldPools = await steerClient.staking.getHighYieldStakingPools(
1000, // Minimum daily emission for reward A
500 // Minimum daily emission for reward B
);

3. Staking Tokens

To stake tokens in a pool:

const stakeTx = await steerClient.staking.stake({
stakingPool: poolAddress,
amount: BigInt('1000000000000000000') // 1 token with 18 decimals
});

if (stakeTx.success) {
console.log('Stake transaction hash:', stakeTx.data);
}

4. Checking Balances and Rewards

Monitor staked balances and earned rewards:

// Check staked balance
const balance = await steerClient.staking.balanceOf(
poolAddress,
userAddress
);

// Check earned rewards
const rewards = await steerClient.staking.earned(
poolAddress,
userAddress
);

if (rewards.success) {
console.log('Reward A:', rewards.data.rewardA.toString());
if (rewards.data.rewardB) {
console.log('Reward B:', rewards.data.rewardB.toString());
}
}

5. Claiming Rewards

To claim earned rewards:

const claimTx = await steerClient.staking.getReward({
stakingPool: poolAddress
});

if (claimTx.success) {
console.log('Claim transaction hash:', claimTx.data);
}

6. Withdrawing Tokens

To withdraw staked tokens:

const withdrawTx = await steerClient.staking.withdraw({
stakingPool: poolAddress,
amount: BigInt('1000000000000000000') // 1 token with 18 decimals
});

if (withdrawTx.success) {
console.log('Withdraw transaction hash:', withdrawTx.data);
}

Advanced Features

Calculating APR

Calculate the APR for a staking pool:

const apr = steerClient.staking.calculateAPR(
pool,
rewardTokenPriceUSD,
totalStakedUSD
);
console.log('Pool APR:', apr);

Getting LP Token Values

For Steer vault staking pools, get the underlying token values:

const tokenValue = await steerClient.staking.getSteerLpTokenValue({
stakingPool: pool,
amount: lpTokenAmount,
token0UsdPrice: price0,
token1UsdPrice: price1
});

if (tokenValue.success) {
console.log('Total Value USD:', tokenValue.data.totalValueUsd);
console.log('Token 0 Value:', tokenValue.data.token0Value);
console.log('Token 1 Value:', tokenValue.data.token1Value);
}

Getting Total Staked Value

Get the total value staked in a pool:

const totalStaked = await steerClient.staking.getTotalStakingAmountInUsd(
pool,
token0Price,
token1Price // Required for Steer vaults
);

if (totalStaked.success) {
console.log('Total Staked USD:', totalStaked.data);
}

Error Handling

Always implement proper error handling:

try {
const stakeTx = await steerClient.staking.stake({
stakingPool: poolAddress,
amount: stakeAmount
});

if (!stakeTx.success) {
console.error('Stake failed:', stakeTx.error);
return;
}

console.log('Stake successful:', stakeTx.data);
} catch (error) {
console.error('Unexpected error:', error);
}

Best Practices

  1. Validate Chain Support
const chainId = await publicClient.getChainId();
if (chainId !== pool.chainId) {
console.error('Chain not supported');
return;
}
  1. Check Balances Before Actions Always verify user has sufficient balance before staking.

  2. Monitor Transaction Status Implement proper transaction monitoring and user feedback.

  3. Handle Failed Transactions Implement proper error handling and user feedback for failed transactions.

Next Steps