ERC-1973 Extensible Minting Reward Distribution (Scalable Rewards)
Stagnant · Vaults & DeFi · created 2019-04-01
A reward distribution interface that mints a fixed amount of tokens per block, divides it by the current total number of participants, accumulates each participant's share, and lets each participant withdraw it themselves.
Why it is needed
As the number of participants grows into the thousands or tens of thousands, a push model, where the contract sends tokens directly to each address, cannot work because it hits the limit on how much computation fits in a single block (the block gas limit). Looping through thousands of addresses to send very small amounts is inefficient and gas-expensive, and if the DApp developer or operating company bears that cost instead, the application ends up depending on a specific party, creating centralization problems. What's needed is a pull model where each participant claims their own reward, but there was no standardized way to do this that accounts for shares changing with the number of participants and correctly settles people joining and leaving in the middle.
What to implement
constructor(uint256 _tokensPerBlock, uint256 _blockFreezeInterval) public ERC20Detailed("Simple Token", "SIM", 18): Initializes the contract with the amount minted per block and the minting interval.modifier isAuthorized(): Checks whether the caller is a registered minter.function addMinters(address _minter) external returns (bool): Registers a new participant as a minter and increases the participant count.function removeMinters(address _minter) external returns (bool): Removes a participant from the minter list and decreases the participant count.function trigger() external isAuthorized returns (bool): Mints new tokens once the minting condition is met.function withdraw() external isAuthorized returns (bool): Transfers the caller's accumulated reward tokens.function readyToMint() public view returns (bool): Checks whether the set number of blocks has passed since the last minting.function calculateRewards() private returns (uint256): Calculates the caller's accrued rewards and updates participantMask.function mintTokens() private returns (bool): Mints tokens for the number of elapsed blocks and updates the last minted block.function calculateTPP(uint256 tokens) private returns (bool): Calculates the per-participant share (TPP) by dividing the minted amount by the number of participants.function updateRoundMask(uint256 tpp) private returns (bool): Accumulates the newly calculated TPP into roundMask.function updateParticipantMask(address participant) private returns (bool): Updates that participant's baseline to the current roundMask.
In depth
TPP, the unit of per-participant share
TPP (token per participant) is the share each participant receives from a single minting event, calculated as the amount minted this round divided by totalParticipants at that time. When there are few participants TPP is large, and as participants grow the per-participant share naturally shrinks even for the same minted amount.
roundMask, a global cumulative snapshot
roundMask is a global value that keeps adding the newly calculated TPP each time a trigger occurs. For example, if the first minting splits 10 tokens among 100 participants (TPP=10/100) and the second minting splits 12 tokens among 95 participants (TPP=12/95), roundMask becomes 10/100 plus 12/95. This value is updated via calculateTPP and updateRoundMask every time mintTokens is called.
participantMask, a per-participant baseline
participantMask[participant] stores the roundMask value at the time that participant last registered or withdrew. A participant's reward is calculated as the current roundMask minus their stored participantMask, which equals the sum of the TPPs accumulated since their last baseline. This value is updated to the current roundMask via updateParticipantMask every time addMinters and withdraw (internally calculateRewards) are called.
Example of a participant joining and leaving mid-stream
In the original example, with 10 participants active and 20 tokens minted on every transaction, an 11th participant joins at the 2nd transaction and leaves before the 5th transaction. At entry, participantMask is fixed to the roundMask at that time (20/10), and subtracting this from the roundMask just before exit leaves exactly the segment the participant actually stayed for (three additions of 20/11), which is calculated as their reward.
Minting triggers and interval limits
trigger() can only be called by a registered minter, and internally readyToMint() checks whether the current block exceeds lastMintedBlockNumber plus blockFreezeInterval. If the condition is met, mintTokens() mints (current block minus last minted block) multiplied by tokensPerBlock new tokens to tokencontractAddress, updates lastMintedBlockNumber, and then updates roundMask via calculateTPP.
Withdrawal procedure
Participants call withdraw() to claim their rewards. Internally, calculateRewards() computes the reward as roundMask minus that participant's participantMask, immediately updates participantMask to the current roundMask, requires the calculated amount to be greater than 0, and transfers that amount of tokens. Because each participant calls this individually, gas cost per trigger stays constant regardless of how many participants there are.
Registering and deregistering participants
addMinters(_minter) registers the given address as a minter, increments totalParticipants by 1, and immediately calls updateParticipantMask to align participantMask with the current roundMask. This way, only the TPP accumulated after registration counts toward that participant's reward. removeMinters(_minter) decrements totalParticipants by 1 and removes minter privileges. tokensPerBlock and blockFreezeInterval are fixed at deployment time in the constructor.
Note on progress status
This proposal was written in 2019, and its test cases and implementation remain marked 'WIP' (work in progress). Its status is Stagnant, meaning discussion has stalled and is no longer progressing. Further validation and implementation would be needed before applying it in practice.