ERGO
DeFi Primitives
Advanced
1-2 weeks

Rewards Vault & Liquidity Mining Pattern

Time-based rewards distribution to LP stakers from a single vault

GitHub

문제

You want to incentivize liquidity provision by distributing reward tokens to LP stakers over time.

솔루션

A rewards vault holds reward tokens and distributes them proportionally to stakers based on their stake amount and duration. Uses accumulator pattern for gas efficiency.

작동 방식

  1. 1Protocol deposits reward tokens into vault with emission schedule
  2. 2Users stake LP tokens to receive proportional rewards
  3. 3Reward rate = totalRewards / emissionPeriod
  4. 4Each staker earns: (theirStake / totalStake) * rewardRate * timeStaked
  5. 5Accumulator pattern tracks rewards without iterating all stakers
  6. 6Users can claim rewards and unstake at any time

코드 예제

{
  // Rewards Vault with accumulator pattern
  // R4: Reward per token accumulated (scaled by 1e18)
  // R5: Last update height
  // R6: Reward rate per block
  // R7: Total staked amount
  
  val vaultNFT = SELF.tokens(0)._1
  val rewardToken = SELF.tokens(1)._1
  val rewardBalance = SELF.tokens(1)._2
  
  val rewardPerToken = SELF.R4[Long].get
  val lastUpdate = SELF.R5[Int].get
  val rewardRate = SELF.R6[Long].get
  val totalStaked = SELF.R7[Long].get
  
  // Calculate new accumulated rewards
  val blocksPassed = HEIGHT - lastUpdate
  val newRewards = if (totalStaked > 0) {
    blocksPassed * rewardRate * 1000000000000000000L / totalStaked
  } else 0L
  val newRewardPerToken = rewardPerToken + newRewards
  
  // Vault continues with updated state
  val vaultContinues = {
    val newVault = OUTPUTS(0)
    newVault.tokens(0)._1 == vaultNFT &&
    newVault.R4[Long].get == newRewardPerToken &&
    newVault.R5[Int].get == HEIGHT
  }
  
  // Validate stake/unstake/claim operations
  val validOperation = {
    // Check stake boxes in data inputs for reward calculation
    // Implementation depends on staking box structure
    true
  }
  
  vaultContinues && validOperation
}

Rewards vault using accumulator pattern. Tracks rewardPerToken to calculate individual rewards without iterating all stakers.

사용 사례

  • Liquidity mining programs
  • Protocol token distribution
  • Staking rewards
  • Yield farming incentives
  • Governance token distribution

보안 고려사항

  • !Use accumulator pattern to avoid iteration gas costs
  • !Protect against reward manipulation via flash stakes
  • !Consider minimum stake duration
  • !Audit precision and rounding
  • !Plan for reward token exhaustion

실제 구현 사례

Spectrum Finance

LP staking rewards

리소스

수수료 고려사항

Claiming rewards requires transaction. Consider batching claims or minimum claim amounts.

ErgoScript 실력을 향상시키세요

새로운 패턴, 튜토리얼, 개발자 리소스 알림을 받으세요.

Follow for daily updates