Skip to content

Repository files navigation


VaultManager Smart Contract

A secure and simple Ethereum vault contract that allows users to deposit and withdraw ETH with overflow/underflow protection.

Overview

VaultManager is a Solidity smart contract designed to manage user deposits and withdrawals of ETH in a secure way. It uses a custom Math library to prevent arithmetic overflows and underflows, and enforces proper access control and validation.

Built with Solidity 0.8.30, it leverages modern best practices while maintaining clarity and safety.


Features

  • Deposit ETH – Users can deposit any amount greater than 0.
  • Withdraw ETH – Users can withdraw up to their deposited balance.
  • Safe Math Operations – Custom Math library prevents overflow and underflow.
  • Balance Tracking – Each user’s balance is tracked on-chain.
  • Event Logging – Deposits and withdrawals are emitted as events.
  • Contract Balance Check – Ensures the contract has enough funds before withdrawal.

Contract Structure

Libraries

Math

A simple safe math library for addition and subtraction.

Function Description
add(uint256 a, uint256 b) Returns a + b with overflow check
sub(uint256 a, uint256 b) Returns a - b with underflow check

Note: Solidity 0.8+ has built-in overflow checks, but this library provides explicit, readable safety.


Abstract Contract: VaultBase

Base contract containing shared state, modifiers, and utilities.

State Variables

  • balances[address] – Tracks each user’s deposited ETH balance.

Events

  • Deposited(address indexed user, uint256 amount) – Emitted on deposit.
  • Withdrawn(address indexed user, uint256 amount) – Emitted on withdrawal.

Modifiers

  • validDepositAmount(uint256 amount) – Ensures deposit amount > 0.
  • sufficientBalance(uint256 amount) – Ensures user has enough balance.

View Function

  • getContractBalance() – Returns the current ETH balance of the contract.

Main Contract: VaultManager

Implements core deposit and withdrawal functionality.

Functions

Function Visibility Payable Modifiers Description
deposit() external Yes validDepositAmount Accepts ETH and updates user balance
withdraw(uint256 amount) external ❌ No sufficientBalance Transfers ETH to user if balance and contract funds allow

Uses low-level .call for ETH transfer to avoid issues with contracts that don’t implement receive().


Security Considerations

  • Reentrancy Protection: Not strictly needed here since state is updated before transfer, but consider adding ReentrancyGuard in extended versions.
  • Arithmetic Safety: Math library ensures no overflows/underflows.
  • Balance Validation: Checks both user and contract balance before withdrawal.
  • Use of .call: Safer than .transfer(); includes success check.

Future improvement: Add a reentrancy guard if extending functionality.


Usage Example

Deposit

// Send 1 ETH to deposit
await vaultManager.deposit({ value: ethers.parseEther("1.0") });

Withdraw

// Withdraw 0.5 ETH
await vaultManager.withdraw(ethers.parseEther("0.5"));

Deployment & Integration

Deploy using Hardhat, Foundry, or other Ethereum development tools.

Ensure your deployment environment includes:

  • Solidity compiler ^0.8.30
  • Sufficient ETH in the contract to support withdrawals

License

SPDX-License-Identifier: MIT


Project Structure (Hardhat)

contracts/
  └── VaultManager.sol

test/
  └── vaultManager.test.js  # (You can create this)

scripts/
  └── deploy.js

Future Improvements

  • Add reentrancy guard via OpenZeppelin.
  • Support multiple tokens (ERC-20) in addition to ETH.
  • Add admin functions for emergency withdrawal or fee collection.
  • Implement withdrawAll() convenience function.
  • Add Slippage/timelock for advanced vaults.

Acknowledgements

Built with Hardhat and inspired by secure vault patterns in the Ethereum ecosystem.


Always test on local or testnet before deploying to mainnet.


About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages