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

https://hackernoon.imgix.net/images/u6Q55cGlDHYO4zRjMDXW8yodW622-o483it4.jpeg

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:

  1. How much has each user deposited?
  2. How much has the entire protocol received?
  3. How long has each position been active?
  4. How should rewards be distributed between users?
  5. What happens when the reward pool runs out?
  6. How can the frontend represent global protocol state separately from individual wallet state?

StakeVault addresses these through separate user accounting and global accounting.

Global and Individual Staking State

Consider...

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more