Skip to content

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Time Staking DApp

A decentralized time-locked ETH staking dApp built with Solidity, JavaScript, HTML, CSS, and Ethers.js.

Users can stake ETH for a fixed period and earn rewards based on the contract's APY. Funds can only be withdrawn after the selected lock period has ended.

Features

  • Connect wallet with MetaMask
  • Stake ETH for a fixed duration
  • Earn APY-based rewards
  • View staking balance and reward
  • View staking start and unlock time
  • Withdraw ETH after the lock period
  • Owner-controlled APY
  • Reentrancy protection
  • On-chain staking state and events

How It Works

Connect Wallet
      ↓
Choose Lock Duration
      ↓
Stake ETH
      ↓
Funds Locked
      ↓
Rewards Accumulate
      ↓
Lock Period Ends
      ↓
Unstake
      ↓
Receive Stake + Reward

Each user can have one active stake at a time. The contract stores the user's deposited amount, start timestamp, unlock timestamp, and staking status.

Reward Calculation

The contract uses simple interest, not compound interest:

Reward = (Staked Amount × APY × Days) / (100 × 365)

For example, with 1 ETH at 10% APY for 30 days:

Reward ≈ 0.0082 ETH

Rewards are calculated from the elapsed staking time.

Smart Contract

TimeStaking.sol

The contract provides:

stake(uint time)
unstake()
calculateReward(address user)
seeUserReward(address user)
changeAPY(uint8 APY)
ownerWithdraw()

The APY can be changed by the contract owner and is limited to a maximum of 30%.

Security

  • Custom Solidity errors
  • Reentrancy guard
  • Owner access control
  • Checks before ETH withdrawal
  • User state reset after withdrawal

Note: ownerWithdraw() allows the owner to withdraw the entire contract balance. This is an important trust assumption and means the contract is not trustless for user funds.

Tech Stack

  • Solidity ^0.8.21
  • JavaScript
  • HTML / CSS
  • Ethers.js
  • MetaMask

Project Structure

TimeStakingDapp/
├── TimeStaking.sol
├── index.html
├── script.js
├── dashboard.html
├── dashboard.js
├── status.html
├── status.js
└── README.md

The project includes separate pages for staking, dashboard information, and staking status.

Getting Started

git clone https://github.com/mohit-solidity/TimeStakingDapp.git
cd TimeStakingDapp

Open index.html in a browser with MetaMask installed.

The frontend uses Ethers.js to connect to the deployed contract and interact with its staking functions.

Disclaimer

This project is for educational purposes and has not been professionally audited. Do not use it with significant real funds.

Author

Mohit Sharma

Blockchain Developer | Solidity • React.js • Ethers.js

GitHub: https://github.com/mohit-solidity