-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankContract.sol
76 lines (58 loc) · 2.24 KB
/
bankContract.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
//every customer will be represented by address.
contract Bank{
mapping(address => uint) ledgerBalance;
mapping (uint=>address) addressIndex;
address public owner;
uint64 public addrCount;
//Use a constructor - Triggered at the time of deployment
constructor() {
//Whoever deploys the contract will be the owner
owner = msg.sender;
}
//custom modifier, Functions with this modifier should pass the require condition
modifier onlyOwner() {
require(msg.sender == owner,"Access Denied");
_;
}
//events
event Depositevent(address depositor ,uint amount) ;
//deposit
function deposit() public payable {
if(ledgerBalance[msg.sender] ==0){
addressIndex[addrCount++] == msg.sender;
}
ledgerBalance[msg.sender] = ledgerBalance[msg.sender] + msg.value;
emit Depositevent(msg.sender,msg.value);
}
//functions to be called by only Owner ,Fns executed only if it passes
function getBankBalance() public view onlyOwner returns(uint){
return address(this).balance;
}
//Send his balance back
function deleteAccount()public {
payable (msg.sender).transfer(ledgerBalance[msg.sender]);
delete ledgerBalance[msg.sender]; //reset values to default
}
function closeBank() public onlyOwner {
//deprecated. Reset the code to default state
// selfdestruct(payable (owner))
//Allow owner to close account only if all other's got their money
// require(addrCount==0,"You can't close the bank when existing users are there");
payable (owner).transfer(address(this).balance);
}
//withdraw
function withdraw(uint amount) public {
require(ledgerBalance[msg.sender] > amount,"Not enough balance");
ledgerBalance[msg.sender] = ledgerBalance[msg.sender] - (amount );
payable (msg.sender).transfer(amount);
}
function mybalance() public view returns (uint){
return ledgerBalance[msg.sender];
}
//Function to Transfer ownership
function transferOwner(address newOwner) public onlyOwner {
owner = newOwner;
}
}