Reward Distribution

Learn how to implement reward distribution in your liquid staking protocol.

Last updated: 2024-03-21
Edit on GitHub

Reward Distribution Overview

Efficient reward distribution is crucial for a liquid staking protocol. This guide covers reward collection, calculation, and distribution mechanisms to ensure fair and transparent staking rewards for all participants.

Key Features

  • Automated reward collection
  • Fair distribution mechanism
  • Protocol fee management
  • Reward compounding

Benefits

  • Transparent reward tracking
  • Efficient distribution
  • Automatic compounding
  • Fair fee structure

Implementation Guide

Learn how to implement a secure and efficient reward distribution system.

Reward Distribution Implementation

1pub struct RewardDistributor {
2    /// Distribution frequency (epochs)
3    pub distribution_frequency: u64,
4    
5    /// Last distribution epoch
6    pub last_distribution: u64,
7    
8    /// Minimum distribution amount
9    pub min_distribution: u64,
10    
11    /// Fee schedule
12    pub fee_schedule: FeeSchedule,
13}
14
15impl RewardDistributor {
16    pub fn distribute_rewards(
17        &mut self,
18        stake_pool: &mut StakePool,
19        total_rewards: u64,
20    ) -> ProgramResult {
21        let current_epoch = Clock::get()?.epoch;
22        
23        // Check if distribution is needed
24        if current_epoch.saturating_sub(self.last_distribution)
25            < self.distribution_frequency {
26            return Ok(());
27        }
28        
29        // Check minimum distribution
30        if total_rewards < self.min_distribution {
31            return Ok(());
32        }
33        
34        // Calculate fees
35        let fees = self.fee_schedule.calculate_reward_fees(total_rewards)?;
36        let protocol_fee = fees.protocol_fee;
37        let treasury_fee = fees.treasury_fee;
38        
39        // Calculate net rewards
40        let net_rewards = total_rewards
41            .checked_sub(protocol_fee)?
42            .checked_sub(treasury_fee)?;
43            
44        // Update exchange rate
45        stake_pool.exchange_rate.update_with_rewards(
46            net_rewards,
47            current_epoch,
48        )?;
49        
50        // Transfer fees
51        self.transfer_protocol_fee(protocol_fee)?;
52        self.transfer_treasury_fee(treasury_fee)?;
53        
54        // Update distribution state
55        self.last_distribution = current_epoch;
56        
57        // Emit distribution event
58        emit!(RewardsDistributed {
59            stake_pool: stake_pool.pubkey(),
60            total_rewards,
61            net_rewards,
62            protocol_fee,
63            treasury_fee,
64            epoch: current_epoch,
65        });
66        
67        Ok(())
68    }
69}
Best Practices
  • Implement proper validation and error handling
  • Use checked math operations to prevent overflows
  • Maintain transparent fee structures
  • Include comprehensive event logging
  • Consider implementing reward smoothing mechanisms