October 15, 2024 · ⏳ 5 min · X ↗
DeFi primitive - billion-dollar algorithm: Scalable Reward Distribution whitepaper

Simple is way more than simplistic. Many white papers in the DeFi space seem to be written more to impress than to educate. Curious readers often find themselves lost in a sea of advanced mathematical formulas filled with Greek symbols. Instead of gaining clarity, they leave the experience feeling more confused and less confident than when they began. Fortunately, the reality of building protocols is often different. It is based on a few fundamentally simple — though not simplistic — algorithms. These DeFi primitives power multi-billion-dollar protocols and have become essential tools in the DeFi engineer’s toolkit.

Attitude towards algo, eng, and life: simplistic, complex, simple

A Billion-Dollar Algorithm

A prime example is an algorithm that eventually earned the name ‘billion-dollar algorithm’ and became the de facto standard for staking rewards contracts. It has become so ubiquitous in the space that it is often used without a second thought, sometimes without even realizing it’s in play. This algorithm solves the common challenge of distributing token rewards among users based on their pro-rata deposits / stakes.

It all began with the Scalable Reward Distribution on the Ethereum Blockchain whitepaper, written in 2018 for DappCon by Bogdan Batog, Lucian Boca, and Nick Johnson. The whitepaper defined the distinction between ‘Pull’ and ‘Push’ reward distribution mechanisms, explaining why the Push mechanism is impractical for blockchains and how to implement the Pull model effectively. Let’s define the difference:

Core Logic

A simplified version of the algorithm where a user is allowed to deposit and withdraw only once.

One deposit, one withdrawal - simplified reward distribution algorithm

A more practical version allows users to deposit and withdraw multiple times. Rewards are calculated and distributed before any changes are made to the eligible reward amount (i.e., before each deposit or withdrawal).

Multiple deposits, multiple withdrawals - practical reward distribution algorithm

History of Adoption

It is everywhere, literally. The first liquidity mining program where rewards were computed on-chain was SNX’s incentive for the sETH Uniswap pool. This was implemented in the Unipool sETH contract, written in 2019 by Anton Bukov. [2]

Today, the algorithm is used by many projects in the Ethereum ecosystem. For example, Uniswap v3 uses it to track fees earned by individual positions, SushiSwap uses it in its MasterChef smart contract to incentivize liquidity providers, and 1inch has adapted it as part of their ERC20Plugins concept to allow farming without a need to stake tokens. [3]

The simplest conceptual implementations I would like to refer: Solidity By Example: Staking rewards, Solidity By Example: Discrete Staking rewards. The widespread popularity and adoption of the algorithm place it among the top DeFi primitives, continually proving that simple is way more than simplistic.

References

  1. https://batog.info/papers/scalable-reward-distribution.pdf
  2. https://www.paradigm.xyz/2021/05/liquidity-mining-on-uniswap-v3
  3. https://github.com/euler-xyz/reward-streams