Skip to main content

Implementing Reward Claims

This guide walks you through implementing reward claims 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. Check Available Rewards

Before claiming, check the user's reward summary:

const rewardSummary = await steerClient.rewards.getRewardSummary(
userAddress as `0x${string}`,
poolAddress as `0x${string}`,
chainId,
campaignId,
rewardTokenDecimals
);

if (rewardSummary.success) {
console.log('Total earned:', rewardSummary.data.totalEarned.formatted);
console.log('Already claimed:', rewardSummary.data.totalClaimed.formatted);
console.log('Available to claim:', rewardSummary.data.claimable.formatted);
}

3. Implementing Claims

You have several options for implementing claims:

Option 1: Simple Claim Implementation

The easiest way to implement claims:

const claimTx = await steerClient.rewards.claimCampaignRewards(
userAddress as `0x${string}`,
chainId,
campaignId
);

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

Option 2: Prepare Claim Data for Custom UI

If you need to handle the contract interaction yourself:

const preparedClaim = await steerClient.rewards.prepareClaimRewardsCampaign(
getAddress(userAddress),
chainId,
parseInt(campaignId)
);

if (preparedClaim.success) {
const { address, abi, functionName, args } = preparedClaim.data;
// Use these parameters with your own contract interaction logic
}

Option 3: Manual Claim Process

For complete control over the claim process:

// 1. Get claim proofs
const proofs = await steerClient.rewards.getClaimProofs(
userAddress as `0x${string}`,
chainId,
campaignId
);

// 2. Get rewarder contract
const rewarders = await steerClient.rewards.getSmartRewarderContracts();
const rewarder = rewarders.data?.find(r => r.chainId === chainId);

if (proofs.data && rewarder) {
// 3. Prepare claim data
const claimData = steerClient.rewards.prepareClaimData(proofs.data);
claimData.rewarderAddress = rewarder.address;

// 4. Execute claim
const tx = await steerClient.rewards.claimReward(claimData);
}

Error Handling

Always implement proper error handling:

try {
const claimTx = await steerClient.rewards.claimCampaignRewards(
userAddress as `0x${string}`,
chainId,
campaignId
);

if (!claimTx.success) {
console.error('Claim failed:', claimTx.error);
// Handle error appropriately
return;
}

// Handle successful claim
console.log('Claim successful:', claimTx.data);
} catch (error) {
console.error('Unexpected error:', error);
// Handle unexpected errors
}

Best Practices

  1. Check Chain Support
if (!steerClient.rewards.supportsChain(chainId)) {
console.error('Chain not supported');
return;
}
  1. Verify Claimable Amount Always check if there are rewards to claim before initiating a claim transaction.

  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

  • Learn about Creating Campaigns
  • Explore reward distribution mechanisms
  • Set up monitoring and analytics