Skip to main content

Fetching Campaign Data

This guide explains how to fetch and work with campaign data using the Steer Protocol SDK.

Prerequisites

Make sure you have the SDK initialized as shown in the Implementing Reward Claims guide.

Fetching All Campaigns

To fetch all campaigns for a specific chain:

const campaigns = await steerClient.rewards.getCampaigns(chainId);

if (campaigns.success) {
campaigns.data.forEach(campaign => {
console.log('Campaign ID:', campaign.id);
console.log('Name:', campaign.name);
console.log('Start Date:', campaign.startDate);
console.log('End Date:', campaign.endDate);

// Access vault information if available
campaign.vaults?.forEach(vault => {
console.log('Vault Name:', vault.name);
console.log('Protocol:', vault.protocol);
console.log('Beacon Name:', vault.beaconName);
});
});
}

Working with Campaign Data

Filtering Active Campaigns

function getActiveCampaigns(campaigns) {
const now = new Date().getTime();
return campaigns.filter(campaign => {
const startTime = new Date(campaign.startDate).getTime();
const endTime = new Date(campaign.endDate).getTime();
return startTime <= now && endTime >= now;
});
}

const allCampaigns = await steerClient.rewards.getCampaigns(chainId);
if (allCampaigns.success) {
const activeCampaigns = getActiveCampaigns(allCampaigns.data);
console.log('Active campaigns:', activeCampaigns);
}

Checking Campaign Eligibility

async function checkCampaignEligibility(campaignId: number, userAddress: string) {
// Get campaign details
const campaigns = await steerClient.rewards.getCampaigns(chainId);
const campaign = campaigns.data?.find(c => c.id === campaignId);

if (!campaign) {
return false;
}

// Check if campaign is active
const now = new Date().getTime();
const isActive = now >= new Date(campaign.startDate).getTime() &&
now <= new Date(campaign.endDate).getTime();

if (!isActive) {
return false;
}

// Check if user has claimable rewards
const rewards = await steerClient.rewards.getRewardSummary(
userAddress as `0x${string}`,
campaign.poolAddress as `0x${string}`,
chainId,
campaignId,
campaign.rewardTokenDecimals
);

return rewards.success && rewards.data.claimable.native > 0n;
}

Error Handling

Implement proper error handling when fetching campaign data:

try {
const campaigns = await steerClient.rewards.getCampaigns(chainId);

if (!campaigns.success) {
console.error('Failed to fetch campaigns:', campaigns.error);
return;
}

if (!campaigns.data || campaigns.data.length === 0) {
console.log('No campaigns found for this chain');
return;
}

// Process campaigns...
} catch (error) {
console.error('Unexpected error fetching campaigns:', error);
}

Best Practices

  1. Cache Campaign Data
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
let campaignCache = {
data: null,
timestamp: 0
};

async function getCachedCampaigns(chainId: number) {
const now = Date.now();
if (!campaignCache.data || now - campaignCache.timestamp > CACHE_DURATION) {
const campaigns = await steerClient.rewards.getCampaigns(chainId);
if (campaigns.success) {
campaignCache = {
data: campaigns.data,
timestamp: now
};
}
}
return campaignCache.data;
}
  1. Validate Chain Support
if (!steerClient.rewards.supportsChain(chainId)) {
console.error('Chain not supported');
return;
}
  1. Regular Data Updates Implement polling or websocket connections for real-time campaign updates:
const POLLING_INTERVAL = 60000; // 1 minute

function startCampaignPolling(chainId: number) {
setInterval(async () => {
const campaigns = await getCachedCampaigns(chainId);
// Update UI or state with new campaign data
}, POLLING_INTERVAL);
}

Next Steps