Building a DeFi Staking Platform with Solidity, Foundry, and Next.js on Ethereum Sepolia
As part of my EtherAuthority Web3 Training, I built a full-stack DeFi Staking Platform to gain practical experience in smart contract development, ERC-20 tokens, blockchain testing, and Web3 frontend integration.
The project was developed and deployed on the Ethereum Sepolia testnet.
Project Objective
The main objective was to build a decentralized staking application where users can connect their wallet, approve and stake ERC-20 tokens, withdraw their staked tokens, and claim rewards generated according to the staking contract's APR.
The project also gave me hands-on experience with Foundry-based testing and integrating deployed Solidity contracts with a Next.js frontend.
Technologies Used
- Solidity
- Foundry
- OpenZeppelin
- Next.js
- TypeScript
- ethers.js
- MetaMask
- Ethereum Sepolia
- Vercel
Smart Contracts
The project contains three main Solidity contracts.
1. StakeToken
StakeToken.sol is an ERC-20 token named Stake Token (STK).
The contract mints an initial supply of 100,000 STK to the deploying account.
2. RewardToken
RewardToken.sol is an ERC-20 token named Reward Token (RWD).
The contract mints an initial supply of 1,000,000 RWD to the deploying account.
3. Staking
Staking.sol manages the staking process and reward calculation.
The main functions include:
-
stake()— deposits STK into the staking contract. -
Withdraw()— withdraws previously staked STK. -
claimRewards()— claims accumulated RWD rewards. -
setAPR()— allows the owner to update the APR. -
pause()/unpause()— allows the owner to pause and resume staking-related operations.
The contract also uses OpenZeppelin components such as SafeERC20, Ownable, Pausable, and ReentrancyGuard.
How Staking Works
The user first connects their MetaMask wallet to the application.
Before staking, the user approves the staking contract to spend a selected amount of STK tokens.
The user can then stake the approved amount.
The staking contract records the user's staked balance and updates the reward calculation based on the amount staked, the configured APR, and the time elapsed.
Users can later withdraw their staked tokens or claim their accumulated rewards.
Frontend
The frontend was built using Next.js, TypeScript, and ethers.js.
The application connects to MetaMask through the browser wallet provider and reads blockchain data directly from the deployed contracts.
The dashboard displays:
- STK wallet balance
- Staked STK
- Pending rewards
- RWD wallet balance
- Current APR
The application also displays transaction hashes after blockchain transactions so that transactions can be verified on the Sepolia network.
Foundry Testing
I used Foundry to test the staking contracts before working with the deployed application.
The tests cover:
- Constructor token addresses
- Successful staking
- Successful withdrawal
- Zero-amount staking
- Withdrawal greater than the staked balance
- Claiming without rewards
- Reward claiming
- Owner APR updates
- Non-owner APR restrictions
- Contract pause
- Contract unpause
- Invalid zero APR
These tests helped verify the core contract behavior and important edge cases.
Ethereum Sepolia Deployment
The contracts were deployed on Ethereum Sepolia.
Contract Addresses
Staking Contract
0x1873eca046e15b910da2ea9ad50fce9fd695b0a7
Stake Token (STK)
0xaba6599a21cf3fa1bcf4038eb225f829bdb17cf5
Reward Token (RWD)
0x36070c3ad6efa8045d87e97e627dcb0eced73279
Challenges
One of the practical challenges was ensuring that the frontend correctly interacted with the deployed contracts and displayed the connected wallet's blockchain data.
I also worked through the complete approval → staking → withdrawal/reward flow and verified transactions on the Sepolia testnet.
Another important part of the project was testing contract behavior with Foundry before using the deployed contracts through the frontend.
Live Project
The frontend is deployed and available online, and the complete source code is available on GitHub.
GitHub Repository:
https://github.com/PranshuMishra2004/DeFi-Staking-Platform
Live Project:
https://de-fi-staking-platform-opal.vercel.app/
Conclusion
Building this DeFi Staking Platform gave me practical experience beyond writing individual Solidity contracts. I worked through the complete process of developing smart contracts, testing them with Foundry, deploying them to Ethereum Sepolia, and connecting them to a Web3 frontend using MetaMask and ethers.js.
This project has strengthened my understanding of Solidity, ERC-20 token interactions, staking mechanisms, smart contract testing, and decentralized application development.
Top comments (0)