|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +contract BankAccount { |
| 5 | + // State variable to store the balance of each account |
| 6 | + mapping(address => uint256) private balances; |
| 7 | + |
| 8 | + // Event to log deposits |
| 9 | + event Deposit(address indexed account, uint256 amount); |
| 10 | + |
| 11 | + // Event to log withdrawals |
| 12 | + event Withdraw(address indexed account, uint256 amount); |
| 13 | + |
| 14 | + // Function to deposit money into the account |
| 15 | + function deposit(uint256 amount) public payable { |
| 16 | + require(amount > 0, "Deposit amount must be greater than zero"); |
| 17 | + // Ensure the value sent with the transaction matches the amount specified |
| 18 | + require(msg.value == amount, "Amount does not match the value sent"); |
| 19 | + |
| 20 | + balances[msg.sender] += msg.value; |
| 21 | + |
| 22 | + emit Deposit(msg.sender, msg.value); |
| 23 | + } |
| 24 | + |
| 25 | + // Function to withdraw money from the account |
| 26 | + function withdraw(uint256 amount) public { |
| 27 | + require(amount > 0, "Withdrawal amount must be greater than zero"); |
| 28 | + require(balances[msg.sender] >= amount, "Insufficient balance"); |
| 29 | + |
| 30 | + balances[msg.sender] -= amount; |
| 31 | + |
| 32 | + // Transfer the amount back to the caller |
| 33 | + payable(msg.sender).transfer(amount); |
| 34 | + |
| 35 | + emit Withdraw(msg.sender, amount); |
| 36 | + } |
| 37 | + |
| 38 | + // Function to check the balance of the account |
| 39 | + function getBalance() public view returns (uint256) { |
| 40 | + return balances[msg.sender]; |
| 41 | + } |
| 42 | +} |
0 commit comments