A full-stack decentralized finance (DeFi) staking platform built with Solidity, Hardhat, Next.js, and RainbowKit. Users can stake ERC20 tokens and earn rewards at a 10% annual percentage rate (APR).
Deployed on Polygon Amoy Testnet:
- StakingToken Contract: 0x199bE779cA804624E7826bcD5ec69F3Fd29caD44
- StakingRewards Contract: 0x817e3d5184852b4B801C06ABc55B325de4334a41
- Network: Polygon Amoy (Chain ID: 80002)
-
Smart Contracts
- ERC20 Staking Token (STK)
- Staking Rewards Contract with time-based reward calculation
- Secure with OpenZeppelin contracts
- Comprehensive test coverage (25 passing tests)
-
Frontend
- Next.js 15 with TypeScript
- RainbowKit for wallet connection
- Wagmi & Viem for blockchain interactions
- Tailwind CSS for styling
- Real-time balance and rewards display
- Auto-refresh every 10 seconds
web3/
├── contracts/ # Solidity smart contracts
│ ├── StakingToken.sol # ERC20 token
│ └── StakingRewards.sol # Staking logic
├── test/ # Contract tests
├── scripts/ # Deployment scripts
├── frontend/ # Next.js frontend
│ └── app/ # App router pages
├── hardhat.config.cjs # Hardhat configuration
└── .env.example # Environment variables template
- Node.js 18+ and npm
- MetaMask or another Web3 wallet
- Polygon Amoy testnet MATIC (get from faucet)
# Install contract dependencies
npm install
# Install frontend dependencies
cd frontend
npm install
cd ..# Copy environment template
cp .env.example .env
# Edit .env and add:
# - Your wallet private key
# - Polygon Amoy RPC URL (optional, uses public RPC by default)
# - PolygonScan API key (optional, for contract verification)# Run all tests
npx hardhat test
# Run with gas reporting
REPORT_GAS=true npx hardhat testAll 25 tests should pass:
- Deployment tests
- Staking functionality
- Reward calculations
- Withdrawal operations
- Admin functions
- View functions
npx hardhat compile-
Get Testnet MATIC
- Visit Polygon Amoy Faucet
- Enter your wallet address
- Wait for testnet MATIC
-
Deploy Contracts
npx hardhat run scripts/deploy.js --network polygonAmoy
-
Save Deployment Info
- Contract addresses will be saved to
deployment.json - Copy the addresses to
frontend/app/constants.ts
- Contract addresses will be saved to
-
Verify Contracts (Optional)
npx hardhat verify --network polygonAmoy STAKING_TOKEN_ADDRESS 1000000 npx hardhat verify --network polygonAmoy STAKING_REWARDS_ADDRESS STAKING_TOKEN_ADDRESS
# Terminal 1: Start local Hardhat node
npx hardhat node
# Terminal 2: Deploy to local network
npx hardhat run scripts/deploy-local.js --network localhostAfter deployment, update /Users/baguskto/Downloads/Work/web3/frontend/app/constants.ts:
export const STAKING_TOKEN_ADDRESS = '0xYourStakingTokenAddress' as `0x${string}`;
export const STAKING_REWARDS_ADDRESS = '0xYourStakingRewardsAddress' as `0x${string}`;- Go to WalletConnect Cloud
- Create a project
- Copy your Project ID
- Create
frontend/.env.local:NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=your_project_id
cd frontend
npm run dev-
Connect Wallet
- Click "Connect Wallet" button
- Select your wallet (MetaMask recommended)
- Approve connection
- Switch to Polygon Amoy network if prompted
-
Get Test Tokens
- After deployment, the deployer has all tokens
- Send some STK tokens to your test address using MetaMask or:
npx hardhat console --network polygonAmoy
const token = await ethers.getContractAt("StakingToken", "TOKEN_ADDRESS"); await token.transfer("YOUR_ADDRESS", ethers.parseEther("1000"));
-
Approve & Stake Tokens
- Enter amount to stake (minimum 1 STK)
- Click "Approve STK" and confirm transaction
- Wait for confirmation
- Click "Stake Tokens" and confirm transaction
-
Watch Rewards Accumulate
- Rewards update automatically every 10 seconds
- Formula: (stakedAmount × 10% × timeElapsed) / (365 days)
-
Claim Rewards
- Click "Claim Rewards" to collect earned rewards
- Rewards are sent to your wallet
-
Withdraw Tokens
- Enter amount to withdraw (up to staked amount)
- Click "Withdraw Tokens"
- Automatically claims pending rewards
- Type: ERC20
- Initial Supply: 1,000,000 STK
- Decimals: 18
- Features: Mintable, Burnable (owner only)
- APR: 10% (1000 basis points)
- Minimum Stake: 1 STK
- Reward Calculation: Continuous, based on time elapsed
- Features:
- Stake tokens to earn rewards
- Withdraw staked tokens
- Claim accumulated rewards
- Owner can update reward rate
- Wallet Connection: RainbowKit integration with multiple wallet support
- Real-time Updates: Auto-refresh balances and rewards every 10 seconds
- Transaction Status: Loading states and confirmation feedback
- Responsive Design: Mobile-friendly with Tailwind CSS
- Dark Mode: Automatic dark mode support
-
Tests Failing
- Ensure you're using compatible dependency versions
- Check that you have
"type": "commonjs"in package.json - Clear cache:
rm -rf cache artifacts
-
Frontend Won't Build
- Make sure contract addresses are updated in
constants.ts - Check that ABIs were generated:
frontend/app/*.abi.json - Verify all dependencies are installed
- Make sure contract addresses are updated in
-
Transactions Failing
- Ensure you have enough MATIC for gas
- Check that you have STK tokens in your wallet
- Verify you're on Polygon Amoy network
- Make sure contracts are deployed correctly
-
RainbowKit Not Showing
- Check WalletConnect Project ID in
.env.local - Verify providers are properly configured in
layout.tsx
- Check WalletConnect Project ID in
- Chain ID: 80002
- RPC URL: https://rpc-mumbai.maticvigil.com
- Block Explorer: https://amoy.polygonscan.com
- Faucet: https://faucet.polygon.technology
- Open MetaMask
- Click network dropdown
- Click "Add Network"
- Enter Amoy testnet details above
- Save
The contracts use OpenZeppelin's SafeERC20 and are optimized with:
- Compiler optimization enabled (200 runs)
- ReentrancyGuard for security
- Efficient storage patterns
- Contracts use OpenZeppelin's audited libraries
- ReentrancyGuard prevents reentrancy attacks
- Access control with Ownable pattern
- Minimum stake requirement prevents dust attacks
- SafeERC20 for token transfers
MIT
For issues or questions:
- Check deployment.json for correct addresses
- Verify .env configuration
- Check Polygon Amoy block explorer for transactions
- Review Hardhat and Next.js logs for errors
- Deploy to Polygon mainnet (update network config)
- Add more features (compound staking, multiple reward tokens)
- Implement governance
- Add analytics dashboard
- Create mobile app
Built with ❤️ for learning Web3 and DeFi development
