Building a Finite-Reward DeFi Staking Protocol With Solidity and Foundry

DeFi staking protocols appear simple from the outside: deposit tokens, wait, and receive rewards. The difficult part is making the accounting behave correctly when multiple users are staking, withdrawing, claiming rewards, changing reward rates, and interacting with a reward pool that contains a finite number of tokens. I built StakeVault to explore these problems in a complete Web3 application. The protocol combines Solidity smart contracts, Foundry testing, and a React frontend deployed on Ethereum Sepolia. The Core Problem A staking protocol has to answer several questions: How much has each user deposited? How much has the entire protocol received? How long has each position been active?
…