forked from cod1ng-studio/TRC20
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.fc
332 lines (252 loc) · 10.1 KB
/
token.fc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;; TRC20 basic token implementation.
(slice, int) dict_get?(cell dict, int key_len, slice index) asm(index dict key_len) "DICTGET" "NULLSWAPIFNOT";
slice clone_slice(slice s) {
return begin_cell().store_slice(s).end_cell().begin_parse();
}
slice pack_addr(int wc, slice addr) {
return begin_cell().store_int(wc, 8).store_slice(addr).end_cell().begin_parse();
}
(slice, slice, int, int, cell, cell, int) load_data(cell data) {
slice ds = data.begin_parse();
int name_size = ds~load_uint(8);
slice name = ds~load_bits(name_size * 8);
int symbol_size = ds~load_uint(8);
slice symbol = ds~load_bits(symbol_size * 8);
var result = (name, symbol, ds~load_uint(8), ds~load_grams(), ds~load_dict(), ds~load_dict(), ds~load_uint(1));
ds.end_parse();
return result;
}
() store_data(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, int initialized) impure {
set_data(begin_cell()
.store_uint(name.slice_bits() / 8, 8)
.store_slice(name)
.store_uint(symbol.slice_bits() / 8, 8)
.store_slice(symbol)
.store_uint(decimals, 8)
.store_grams(total_supply)
.store_dict(balances)
.store_dict(allowed)
.store_uint(initialized, initialized)
.end_cell()
);
}
int _get_balance(cell balances, slice address) {
(slice balance_slice, int found) = balances.dict_get?(264, address);
if (found) {
return balance_slice~load_grams();
} else {
return 0;
}
}
(cell, ()) ~set_balance(cell balances, slice address, int value) {
balances~dict_set_builder(264, address, begin_cell().store_grams(value));
return (balances, ());
}
int _get_allowance(cell allowed, slice owner, slice spender) {
(slice owner_slice, int owner_found) = allowed.dict_get?(264, owner);
if (owner_found) {
cell dict = owner_slice~load_dict();
(slice spender_slice, int spender_found) = dict.dict_get?(264, spender);
if (spender_found) {
return spender_slice~load_grams();
} else {
return 0;
}
} else {
return 0;
}
}
(cell, ()) ~set_allowance(cell allowed, slice owner, slice spender, int value) {
(slice owner_slice, int owner_found) = allowed.dict_get?(264, owner);
cell dict = owner_found ? owner_slice~load_dict() : new_dict();
dict~dict_set_builder(264, spender, begin_cell().store_grams(value));
allowed~dict_set_builder(264, owner, begin_cell().store_dict(dict));
return (allowed, ());
}
;; sender - raw address
() send_message_back(slice sender, int action, int query_id, int query_action) impure {
;; int_msg_info ihr_disabled:1 bounce:1 bounced:0 src:MsgAddress -> 011000
var msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(sender)
.store_grams(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(action, 32)
.store_uint(query_id, 64)
.store_uint(query_action, 32);
send_raw_message(msg.end_cell(), 64); ;; carry all the remaining value of the inbound message
}
;; to_addr - 264bit address
;; address - 264bit address
() send_message(slice to_addr, int action, int query_id, slice address, int value, slice payload) impure {
;; int_msg_info ihr_disabled:1 bounce:1 bounced:0 src:MsgAddress -> 011000
var msg = begin_cell()
.store_uint(0x18, 6)
.store_uint(0x4, 3)
.store_slice(to_addr)
.store_grams(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(action, 32)
.store_uint(query_id, 64)
.store_slice(address)
.store_grams(value);
if (payload.slice_bits() > 0) {
msg = msg.store_ref(
begin_cell()
.store_slice(payload~load_bits(payload.slice_bits()))
.end_cell()
);
}
send_raw_message(msg.end_cell(), 64); ;; carry all the remaining value of the inbound message
}
;; value - MUST BE NON-NEGATIVE
(cell, ()) ~transfer(cell balances, slice from, slice to, int value) impure {
int from_balance = _get_balance(balances, from);
throw_unless(100, value <= from_balance);
balances~set_balance(from, from_balance - value);
balances~set_balance(to, _get_balance(balances, to) + value);
return (balances, ());
}
;; delta_value - can be negative
(cell, int) ~increase_allowance(cell allowed, slice sender, slice spender, int delta_value) impure {
int old_allowed = _get_allowance(allowed, sender, spender);
int new_allowed = old_allowed + delta_value;
throw_unless(103, new_allowed >= 0);
allowed~set_allowance(sender, spender, new_allowed);
return (allowed, new_allowed);
}
() process_rollback(slice sender, int action, slice cs) impure {
slice rollback_address = cs~load_bits(264);
int rollback_value = cs~load_grams();
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, int initialized) = load_data(get_data());
if (action == 0x20000001) { ;; transfer
balances~transfer(sender, rollback_address, rollback_value);
} elseif (action == 0x20000002) { ;; transfer from
allowed~increase_allowance(sender, rollback_address, rollback_value);
}
store_data(name, symbol, decimals, total_supply, balances, allowed, initialized);
}
() recv_internal(int msg_value, cell in_msg_cell, slice in_msg) impure {
slice cs = in_msg_cell.begin_parse();
int flags = cs~load_uint(4);
slice sender_raw = cs~load_msg_addr();
(int sender_wc, slice sender_addr) = parse_var_addr(sender_raw);
slice sender = pack_addr(sender_wc, sender_addr);
cs = in_msg;
if (cs.slice_bits() == 0) { ;; just accept grams
return ();
}
int action = cs~load_uint(32);
if (action == 0xffffffff) { ;; 'not supported' response
int query_id = cs~load_uint(64);
int query_action = cs~load_uint(32);
process_rollback(sender, query_action, cs);
return ();
}
if (flags & 1) {
int query_id = cs~load_uint(64);
process_rollback(sender, action, cs);
return ();
}
if ((action == 0) & (cs.slice_bits() == 0)) { ;; just accept grams
return ();
}
(action, cs) = parse_msg(action, cs);
int query_id = cs~load_uint(64);
if (action > 4) {
if (action < 0x80000000) { ;; not response
send_message_back(sender_raw, 0xffffffff, query_id, action); ;; operation not supported
}
return ();
}
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, int initialized) = load_data(get_data());
slice to = null();
int value = 0;
int event_action = 0;
if (action == 1) { ;; transfer
to = cs~load_bits(264);
value = cs~load_grams();
balances~transfer(sender, to, value);
event_action = 0x20000001;
} elseif (action == 2) { ;; transfer_from
slice from = cs~load_bits(264);
to = cs~load_bits(264);
value = cs~load_grams();
allowed~increase_allowance(from, sender, - value);
balances~transfer(from, to, value);
event_action = 0x20000002;
} elseif (action == 3) { ;; approve
slice spender = cs~load_bits(264);
to = spender;
value = cs~load_grams();
int old_allowed = _get_allowance(allowed, sender, spender);
throw_unless(102, (old_allowed == 0) | (value == 0));
allowed~set_allowance(sender, spender, value);
event_action = 0x20000003;
} elseif (action == 4) { ;; increase/decrease allowance
int is_decrease = cs~load_uint(1);
slice spender = cs~load_bits(264);
to = spender;
int delta_value = cs~load_grams();
value = allowed~increase_allowance(sender, spender, is_decrease ? - delta_value : delta_value);
event_action = 0x20000003;
}
store_data(name, symbol, decimals, total_supply, balances, allowed, initialized);
send_message(to, event_action, query_id, sender, value, cs);
}
() recv_external(slice in_msg) impure {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, int initialized) = load_data(get_data());
throw_unless(60, initialized == 0);
accept_message();
store_data(name, symbol, decimals, total_supply, balances, allowed, 1);
}
;; Returns the name of the token.
slice get_name() method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return clone_slice(name);
}
;; Returns the symbol of the token.
slice get_symbol() method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return clone_slice(symbol);
}
;; Returns the number of decimals the token uses.
int get_decimals() method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return decimals;
}
;; Returns the total token supply.
int get_total_supply() method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return total_supply;
}
;; Returns the account balance of another account with address _owner.
int balance_of(slice owner) method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return _get_balance(balances, owner);
}
;; Returns the amount which _spender is still allowed to withdraw from _owner.
;; @param _owner The address of the account owning tokens.
;; @param _spender The address of the account able to transfer the tokens.
int allowance(slice owner, slice spender) method_id {
(slice name, slice symbol, int decimals, int total_supply, cell balances, cell allowed, _) = load_data(get_data());
return _get_allowance(allowed, owner, spender);
}
;; TEST
int ibalance_of(int owner_wc, int owner_addr) method_id {
slice owner = begin_cell().store_int(owner_wc, 8).store_uint(owner_addr, 256).end_cell().begin_parse();
return balance_of(owner);
}
int iallowance(int owner_wc, int owner_addr, int spender_wc, int spender_addr) method_id {
slice owner = begin_cell().store_int(owner_wc, 8).store_uint(owner_addr, 256).end_cell().begin_parse();
slice spender = begin_cell().store_int(spender_wc, 8).store_uint(spender_addr, 256).end_cell().begin_parse();
return allowance(owner, spender);
}
int test_recv_external(slice in_msg) method_id {
recv_external(in_msg);
return 0;
}
int test_recv_internal(int value, cell in_msg_cell, slice in_msg) method_id {
recv_internal(value, in_msg_cell, in_msg);
return 0;
}