-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleBank.sol
More file actions
83 lines (65 loc) · 3.1 KB
/
SimpleBank.sol
File metadata and controls
83 lines (65 loc) · 3.1 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
pragma solidity ^0.4.13;
contract SimpleBank {
/* Fill in the keyword. Hint: We want to protect our users balance from other contracts*/
mapping (address => uint) private balances;
/* Let's make sure everyone knows who owns the bank. Use the appropriate keyword for this*/
address public owner;
// Events - publicize actions to external listeners
/* Add 2 arguments for this event, an accountAddress and an amount */
event LogDepositMade(address accountAddress, uint amount);
// Constructor, can receive one or many variables here; only one allowed
constructor() public {
/* Set the owner to the creator of this contract */
owner = msg.sender;
}
/// @notice Enroll a customer with the bank, giving them 1000 tokens for free
/// @return The balance of the user after enrolling
function enroll() public returns (uint){
balances[msg.sender] = 1000;
return balances[msg.sender];
}
/// @notice Deposit ether into bank
/// @return The balance of the user after the deposit is made
// Add the appropriate keyword so that this function can receive ether
function deposit() public payable returns (uint) {
/* Add the amount to the user's balance, call the event associated with a deposit,
then return the balance of the user */
balances[msg.sender] += msg.value;
emit LogDepositMade(msg.sender, msg.value);
return balances[msg.sender];
}
/// @notice Withdraw ether from bank
/// @dev This does not return any excess ether sent to it
/// @param withdrawAmount amount you want to withdraw
/// @return The balance remaining for the user
function withdraw (uint256 withdrawAmount) public payable returns (uint remainingBal) {
/* If the sender's balance is at least the amount they want to withdraw,
Subtract the amount from the sender's balance, and try to send that amount of ether
to the user attempting to withdraw. IF the send fails, add the amount back to the user's balance
return the user's balance.*/
if (balances[msg.sender]>=withdrawAmount) {
balances[msg.sender] -=withdrawAmount;
remainingBal = balances[msg.sender];
if(!msg.sender.send(withdrawAmount)){
balances[msg.sender] += withdrawAmount;
}
}
return remainingBal;
}
/// @notice Get balance
/// @return The balance of the user
// A SPECIAL KEYWORD prevents function from editing state variables;
// allows function to run locally/off blockchain
function balance () public view returns (uint) {
/* Get the balance of the sender of this transaction */
return balances[msg.sender];
}
// Fallback function - Called if other functions don't match call or
// sent ether without data
// Typically, called when invalid data is sent
// Added so ether sent to this contract is reverted if the contract fails
// otherwise, the sender's money is transferred to contract
function () {
revert();
}
}