-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.sol
187 lines (153 loc) · 5.88 KB
/
Bank.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/// @title Bank
/// @author Andreas Olofsson ([email protected])
contract Bank {
// The bank account struct.
struct Account {
address owner;
uint balance;
}
// Null address
address constant ADDRESS_NULL = 0;
// Error codes.
// General
uint8 constant SUCCESS = 0; // Success
// Account related.
uint8 constant NO_ACCOUNT = 10; // Caller has no account.
uint8 constant ACCOUNT_EXISTS = 11; // Caller already has an account.
uint8 constant NO_TARGET = 12; // Recipient does not have an account.
uint8 constant NOT_OWNER = 13; // Caller is not owner of bank.
uint8 constant NOT_ACCOUNT_OWNER = 14; // Caller is not owner of bank account.
uint8 constant INVALID_NAME = 15; // Name is invalid.
// Balance related
uint8 constant NO_AMOUNT = 20; // Transacted amount is 0
uint8 constant INSUFFICIENT_BALANCE = 21; // Insufficient balance.
// Fields
// The owner of the contract.
address public owner;
// The accounts.
mapping(bytes32 => Account) accounts;
mapping(address => bytes32) public names;
event LogPaymentBusMade(bytes32 accountId, uint amount, uint busId);
// Constructor
function Bank(){
owner = msg.sender;
}
/// @dev Registers if not already registered.
/// @param name - the user name.
/// @return Error code. INVALID_NAME || ACCOUNT_EXISTS || ADDRESS_EXISTS || SUCCESS
function registerNewAccount(bytes32 name) external returns (uint16 result) {
if (name == "")
return INVALID_NAME;
if (names[msg.sender] != "")
return ACCOUNT_EXISTS;
accounts[name] = Account(msg.sender, 0);
names[msg.sender] = name;
return SUCCESS;
}
/// @dev Deletes an existing account. This can be done only by the bank owner or the owner of the account.
/// @param name - the user name.
/// @return Error code. NO_ACCOUNT || NOT_ACCOUNT_OWNER || SUCCESS
function deleteAccount(bytes32 name) external returns (uint16 results) {
var acc = accounts[name];
var accOwner = acc.owner;
if (accOwner == ADDRESS_NULL)
return NO_ACCOUNT;
if (accOwner != msg.sender && msg.sender != owner)
return NOT_ACCOUNT_OWNER;
delete accounts[name];
if (msg.sender != owner || (msg.sender == owner && acc.owner == owner)) {
delete names[msg.sender];
}
return SUCCESS;
}
/// @dev Used by the owner to endow an account with GhavCoin
/// @param user The user name.
/// @param amount The amount.
/// @param message The message.
/// @return Error code. NO_AMOUNT || NOT_OWNER || NO_TARGET || SUCCESS
function endow(bytes32 user, uint amount, bytes32 message) external returns (uint16 result) {
if (amount == 0)
return NO_AMOUNT;
if (msg.sender != owner)
return NOT_OWNER;
var account = accounts[user];
accounts[user].balance += amount;
return SUCCESS;
}
function payment(bytes32 user, uint amount, bytes32 message) external returns (uint16 result) {
if (amount == 0)
return NO_AMOUNT;
if (msg.sender != owner)
return NOT_OWNER;
var account = accounts[user];
if (accounts[user].balance < amount) {
return INSUFFICIENT_BALANCE;
}else {
accounts[user].balance -= amount;
LogPaymentBusMade(user, amount, 11);
return SUCCESS;
}
}
/// @dev Transfer Ghavcoin between accounts. The sender is always the account that called the function.
/// @param to The recipients public address.
/// @param amount The amount.
/// @param message The message.
/// @return Error code. NO_AMOUNT || INSUFFICIENT_BALANCE || NO_ACCOUNT || NO_TARGET || SUCCESS
function transfer(bytes32 to, uint amount, bytes32 message) external returns (uint16 result) {
if (amount == 0)
return NO_AMOUNT;
var sndr = names[msg.sender];
if (sndr == "")
return NO_ACCOUNT;
var account = accounts[sndr];
if (account.owner == ADDRESS_NULL)
return NO_ACCOUNT;
// TODO should be a check that user isn't transferring to himself, and an error to go with it.
if (account.balance < amount)
return INSUFFICIENT_BALANCE;
var target = accounts[to];
if (target.owner == ADDRESS_NULL)
return NO_TARGET;
else {
target.balance += amount;
account.balance -= amount;
}
}
/// @dev Get the balance of a user account.
/// @param user The users public address.
/// @return The users current balance (non existing users would have 0).
function getBalance(bytes32 user) external returns (uint accountBalance) {
return accounts[user].balance;
}
// Convenience method.
function getMyAddress() external returns (address myAddress) {
myAddress = msg.sender;
return;
}
// Convenience method.
function getMyName() external returns (bytes32 myName) {
myName = names[msg.sender];
return;
}
/// @dev Used by the owner to remove (suicide) the contract.
function remove() external {
if(msg.sender == owner)
suicide(owner);
}
function bytes32ToString(bytes32 x) constant returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
}