A transferable ERC-20 whose holders accrue a reward token pro-rata.
Every holder of this share token continuously earns a proportional slice of any rewardToken distributed to
the pool — and, crucially, that accrual follows the shares through transfers, mints, and burns. Sell your
shares and you keep the dividends you earned while holding them; buy shares and you earn only from that point
on. Distributions are O(1) no matter how many holders there are.
Naively splitting each distribution across holders would be O(holders) and would mis-handle transfers. Instead
the contract keeps a single running accumulator magnifiedDividendPerShare (scaled by 2¹²⁸ for precision).
A holder's lifetime entitlement is perShare · balance, adjusted by a per-holder correction term that is
updated on every mint / burn / transfer:
- on transfer of
vshares from → to:correction[from] += perShare·v(preserve whatfromalready earned) andcorrection[to] -= perShare·v(toearns only from now on).
This is the canonical "magnified dividend" algorithm (Roberto Bayardo / PoolTogether / Cometh). Unlike a fixed-share splitter, the shares here are ordinary transferable balances and dividends track them exactly.
distributeDividends(amount)— anyone pushesamountof rewardToken to all current shareholders pro-rata. Crediting is the actual amount received (fee-on-transfer safe).withdrawDividend()— caller pulls their accrued-but-unwithdrawn dividends.- Views:
withdrawableDividendOf(a),accumulativeDividendOf(a); plus the full ERC-20 surface (the shares).
ReentrancyGuard+SafeERC20on the reward paths;SafeCastfor the signed correction arithmetic.- Magnitude scaling (
2¹²⁸) makes per-share rounding negligible; rounding only ever leaves dust in the contract, never an over-payment. - Fully tested: unit tests for equal splits, "dividends before you join aren't yours", "a transfer carries
future accrual but not the past", double-withdraw, and conservation, plus two stateful invariants
(
fail_on_revert = true):- conservation — the reward balance held always equals lifetime distributed minus lifetime withdrawn;
- solvency — the sum of every holder's withdrawable dividend never exceeds the reward balance.
forge test
FOUNDRY_INVARIANT_FAIL_ON_REVERT=true forge test --match-path "test/DividendToken.invariant.t.sol"NAME="Share" SYMBOL="SHR" REWARD_TOKEN=0x... INITIAL_SUPPLY=1000000000000000000000000 \
forge script script/Deploy.s.sol --rpc-url "$RPC_URL" --broadcastNot audited. Reference implementation — review before any real deployment.