Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-2917, which proposes calculating staking rewards on-chain.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

Overview

ERC-2917 calculates staking rewards on-chain based on a user's effective contribution, referred to as productivity, over time.

The reward calculation is expressed as follows.

text
Reward_U = Σ (Δp_i / ΔP_i) × ΔG_i

Staking reward calculation formula

  • Δp_iΔp\_i represents the individual productivity change for user U during the interval from ti-1 to ti.
  • ΔP_iΔP\_i represents the global productivity change during the same interval.
  • ΔG_iΔG\_i represents the change in total production, such as the reward pool, during that interval.

The formula is intended to allocate rewards fairly when participants join late or leave early. Rewards reflect contributions during the relevant periods.

The calculation is simplified for implementation in Solidity and designed for reuse across DeFi projects.

The smart contract triggers calculations in situations such as:

  • An increase or decrease in a user's productivity, such as their collateral amount.
  • Withdrawal of a user's funds.

Motivation

Many DeFi projects face inefficient or opaque reward distribution mechanisms. Two common approaches are:

  • Distributing rewards after all users exit the contract

    This is fully on-chain, but users must wait to receive rewards and may be disadvantaged by having to withdraw collateral.

  • Collecting on-chain data, calculating rewards off-chain, and submitting the results

    This introduces centralized computation, making fairness and transparency dependent on how the calculation is performed and verified.

Users may consequently find it difficult to determine:

  • How much interest they can receive.
  • How that interest was calculated.
  • How their contribution compares with the overall contribution.

Standardizing ERC-2917 abstracts interest generation mechanisms across DeFi projects, making project metrics easier for wallets and other applications to retrieve and easier for users to understand.

Specification

Interface

IERC2917.sol
interface IERC2917 is IERC20 {

    /// @dev This emit when interests amount per block is changed by the owner of the contract.
    /// It emits with the old interests amount and the new interests amount.
    event InterestRatePerBlockChanged (uint oldValue, uint newValue);

    /// @dev This emit when a users' productivity has changed
    /// It emits with the user's address and the value after the change.
    event ProductivityIncreased (address indexed user, uint value);

    /// @dev This emit when a users' productivity has changed
    /// It emits with the user's address and the value after the change.
    event ProductivityDecreased (address indexed user, uint value);

    
    /// @dev Return the current contract's interests rate per block.
    /// @return The amount of interests currently producing per each block.
    function interestsPerBlock() external view returns (uint);

    /// @notice Change the current contract's interests rate.
    /// @dev Note the best practice will be restrict the gross product provider's contract address to call this.
    /// @return The true/false to notice that the value has successfully changed or not, when it succeed, it will emite the InterestRatePerBlockChanged event.
    function changeInterestRatePerBlock(uint value) external returns (bool);

    /// @notice It will get the productivity of given user.
    /// @dev it will return 0 if user has no productivity proved in the contract.
    /// @return user's productivity and overall productivity.
    function getProductivity(address user) external view returns (uint, uint);

    /// @notice increase a user's productivity.
    /// @dev Note the best practice will be restrict the callee to prove of productivity's contract address.
    /// @return true to confirm that the productivity added success.
    function increaseProductivity(address user, uint value) external returns (bool);

    /// @notice decrease a user's productivity.
    /// @dev Note the best practice will be restrict the callee to prove of productivity's contract address.
    /// @return true to confirm that the productivity removed success.
    function decreaseProductivity(address user, uint value) external returns (bool);

    /// @notice take() will return the interests that callee will get at current block height.
    /// @dev it will always calculated by block.number, so it will change when block height changes.
    /// @return amount of the interests that user are able to mint() at current block height.
    function take() external view returns (uint);

    /// @notice similar to take(), but with the block height joined to calculate return.
    /// @dev for instance, it returns (_amount, _block), which means at block height _block, the callee has accumulated _amount of interests.
    /// @return amount of interests and the block height.
    function takeWithBlock() external view returns (uint, uint);

