Introduction
This project demonstrates a Solidity smart contract named Assessment designed for managing Ether deposits, withdrawals, and transfers. It integrates error-handling techniques and connects to a React-based frontend for decentralized application (DApp) interactions.
Contract Overview
The Assessment contract enforces an onlyOwner modifier to restrict key operations (deposit, withdraw, transfer) to the contract owner. Key features:
- Deposit/Withdraw/Transfer Functions
- Balance Tracking
- Event Logging
Core Functions
| Function | Description |
|---|---|
deposit(uint256) | Owner deposits Ether into the contract. Emits Deposit event. |
withdraw(uint256) | Owner withdraws Ether. Checks balance sufficiency. Emits Withdraw event. |
transfer(address,uint256) | Transfers Ether to a specified address. Emits Transfer event. |
getBalance() | Returns the contract's current Ether balance (read-only). |
Events
Deposit(uint256 amount)Withdraw(uint256 amount)Transfer(address indexed to, uint256 amount)
๐ Learn more about smart contract security best practices
Frontend Integration (React DApp)
The React interface enables users to:
- Connect MetaMask wallets.
- Deposit/withdraw Ether.
- Transfer funds via a user-friendly UI.
Key Components
- Wallet Connection:
getWalletandconnectAccountfunctions handle MetaMask integration. - Contract Interaction: Functions like
deposit,withdraw, andtransferFundsuse ethers.js to call smart contract methods. - Dynamic UI: Displays real-time balance updates and transaction forms.
Project Requirements
- GitHub Submission: Public repository with
README.mddetailing project purpose. - Video Demonstration: 5-minute review of three contracts (optional for this project).
- Code Quality: MIT-licensed, modular Solidity code with error handling.
Usage Instructions
Local Development Setup
Clone the repository and run:
npm i npx hardhat node npx hardhat run --network localhost scripts/deploy.js npm run dev- Access the DApp at
http://localhost:3000.
MetaMask Configuration
- RPC URL:
https://8545-metacrafterc-scmstarter-msotxik1s10.ws-us104.gitpod.io - Chain ID:
31337 - Currency Symbol:
ETH
๐ Troubleshoot MetaMask connection issues
FAQ
How do I deposit Ether into the contract?
Call the deposit function via the DApp or directly through Etherscan, specifying the amount in Wei.
What happens if a non-owner tries to withdraw?
The onlyOwner modifier reverts the transaction with an error.
Can I test this without real ETH?
Yes! Deploy to a local Hardhat network or testnets like Goerli.
License
MIT License.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;