-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathFixedNaiveBank.sol
84 lines (64 loc) · 3.12 KB
/
FixedNaiveBank.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: BSL-1.0 (Boost Software License 1.0)
//-------------------------------------------------------------------------------------//
// Copyright (c) 2022 - 2023 serial-coder: Phuwanai Thummavet ([email protected]) //
//-------------------------------------------------------------------------------------//
// For more info, please refer to my article:
// - On Medium: https://medium.com/valixconsulting/solidity-security-by-example-13-double-spending-2-609ba4402aca
// - On serial-coder.com: https://www.serial-coder.com/post/solidity-smart-contract-security-by-example-13-double-spending-02/
pragma solidity 0.8.20;
// This contract still has a denial-of-service issue, but it is not the scope of
// this example though. Therefore, please do not use this contract code in production
contract FixedNaiveBank {
uint256 public constant INTEREST_RATE = 5; // 5% interest
struct Account {
bool registered; // FIX: Use the 'registered' attribute to keep track of every registered account
uint256 balance;
}
mapping (address => Account) private userAccounts;
address[] private userAddresses;
address public immutable owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(owner == msg.sender, "You are not the owner");
_;
}
function depositBankFunds() external payable onlyOwner {
require(msg.value > 0, "Require some funds");
}
function deposit() external payable {
require(msg.value > 0, "Require some funds");
// FIX: Use the 'registered' attribute to keep track of every registered account
if (!userAccounts[msg.sender].registered) {
// Register new user
userAddresses.push(msg.sender);
userAccounts[msg.sender].registered = true;
}
userAccounts[msg.sender].balance += msg.value;
}
function withdraw(uint256 _withdrawAmount) external {
require(userAccounts[msg.sender].balance >= _withdrawAmount, "Insufficient balance");
userAccounts[msg.sender].balance -= _withdrawAmount;
(bool success, ) = msg.sender.call{value: _withdrawAmount}("");
require(success, "Failed to send Ether");
}
// There is a denial-of-service issue on the applyInterest() function,
// but it is not the scope of this example though
function applyInterest() external onlyOwner returns (uint256 minBankBalanceRequired_) {
for (uint256 i = 0; i < userAddresses.length; i++) {
address user = userAddresses[i];
uint256 balance = userAccounts[user].balance;
// Update user's compound interest
userAccounts[user].balance = balance * (100 + INTEREST_RATE) / 100;
// Calculate the minimum bank balance required to pay for each user
minBankBalanceRequired_ += userAccounts[user].balance;
}
}
function getBankBalance() external view returns (uint256) {
return address(this).balance;
}
function getUserBalance(address _user) external view returns (uint256) {
return userAccounts[_user].balance;
}
}