-
Notifications
You must be signed in to change notification settings - Fork 7
/
yourbankaccount.sol
69 lines (48 loc) · 1.85 KB
/
yourbankaccount.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract YourSmartBank {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
error cannotSendEth();
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
event AddBalance(address indexed fromAddress, uint256 amountDeposited);
event WithdrawAmount(address indexed toAddress, uint256 amountWithdrawn);
receive() external payable {
revert cannotSendEth();
}
mapping (address => uint256) accountBalance;
function getContractBalance() public view returns(uint256) {
return address(this).balance;
}
function addBalance() public payable {
require(msg.value > 0, "Enter a valid amount");
accountBalance[msg.sender] += msg.value;
emit AddBalance(msg.sender, msg.value);
}
function getBalance() public view returns (uint256) {
return accountBalance[msg.sender];
}
// Avoids Re-Entrancy
function withdraw(uint256 _amountToWithdraw) public nonReentrant {
require(_amountToWithdraw <= getBalance(), "Insufficient Funds");
accountBalance[msg.sender] -= _amountToWithdraw;
payable(msg.sender).transfer(_amountToWithdraw);
emit WithdrawAmount(msg.sender, _amountToWithdraw);
}
fallback() external payable {
revert error cannotSendEth();
}
}