Reward

Git Source

Inherits: IReward, ReentrancyGuard

Author: velodrome.finance, @figs999, @pegahcarter

Base implementation for reward distribution contracts

Abstract contract providing core reward distribution functionality

State Variables

DURATION

Duration of each reward epoch in seconds

uint256 public constant DURATION = 7 days;

voter

Address of the Voter contract that manages rewards

address public immutable voter;

ve

Address of the VotingEscrow contract that manages veNFTs

address public immutable ve;

authorized

Address permitted to call privileged state-changing functions

address public immutable authorized;

totalSupply

Total amount of staking tokens locked in contract

uint256 public totalSupply;

supplyNumCheckpoints

Total number of supply checkpoints recorded

uint256 public supplyNumCheckpoints;

rewards

List of all reward tokens supported by this contract

Used for token enumeration and management

address[] public rewards;

balanceOf

Retrieves current staked balance for a veNFT

mapping(uint256 => uint256) public balanceOf;

tokenRewardsPerEpoch

Gets reward amount allocated for a specific epoch

mapping(address => mapping(uint256 => uint256)) public tokenRewardsPerEpoch;

lastEarn

Retrieves timestamp of last reward claim for a veNFT

mapping(address => mapping(uint256 => uint256)) public lastEarn;

isReward

Checks if a token is configured as a reward token

mapping(address => bool) public isReward;

checkpoints

A record of balance checkpoints for each account, by index

mapping(uint256 => mapping(uint256 => Checkpoint)) public checkpoints;

numCheckpoints

Number of balance checkpoints for a veNFT

mapping(uint256 => uint256) public numCheckpoints;

supplyCheckpoints

A record of balance checkpoints for each token, by index

mapping(uint256 => SupplyCheckpoint) public supplyCheckpoints;

Functions

constructor

Initializes reward contract with voter address

constructor(address _voter);

Parameters

NameTypeDescription
_voteraddressAddress of voter contract managing rewards

getPriorBalanceIndex

Gets historical balance index for a veNFT at timestamp

Uses binary search to find checkpoint index

function getPriorBalanceIndex(uint256 tokenId, uint256 timestamp)
    public
    view
    returns (uint256);

Parameters

NameTypeDescription
tokenIduint256ID of veNFT to query
timestampuint256Time to query balance at

Returns

NameTypeDescription
<none>uint256Index of nearest checkpoint before timestamp

getPriorSupplyIndex

Gets historical supply index at timestamp

Uses binary search to find checkpoint index

function getPriorSupplyIndex(uint256 timestamp) public view returns (uint256);

Parameters

NameTypeDescription
timestampuint256Time to query supply at

Returns

NameTypeDescription
<none>uint256Index of nearest checkpoint before timestamp

_writeCheckpoint

Writes user checkpoint with updated balance

Updates or creates checkpoint based on epoch timing

function _writeCheckpoint(uint256 tokenId, uint256 balance) internal;

Parameters

NameTypeDescription
tokenIduint256ID of veNFT to checkpoint
balanceuint256New balance to record

_writeSupplyCheckpoint

Writes global supply checkpoint

Updates or creates checkpoint based on epoch timing

function _writeSupplyCheckpoint() internal;

rewardsListLength

Number of tokens configured for rewards

function rewardsListLength() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256Length of rewards token list

earned

Calculates unclaimed rewards for a veNFT

function earned(address token, uint256 tokenId) public view returns (uint256);

Parameters

NameTypeDescription
tokenaddressAddress of reward token to calculate
tokenIduint256ID of veNFT to calculate for

Returns

NameTypeDescription
<none>uint256Amount of unclaimed rewards

_deposit

Records a token deposit and updates checkpoints

Can only be called by authorized address

function _deposit(uint256 amount, uint256 tokenId) external;

Parameters

NameTypeDescription
amountuint256Amount of tokens being deposited
tokenIduint256ID of veNFT receiving deposit

_withdraw

Records a token withdrawal and updates checkpoints

Can only be called by authorized address

function _withdraw(uint256 amount, uint256 tokenId) external;

Parameters

NameTypeDescription
amountuint256Amount of tokens being withdrawn
tokenIduint256ID of veNFT withdrawing from

getReward

Claims accumulated rewards for a veNFT

function getReward(uint256 tokenId, address[] memory tokens)
    external
    virtual
    nonReentrant;

Parameters

NameTypeDescription
tokenIduint256ID of veNFT claiming rewards
tokensaddress[]Array of reward token addresses to claim

_getReward

Internal helper for processing reward claims

Calculates and transfers earned rewards to recipient

function _getReward(address recipient, uint256 tokenId, address[] memory tokens)
    internal;

Parameters

NameTypeDescription
recipientaddressAddress to receive claimed rewards
tokenIduint256ID of veNFT claiming rewards
tokensaddress[]Array of reward tokens to claim

notifyRewardAmount

Adds new reward tokens for distribution

Transfers tokens from caller and updates reward accounting

function notifyRewardAmount(address token, uint256 amount)
    external
    virtual
    nonReentrant;

Parameters

NameTypeDescription
tokenaddressAddress of token to add as reward
amountuint256Amount of token to add to rewards

renotifyRewardAmount

in case rewards where distributed during a epoch with no deposits, redistribute the rewards

function renotifyRewardAmount(uint256 timestamp, address token) external;

Parameters

NameTypeDescription
timestampuint256Timestamp of the start of the epoch to renotify
tokenaddressAddress of token to renotify

_notifyRewardAmount

Internal helper for adding rewards

Transfers tokens and updates reward accounting

function _notifyRewardAmount(address sender, address token, uint256 amount)
    internal;

Parameters

NameTypeDescription
senderaddressAddress providing reward tokens
tokenaddressAddress of reward token
amountuint256Amount of tokens to add as rewards