A basic Solidity vault system demonstrating inheritance patterns with secure deposit and withdrawal functionality using OpenZeppelin's SafeCast library.
This project consists of two contracts:
- VaultBase: Foundation contract handling asset storage and balance tracking
- VaultManager: Inherited contract implementing deposit/withdrawal logic
- ✅ Inheritance Pattern: Clean separation of base functionality and management logic
- ✅ Balance Tracking: Individual user balance mapping with total deposits counter
- ✅ SafeCast Integration: OpenZeppelin library for secure mathematical operations
- ✅ Event Logging: Deposit and withdrawal events for transparency
- ✅ Input Validation: Prevents zero-amount transactions and insufficient balance withdrawals
- ✅ Immutable Asset: Asset address cannot be changed after deployment
- Stores the asset address (immutable)
- Maintains user balance mappings
- Provides balance query functionality
- Inherits from VaultBase
- Implements deposit and withdrawal functions
- Tracks total deposited amount
- Emits transaction events
- Solidity ^0.8.24
- OpenZeppelin Contracts
- Create a new workspace in Remix
- Install OpenZeppelin contracts:
npm install @openzeppelin/contracts
- Copy the contract code into a new
.solfile - Compile and deploy
Deploy the VaultManager contract with the target asset address:
VaultManager vault = new VaultManager(0x1234...); // Asset contract addressvault.deposit(1000); // Deposit 1000 tokensvault.withdraw(500); // Withdraw 500 tokensuint256 balance = vault.getBalance(userAddress);
uint256 myBalance = vault.balances(msg.sender);uint256 total = vault.totalDeposited();| Function | Description | Visibility |
|---|---|---|
deposit(uint256) |
Deposit tokens to vault | Public |
withdraw(uint256) |
Withdraw tokens from vault | Public |
getBalance(address) |
Get user's balance | Public View |
balances(address) |
Direct balance mapping access | Public View |
totalDeposited() |
Get total amount deposited | Public View |
asset() |
Get asset contract address | Public View |
event Deposit(address indexed user, uint256 amount);
event Withdraw(address indexed user, uint256 amount);- Input validation prevents zero-amount transactions
- Balance checks prevent overdrafts
- SafeCast library provides overflow protection
- Immutable asset prevents asset switching after deployment
- No actual ERC-20 token transfers (interface calls missing)
- No access controls or admin functions
- No pause/emergency mechanisms
- No fee structures
- No reentrancy guards
Test the contract thoroughly on testnets before mainnet deployment.
- Consider using
uint128for balances to pack storage - Implement batch operations for multiple transactions
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This contract is for educational purposes. Use at your own risk and conduct thorough audits before deploying to mainnet with real funds.