-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathInsecureNaiveBank.sol
75 lines (56 loc) · 2.59 KB
/
InsecureNaiveBank.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
// 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;
contract InsecureNaiveBank {
uint256 public constant INTEREST_RATE = 5; // 5% interest
mapping (address => uint256) private userBalances;
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");
// Register new user
if (userBalances[msg.sender] == 0) {
userAddresses.push(msg.sender);
}
userBalances[msg.sender] += msg.value;
}
function withdraw(uint256 _withdrawAmount) external {
require(userBalances[msg.sender] >= _withdrawAmount, "Insufficient balance");
userBalances[msg.sender] -= _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 = userBalances[user];
// Update user's compound interest
userBalances[user] = balance * (100 + INTEREST_RATE) / 100;
// Calculate the minimum bank balance required to pay for each user
minBankBalanceRequired_ += userBalances[user];
}
}
function getBankBalance() external view returns (uint256) {
return address(this).balance;
}
function getUserBalance(address _user) external view returns (uint256) {
return userBalances[_user];
}
}