ERC-2917 Staking reward calculation standard (Staking Reward Calculation)
Stagnant · Vaults & DeFi · created 2020-08-28
A common interface that calculates and pays out staking rewards on-chain in real time, proportional to a user's collateral and deposit duration.
Why it is needed
Existing DeFi projects' reward distribution methods had two problems. One was not distributing rewards until all users exited the contract, which made the distribution cycle too long and meant users had to withdraw their collateral first to receive rewards, which could actually put them at a disadvantage. The other was computing on-chain data off-chain and then only posting the result on-chain for distribution, which made it difficult to guarantee fairness and transparency since the core computation happened off-chain, creating a barrier to investment for users. Even as new DeFi projects emerged daily, users found it hard to know how much interest they would receive, how it was calculated, or how their contribution compared to that of all participants.
What to implement
event InterestRatePerBlockChanged (uint oldValue, uint newValue);: An event emitted with the previous and new values when the contract owner changes the interest rate per block.event ProductivityIncreased (address indexed user, uint value);: An event emitted with the user's address and the changed value when a user's productivity increases.event ProductivityDecreased (address indexed user, uint value);: An event emitted with the user's address and the changed value when a user's productivity decreases.function interestsPerBlock() external view returns (uint);: Returns the amount of interest the contract currently generates per block.function changeInterestRatePerBlock(uint value) external returns (bool);: Changes the contract's interest rate, emits an InterestRatePerBlockChanged event on success, and returns whether it succeeded.function getProductivity(address user) external view returns (uint, uint);: Returns the given user's productivity and the total productivity, returning 0 if there is no productivity.function increaseProductivity(address user, uint value) external returns (bool);: Increases a user's productivity and returns whether it succeeded.function decreaseProductivity(address user, uint value) external returns (bool);: Decreases a user's productivity and returns whether it succeeded.function take() external view returns (uint);: Returns the amount of interest the caller can receive as of the current block height.function takeWithBlock() external view returns (uint, uint);: Similar to take(), but also returns the block height on which the interest amount is based, along with the interest amount.function mint() external returns (uint);: Transfers accrued interest to the caller's address and returns the amount of interest minted.
In depth
Principle behind the reward calculation formula
ERC-2917 rewards are calculated based on the product of effective collateral and deposit duration. Between consecutive block numbers t_i-1 and t_i, the share for each interval (Δp_i/ΔP_i x ΔG_i) is computed using user U's individual productivity change (Δp_i), the total productivity change (ΔP_i), and the total reward amount for that interval (ΔG_i), and these shares are summed across all intervals. This formula is designed so that exiting early or entering late carries neither advantage nor disadvantage.
When recalculation occurs
The contract triggers reward calculation whenever a user's productivity increases or decreases, and whenever a user withdraws rewards. Because these two events serve as the trigger points, fair distribution is possible on-chain in real time without any separate off-chain computation.
Limitations of the two existing distribution methods
According to the Motivation section, existing DeFi projects used two methods. The first distributes rewards only when all users exit the contract, which has a long distribution cycle and can be disadvantageous since users must remove their collateral first to receive rewards. The second is a semi-decentralized approach that aggregates on-chain data, computes it off-chain, and then posts the result on-chain for distribution, which reduces fairness and transparency since the core algorithm runs off-chain, creating a barrier to investment for users.
Three groups within the interface
IERC2917 inherits from IERC20, and its functionality is divided into three groups: the interest rate management group (interestsPerBlock, changeInterestRatePerBlock), the productivity management group (getProductivity, increaseProductivity, decreaseProductivity), and the reward query/claim group (take, takeWithBlock, mint).
Recommendation on access restriction
It is recommended best practice to restrict changeInterestRatePerBlock so that only the contract address providing the gross product can call it. It is also recommended that increaseProductivity and decreaseProductivity be restricted so that only the contract proving productivity can call them. However, this is a recommendation in the @dev comments and is not enforced by the interface itself.
Difference between take() and takeWithBlock()
take() returns only the amount of interest mintable as of the current block height. takeWithBlock() returns the same interest amount together with the block height on which it was calculated, for example in the form (_amount, _block), allowing you to know the amount of interest accrued as of a specific block.
Proposal status
For this ERC, both the Rationale and Security Considerations sections are left as TBD, and its status is Stagnant. In other words, the detailed rationale and security considerations have not yet been filled in, and discussion on this proposal has stalled.