-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankSystem.sol
More file actions
113 lines (82 loc) · 3.28 KB
/
BankSystem.sol
File metadata and controls
113 lines (82 loc) · 3.28 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
pragma solidity ^0.5.0;
contract Bank {
//Dictionary that maps address to balances
mapping(address => uint256) private balances;
//User in the system
address[] accounts;
//Interest rate
uint256 rate = 3;
//system owner
address public owner;
//Events
event DepositMade(address indexed account, uint amount);
event WithdrawMade(address indexed account, uint amount);
constructor() public {
owner = msg.sender;
}
//User can deposit any amount to the system
function deposit() public payable returns(uint256){
// Record account into array
if (0 == balances[msg.sender]){
accounts.push(msg.sender);
}
balances[msg.sender] = balances[msg.sender] + msg.value;
emit DepositMade(msg.sender, msg.value);
return balances[msg.sender];
}
function withdraw(uint amount)public returns (uint256){
require(balances[msg.sender] >= amount, "Balance is not enought");
balances[msg.sender] -= amount;
// Send money back to user
msg.sender.transfer(amount);
// broadcast event
emit WithdrawMade(msg.sender, amount);
return balances[msg.sender];
}
function systemBalance() public view returns (uint256){
return address(this).balance;
}
function userBalance() public view returns (uint256) {
return balances[msg.sender];
}
event systemWithdrawMade(address indexed account, uint256 amount);
event systemDepositMade(address indexed account, uint256 amount);
function systemWithdraw(uint amount) public returns (uint256){
require(owner == msg.sender, "Your are not authorized");
require(systemBalance() >= amount, "System balance is not enought");
msg.sender.transfer(amount);
// broadcast event
emit systemWithdrawMade(msg.sender, amount);
return systemBalance();
}
function systemDeposit() public payable returns (uint256){
require(owner == msg.sender, "You are not authorized");
// broadcast event
emit systemDepositMade(msg.sender, msg.value);
return systemBalance();
}
function calculateInterest(address user, uint256 _rate)private view returns (uint256){
uint256 interest = balances[user] * _rate / 100;
return interest;
}
function totalInterestPerYear() external view returns (uint256){
uint256 total = 0;
for(uint256 i = 0; i < accounts.length; i++){
address account = accounts[i];
uint256 interest = calculateInterest(account, rate);
total += interest;
}
return total;
}
function payDividendsPerYear() payable public{
require(owner == msg.sender, "You are not authorized");
uint256 totalInterest = 0;
for(uint256 i=0; i < accounts.length ; i++){
address account = accounts[i];
uint256 interest = calculateInterest(account, rate);
balances[account] += interest;
totalInterest += interest;
}
require(msg.value == totalInterest, "Not enought interest to pay!!");
}
}