EIPs Explained

ERC-2917 · Stagnant · ERC · 2020-08-28

ERC-2917: Staking Reward Calculation

A proposed interface for calculating staking rewards fully on-chain, paying each user a share of per-block interest proportional to their 'productivity' (stake) over time.

What problem does it solve?

Early DeFi reward schemes tended to fall into two camps, according to the spec: either rewards were only distributed once everyone exited the contract, or the project collected data on-chain, computed rewards off-chain, and pushed the results back. The first is slow and forces users to withdraw collateral; the second relies on trusting the operator's calculation.

ERC-2917 proposes a standard, fully on-chain way to compute rewards at any block, plus a common interface so wallets can show users how much they have earned, how it is calculated, and what share of the total they contribute.

How it works

The contract mints a fixed amount of interest per block (interestsPerBlock). Each user has a productivity value (typically their effective stake) and the contract tracks the global productivity. For every stretch of blocks in which nobody's productivity changes, a user earns:

reward_i = interestsPerBlock × blocks_in_period × (user_productivity / global_productivity)

A user's total reward is the sum over all such periods. The calculation is updated whenever a user's productivity increases or decreases, and when they withdraw, so joining late or leaving early gives no advantage.

The interface (the reward token itself is an ERC-20):

Function / event Purpose
interestsPerBlock() Interest produced per block
changeInterestRatePerBlock(v) Change the rate (should be restricted)
getProductivity(user) User's and global productivity
increaseProductivity(user, v) / decreaseProductivity(user, v) Adjust a user's stake (should be restricted to the staking contract)
take() / takeWithBlock() Rewards claimable by the caller now (and at which block)
mint() Mint the caller's accrued rewards to them
InterestRatePerBlockChanged, ProductivityIncreased, ProductivityDecreased Events

Sample code

The usual way to implement this formula efficiently is an "accumulated reward per unit of productivity" counter, so no loop over periods is needed:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract RewardAccounting {
    uint256 public interestsPerBlock;
    uint256 public totalProductivity;
    uint256 public accPerShare;      // scaled by 1e18
    uint256 public lastBlock;

    mapping(address => uint256) public productivity;
    mapping(address => uint256) public rewardDebt;
    mapping(address => uint256) public pending;

    function _update() internal {
        if (block.number > lastBlock && totalProductivity > 0) {
            uint256 reward = (block.number - lastBlock) * interestsPerBlock;
            accPerShare += reward * 1e18 / totalProductivity;
        }
        lastBlock = block.number;
    }

    function _settle(address user) internal {
        pending[user] += productivity[user] * accPerShare / 1e18 - rewardDebt[user];
    }

    function _increase(address user, uint256 value) internal {
        _update();
        _settle(user);
        productivity[user] += value;
        totalProductivity += value;
        rewardDebt[user] = productivity[user] * accPerShare / 1e18;
    }
}

Things to know

  • Status: Stagnant. The Rationale and Security Considerations sections were never written, and the standard was not finalized.
  • The spec's own wording calls these "interests", but the mechanism is a reward emission, not interest in a financial sense; the emitted token's value depends entirely on the project.
  • increaseProductivity, decreaseProductivity and changeInterestRatePerBlock must be access-controlled. If anyone can call them, anyone can mint rewards.
  • The same accumulator pattern is widely used in staking and liquidity-mining contracts that never referenced this ERC.

#staking #rewards #defi #erc-20 #interface

Details

Authors
Tony Carson <[email protected]>, Mehmet Sabir Kiraz <[email protected]>, Süleyman Kardaş <[email protected]>
Status
Stagnant
Type
Standards Track / ERC
Created
2020-08-28

Machine-readable: Markdown · JSON