    /// @notice mint the available interests to callee.
    /// @dev once it mint, the amount of interests will transfer to callee's address.
    /// @return the amount of interests minted.
    function mint() external returns (uint);
}

InterestRatePerBlockChanged

javascript
event InterestRatePerBlockChanged (uint oldValue, uint newValue);

Emitted when interest generated per block changes. When the authorized party changes the interest rate, the event records both the old and new values.

Parameters

  • oldValue
    • Previous interest amount.
  • newValue
    • Updated interest amount.

ProductivityIncreased

javascript
event ProductivityIncreased (address indexed user, uint value);

Emitted when a user's productivity increases, for example when their staked amount increases.

Parameters

  • user
    • Address of the relevant user.
  • value
    • Value associated with the productivity increase.

ProductivityDecreased

javascript
event ProductivityDecreased (address indexed user, uint value);

Emitted when a user's productivity decreases, for example when their staked amount is reduced.

Parameters

  • user
    • Address of the relevant user.
  • value
    • Value associated with the productivity decrease.

interestsPerBlock

javascript
function interestsPerBlock() external view returns (uint);

Returns the current amount of interest generated per block. Applications can use it to inspect the reward generation rate.

Return value

  • uint
    • Current interest generated per block.

changeInterestRatePerBlock

javascript
function changeInterestRatePerBlock(uint value) external returns (bool);

Sets or changes the reward generation rate per block.

The recommended implementation restricts execution to an authorized reward-supplying contract.

A successful change emits InterestRatePerBlockChanged.

Arguments

  • value
    • New interest amount.

Return value

  • bool
    • Whether the change succeeded.

getProductivity

javascript
function getProductivity(address user) external view returns (uint, uint);

Retrieves an individual user's current productivity, such as their staked amount, together with global productivity.

A user with no recorded productivity has an individual productivity of zero.

Arguments

  • user
    • Address of the relevant user.

Return values

  • uint
    • User productivity.
  • uint
    • Global productivity.

increaseProductivity

javascript
function increaseProductivity(address user, uint value) external returns (bool);

Increases a user's productivity, for example when additional assets are staked. This affects the user's reward calculation.

Execution should be restricted to a trusted, authorized contract.

Arguments

  • user
    • Address of the relevant user.
  • value
    • Amount by which to increase productivity.

Return value

  • bool
    • Whether the operation succeeded.

decreaseProductivity

javascript
function decreaseProductivity(address user, uint value) external returns (bool);

Decreases a user's productivity, for example when their staked amount is reduced.

Execution should likewise be restricted to a trusted, authorized contract.

Arguments

  • user
    • Address of the relevant user.
  • value
    • Amount by which to decrease productivity.

Return value

  • bool
    • Whether the operation succeeded.

take

javascript
function take() external view returns (uint);

Retrieves the rewards accrued by the caller up to the current block, based on the block number.

This is a query; actual reward issuance is performed through mint.

Return value

  • uint
    • Accrued interest available to the caller.

takeWithBlock

javascript
function takeWithBlock() external view returns (uint, uint);

Retrieves both the reward amount and the corresponding block number.

Like take, it reports accrued interest, while also identifying the block used in the calculation.

Return values

  • uint
    • Reward amount.
  • uint
    • Block number associated with the reward calculation.

mint

javascript
function mint() external returns (uint);

Claims accrued interest by minting or distributing the rewards to the user's address.

The claimed portion of accrued rewards is consumed after minting.

Return value

  • uint
    • Amount of interest minted.

Citation

Tony Carson tony.carsonn@gmail.com, Mehmet Sabir Kiraz m.kiraz@gmail.com, Süleyman Kardaş skardas@gmail.com, "ERC-2917: Staking Reward Calculation [DRAFT]," Ethereum Improvement Proposals, no. 2917, August 2020. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-2917.

Conclusion

ERC-2917 proposes a standardized mechanism for calculating staking rewards on-chain according to users' productivity and time.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/