A collection of Ethereum smart contracts built with Solidity and Hardhat, featuring vault management and token sale functionality.
This repository contains two main smart contracts:
- VaultContract - A secure vault system for managing user deposits and withdrawals
- TokenSale - A token sale contract with fixed pricing and ERC20-like token functionality
- User deposit and withdrawal functionality
- Individual balance tracking
- Ownership management with transfer capability
- Emergency withdrawal for contract owner
- Event emission for all major actions
- Fixed-price token sales
- ERC20-compatible token transfers
- Approval and allowance mechanism
- Configurable token price
- Admin fund withdrawal
- Sale status toggle
.
├── contracts/ # Solidity smart contracts
│ ├── VaultContract.sol
│ └── TokenSale.sol
├── scripts/ # Deployment scripts
│ ├── deploy.js
│ ├── deploy-vault.js
│ └── deploy-tokensale.js
├── test/ # Test suites
│ ├── VaultContract.test.js
│ └── TokenSale.test.js
├── hardhat.config.js # Hardhat configuration
└── package.json # Project dependencies
- Node.js (v16 or later)
- npm or yarn
- Git
- Clone the repository:
git clone <repository-url>
cd ethereum-smart-contracts- Install dependencies:
npm installCompile the smart contracts:
npm run compileThis will compile all contracts in the contracts/ directory and generate artifacts in the artifacts/ folder.
Run the full test suite:
npm testThe test suite includes comprehensive tests for:
- Contract deployment
- Deposit and withdrawal functionality
- Token purchases and transfers
- Access control mechanisms
- Edge cases and error conditions
To generate a coverage report:
npx hardhat coverage- Start a local Hardhat network:
npm run node- In a separate terminal, deploy the contracts:
npm run deploy:localhostDeploy only the VaultContract:
npm run deploy:vaultDeploy only the TokenSale contract:
npm run deploy:tokensaleDeploy both contracts at once:
npm run deploy- Create a
.envfile in the root directory:
INFURA_API_KEY=your_infura_api_key
PRIVATE_KEY=your_wallet_private_key
ETHERSCAN_API_KEY=your_etherscan_api_key
-
Uncomment and configure the network settings in
hardhat.config.js -
Deploy to testnet (e.g., Sepolia):
npx hardhat run scripts/deploy.js --network sepoliaAfter deployment, you can interact with the contracts using Hardhat console:
npx hardhat console --network localhostExample interactions:
// Get contract instance
const VaultContract = await ethers.getContractFactory("VaultContract");
const vault = await VaultContract.attach("DEPLOYED_ADDRESS");
// Deposit funds
await vault.deposit({ value: ethers.utils.parseEther("1.0") });
// Check balance
const balance = await vault.getBalance("YOUR_ADDRESS");
console.log(ethers.utils.formatEther(balance));npm test- Run all testsnpm run compile- Compile contractsnpm run deploy- Deploy all contractsnpm run deploy:vault- Deploy VaultContract onlynpm run deploy:tokensale- Deploy TokenSale onlynpm run deploy:localhost- Deploy to local networknpm run node- Start local Hardhat networknpm run clean- Clean artifacts and cache
Main Functions:
deposit()- Deposit ETH into the vaultwithdraw(uint256 _amount)- Withdraw specified amountgetBalance(address _user)- View user balancetransferOwnership(address _newOwner)- Transfer contract ownershipemergencyWithdraw()- Owner emergency withdrawal
Events:
Deposit(address indexed user, uint256 amount)Withdrawal(address indexed user, uint256 amount)OwnershipTransferred(address indexed previousOwner, address indexed newOwner)
Main Functions:
buyTokens()- Purchase tokens with ETHtransfer(address _to, uint256 _value)- Transfer tokensapprove(address _spender, uint256 _value)- Approve spendingtransferFrom(address _from, address _to, uint256 _value)- Transfer on behalftoggleSale()- Enable/disable token saleswithdrawFunds(uint256 _amount)- Withdraw ETH (admin only)updatePrice(uint256 _newPrice)- Update token price
Token Details:
- Name: SaleToken
- Symbol: SALE
- Decimals: 18
- Initial Price: 0.001 ETH per token
- Total Supply: 1,000,000 tokens
Events:
Transfer(address indexed from, address indexed to, uint256 value)Approval(address indexed owner, address indexed spender, uint256 value)TokensPurchased(address indexed buyer, uint256 amount, uint256 cost)SaleStatusChanged(bool status)
- Create your Solidity contract in the
contracts/directory - Write tests in the
test/directory - Create a deployment script in the
scripts/directory - Update the README with contract details
The contracts are compiled with the Solidity optimizer enabled (200 runs). You can adjust this in hardhat.config.js:
solidity: {
settings: {
optimizer: {
enabled: true,
runs: 200, // Adjust this value
},
},
}These smart contracts are provided for educational and development purposes. Before deploying to mainnet:
- Conduct thorough security audits
- Implement comprehensive testing
- Consider formal verification
- Review all external calls and state changes
- Test on testnets extensively
- Follow best practices for access control
Issue: "Cannot find module" error
rm -rf node_modules package-lock.json
npm installIssue: Compilation errors
npm run clean
npm run compileIssue: Test failures
- Ensure local network is running for localhost tests
- Check that contract addresses are correct
- Verify network configuration in
hardhat.config.js
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
For questions or issues, please open an issue on the GitHub repository.
Note: Always exercise caution when deploying smart contracts to mainnet. Ensure proper testing and auditing before deployment.