DeFi Concepts
Custom Reward System: Merkl Replacement
A comprehensive guide for developers to implement a custom reward distribution system replacing Merkl
Overview
This document outlines the implementation of a custom reward distribution system to replace Merkl in our DeFi platform. The new system aims to maintain efficiency while providing more flexibility and control over reward distribution.
Key Components
- RewardDistributor Contract: Main contract for reward calculation and distribution
- EpochManager: Manages epoch-based reward accrual
- UserRewardState: Struct to track user rewards
- PoolRewardState: Struct to track pool-level rewards
Core Functionality
1. Epoch Management
- Epochs last 7 days
flipEpoch
function transitions to the next epoch
2. Reward Accrual
updateReward
function calculates and updates rewards for a user in a specific pool
3. Reward Calculation
rewardPerToken
calculates the reward per token for a poolearned
calculates the total rewards earned by a user in a pool
4. Reward Distribution
getReward
function allows users to claim rewards from multiple pools
Integration with ve3,3 and Gauges
calculatePoolReward
determines reward allocation for a pool based on gauge weightsapplyBoost
applies the ve3,3 boost to a user’s rewardscalculateBoost
computes the boost factor based on a user’s ve3,3 position
Optimization Techniques
- Lazy Updating: Update rewards only when a user interacts with the system
- Batched Updates: Allow updating multiple pools in a single transaction
- Gas-Efficient Storage: Use packed structs and efficient data types
Implementation Steps
- Deploy
RewardDistributor
contract - Integrate with existing
VoterV3
andveNFT
contracts - Modify liquidity pool contracts to interact with
RewardDistributor
- Update frontend to use new reward claiming and viewing functions
Testing and Verification
- Unit test each component (epoch management, reward calculation, distribution)
- Integration tests with ve3,3 and gauge systems
- Stress tests with multiple users and pools
- Gas optimization tests
Upgrade Considerations
- Implement the new system as upgradeable contracts
- Plan for a migration period from Merkl to the new system
- Provide backwards compatibility for a transition period if possible