This repository contains a Solidity smart contract (WChainDapp.sol) for a staking mechanism where users can deposit tokens, earn rewards based on tiered reward rates and lock times, and claim their deposits after the lock period. The contract is designed to be deployed in a Hardhat environment.
The WChainDapp contract inherits from a WToken contract (assumed to be an ERC20-compatible token) and uses an external ERC20 token (dappToken) for staking and rewards. It implements the following features:
- Tiered Rewards: Users can deposit tokens into different tiers, each with its own reward rate (in basis points) and lock time (in seconds).
- Whitelisting: Only whitelisted users can deposit tokens.
- Deposit Cooldown: A cooldown period between deposits is enforced to prevent abuse.
- Claiming: Users can claim their deposited amount and accumulated rewards after the lock time has elapsed.
- Event Logging: Important contract actions are logged as events for easy tracking.
- Error Handling: Custom errors are used for clear error reporting.
WChainDapp.sol: The main contract implementing the staking logic.WToken.sol: (Included by import) The ERC20 token contract used for staking (assumed to be in the same directory).- Dependencies:
@openzeppelin/contracts: For theIERC20interface.
-
Prerequisites:
- Node.js and npm (or yarn)
- Hardhat:
npm install --save-dev hardhat - OpenZeppelin Contracts:
npm install @openzeppelin/contracts
-
Installation:
git clone <repository_url> cd <repository_directory> npm install
-
Configuration:
- In your Hardhat configuration file (
hardhat.config.js), configure your network settings (e.g., local Hardhat network, testnet, mainnet).
- In your Hardhat configuration file (
-
Deployment:
- Compile the contracts:
npx hardhat compile
- Deploy the
WChainDappcontract, providing the address of thewchaindappTokencontract as a constructor argument:
npx hardhat run scripts/deploy.js --network <network_name>
(Create a
deploy.jsscript in thescriptsdirectory similar to the example below)const { ethers } = require("hardhat"); async function main() { const Dapp = await ethers.getContractFactory("WChain"); const dapp = await Dapp.deploy("<wchaindappToken_address>"); await dapp.deployed(); console.log("Dapp deployed to:", wchaindapp.address); } main().catch((error) => { console.error(error); process.exitCode = 1; });
-
Interaction:
- Use Hardhat console or a frontend application to interact with the contract.
constructor(address _token): Initializes the contract with thedappTokenaddress and sets up default tiers.updateTier(uint8 _tier, uint256 _rewardRate, uint256 _lockTime): Updates the reward rate and lock time for a specific tier (only owner).setWhitelist(address _user, bool _status): Whitelists or removes a user (only owner).deposit(uint8 _tier, uint256 _amount): Deposits tokens into a specific tier (only whitelisted users).claim(uint8 _tier): Claims the deposited amount and rewards (only whitelisted users).getDepositDetails(address _user, uint8 _tier): Returns the details of a user's deposit.receive(): Fallback function to accept Ether.
TierUpdated(uint8 indexed tier, uint256 rewardRate, uint256 lockTime): Emitted when a tier is updated.Deposited(address indexed user, uint256 amount, uint8 tier, uint256 reward, uint256 unlockTime): Emitted when a user deposits tokens.Claimed(address indexed user, uint256 amount): Emitted when a user claims their deposit.Whitelisted(address indexed user, bool status): Emitted when a user's whitelist status is changed.
- The contract relies on the security of the underlying ERC20 token (
dappToken). - Proper access control is implemented using modifiers (
onlyOwner,onlyWhitelisted). - Reentrancy attacks are mitigated by using safe transfer functions from OpenZeppelin's
IERC20interface. - It's highly recommended to conduct thorough testing and auditing before deploying to a production environment.
This contract is provided as-is and without any warranties. Use it at your own risk.