Skip to main content

Smart Rewarder Contracts

This guide explains how to work with Smart Rewarder contracts and fetch contract addresses across different chains.

Prerequisites

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

Fetching Contract Addresses

To get the Smart Rewarder contract addresses for all supported chains:

const contracts = await steerClient.rewards.getSmartRewarderContracts();

if (contracts.success) {
contracts.data.forEach(contract => {
console.log(`Chain ID ${contract.chainId}: ${contract.address}`);
});
}

Finding a Specific Chain's Contract

To get the contract address for a specific chain:

async function getContractForChain(chainId: number) {
const contracts = await steerClient.rewards.getSmartRewarderContracts();

if (!contracts.success) {
throw new Error('Failed to fetch contracts');
}

const contract = contracts.data.find(c => c.chainId === chainId);
if (!contract) {
throw new Error(`No contract found for chain ${chainId}`);
}

return contract.address;
}

Type Definitions

The contract data follows this structure:

interface SmartRewarderContract {
chainId: number;
address: string;
}

Error Handling

Implement proper error handling when working with contracts:

try {
const contracts = await steerClient.rewards.getSmartRewarderContracts();

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

if (!contracts.data || contracts.data.length === 0) {
console.log('No contracts found');
return;
}

// Work with contract data...
} catch (error) {
console.error('Unexpected error fetching contracts:', error);
}

Best Practices

  1. Cache Contract Addresses
const CONTRACT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours
let contractCache = {
data: null,
timestamp: 0
};

async function getCachedContracts() {
const now = Date.now();
if (!contractCache.data || now - contractCache.timestamp > CONTRACT_CACHE_DURATION) {
const contracts = await steerClient.rewards.getSmartRewarderContracts();
if (contracts.success) {
contractCache = {
data: contracts.data,
timestamp: now
};
}
}
return contractCache.data;
}
  1. Validate Chain Support
async function isChainSupported(chainId: number) {
const contracts = await steerClient.rewards.getSmartRewarderContracts();
return contracts.success && contracts.data.some(c => c.chainId === chainId);
}
  1. Contract Address Validation
function isValidContractAddress(address: string) {
return /^0x[a-fA-F0-9]{40}$/.test(address);
}

Common Use Cases

Setting Up Contract Interaction

async function setupContractInteraction(chainId: number) {
// Get the contract address
const contracts = await steerClient.rewards.getSmartRewarderContracts();
const contract = contracts.data?.find(c => c.chainId === chainId);

if (!contract) {
throw new Error(`No contract found for chain ${chainId}`);
}

// Now you can use the contract address for interactions
return contract.address;
}

Multi-Chain Support

async function getSupportedChains() {
const contracts = await steerClient.rewards.getSmartRewarderContracts();

if (!contracts.success) {
return [];
}

return contracts.data.map(contract => contract.chainId);
}

Next Steps