---
eip: 2917
label: ERC-2917
title: Staking Reward Calculation
status: Stagnant
type: Standards Track
category: ERC
created: 2020-08-28
official: https://eips.ethereum.org/EIPS/eip-2917
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2917.md
canonical: https://eips.huatuo.xyz/zh/eip-2917/
lang: zh
tldr: "提议的一个接口，用于完全在链上计算质押奖励，按每个用户随时间变化的\"生产力\"（质押量）的比例，向其支付每区块利息的份额。"
tags: [staking, rewards, defi, erc-20, interface]
---

# ERC-2917: Staking Reward Calculation

> ERC2917 is a new standardization for on-chain calculation of staking reward.

**TL;DR:** 提议的一个接口，用于完全在链上计算质押奖励，按每个用户随时间变化的"生产力"（质押量）的比例，向其支付每区块利息的份额。

## 它解决什么问题？

据规范所述，早期 DeFi 的奖励方案往往分为两类：要么只有在所有人都退出合约之后才分发奖励，要么项目在链上收集数据、在链下计算奖励，再把结果推回链上。前者很慢，还迫使用户提走抵押品；后者依赖对运营方计算结果的信任。

ERC-2917 提出一种标准的、完全在链上的方式来计算任意区块的奖励，并提供通用接口，让钱包可以向用户展示他们赚了多少、如何计算，以及他们占总量的份额。

## 它如何运作

合约每个区块铸造固定数量的利息（`interestsPerBlock`）。每个用户有一个**生产力**（productivity）值（通常是其有效质押量），合约跟踪**全局生产力**。在没有任何人的生产力发生变化的每一段区块内，用户获得：

```text
reward_i = interestsPerBlock × blocks_in_period × (user_productivity / global_productivity)
```

用户的总奖励是所有这些时间段之和。每当用户的生产力增加或减少，以及用户提取时，计算都会更新，因此晚加入或早退出都不会有优势。

接口（奖励代币本身是 ERC-20）：

| 函数 / 事件 | 用途 |
| --- | --- |
| `interestsPerBlock()` | 每个区块产生的利息 |
| `changeInterestRatePerBlock(v)` | 修改利率（应受限制） |
| `getProductivity(user)` | 用户和全局的生产力 |
| `increaseProductivity(user, v)` / `decreaseProductivity(user, v)` | 调整用户的质押量（应仅限质押合约调用） |
| `take()` / `takeWithBlock()` | 调用者当前可领取的奖励（以及对应的区块） |
| `mint()` | 把调用者已累积的奖励铸造给他 |
| `InterestRatePerBlockChanged`、`ProductivityIncreased`、`ProductivityDecreased` | 事件 |

## 示例代码

高效实现这个公式的常用方法是使用"每单位生产力的累计奖励"计数器，这样就不需要对各时间段做循环：

```solidity
// 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;
    }
}
```

## 需要注意

- **状态：Stagnant。** Rationale 和 Security Considerations 两节从未撰写，该标准没有最终确定。
- 规范自己的措辞把这些称为"interests"（利息），但这个机制是奖励发行，而不是金融意义上的利息；发行代币的价值完全取决于项目本身。
- `increaseProductivity`、`decreaseProductivity` 和 `changeInterestRatePerBlock` 必须有访问控制。如果任何人都能调用，任何人都能铸造奖励。
- 同样的累加器模式在许多从未引用过这个 ERC 的质押和流动性挖矿合约中被广泛使用。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2917
