-
Notifications
You must be signed in to change notification settings - Fork 2
/
collectibles.fc
424 lines (384 loc) · 17.4 KB
/
collectibles.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(cell, (slice, int)) ~udict::delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDELGET" "NULLSWAPIFNOT";
(cell, ()) ~udict::set(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTUSET";
(slice, int) udict::get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGET" "NULLSWAPIFNOT";
(cell, int, slice, int) udict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
(int, cell, cell, cell, cell) load_data () inline {
var cs = get_data().begin_parse();
(int pubkey,
cell inventories,
cell specifications,
cell pending_queries,
cell manipulation_data) = (cs~load_uint(256),
cs~load_dict(),
cs~load_dict(),
cs~load_dict(),
cs~load_ref());
return (pubkey, specifications, inventories, pending_queries, manipulation_data);
}
() save_data (int pubkey, cell inventories, cell specifications, cell pending_queries, cell manipulation_data) impure inline {
cell storage = begin_cell().store_uint(pubkey, 256)
.store_dict(specifications)
.store_dict(inventories)
.store_dict(pending_queries)
.store_ref(manipulation_data)
.end_cell();
set_data(storage);
}
() bounce_message (slice addr, int operation, int query_id, int operation_fee, int msg_value) impure inline {
var msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(addr)
.store_uint(0, 4 + 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(operation | 0x80000000, 32)
.store_uint(query_id, 64)
.end_cell();
if(operation_fee) {
raw_reserve(get_balance().pair_first() - msg_value + operation_fee, 0);
send_raw_message(msg, 128);
} else {
send_raw_message(msg, 64);
}
}
() bounce_message_with_data (slice addr, int operation, int query_id, int operation_fee, int msg_value, slice data) impure inline {
var msg = begin_cell()
.store_uint (0x18, 6)
.store_slice(addr)
.store_uint (0, 4 + 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint (operation | 0x80000000, 32)
.store_uint (query_id, 64)
.store_slice (data) ;; TODO check empty bits
.end_cell();
if(operation_fee) {
raw_reserve(get_balance().pair_first() - msg_value + operation_fee, 0);
send_raw_message(msg, 128);
} else {
send_raw_message(msg, 64);
}
}
(cell, ()) ~remember_query(cell pending_queries, int query_id) inline {
pending_queries~udict_set_builder(64, query_id, begin_cell());
return (pending_queries, ());
}
(cell, (int)) ~clean_expired_queries(cell pending_queries, int bound) inline {
bound -= (64 << 32); ;; clean up records expired more than 64 seconds ago
int need_save = 0;
do {
var (pending_queries', i, _, f) = pending_queries.udict_delete_get_min(64);
f~touch();
if (f) {
f = (i < bound);
}
if (f) {
pending_queries = pending_queries';
need_save = -1;
}
} until (~ f);
return (pending_queries, (need_save));
}
(cell, ()) ~put_pieces(cell inventories, int user_id, int piece_id, int amount, int timestamp) inline {
(slice user_slice, int user_found?) = inventories~udict::delete_get?(256, user_id);
cell inventory = new_dict();
if(user_found?) {
inventory = user_slice~load_dict();
}
(slice pieces_data, int pieces_found?) = inventory~udict::delete_get?(32, piece_id);
if (pieces_found?) {
amount += pieces_data~load_uint(32);
}
if(amount) { ;; if amount=0 do not stor anything
inventory~udict::set(32, piece_id, begin_cell().store_uint(amount, 32).end_cell().begin_parse());
;; NOTE if amount > 2**32 exception will be raised automatically
}
ifnot(inventory.dict_empty?()) {
inventories~udict::set(256, user_id, begin_cell().store_dict(inventory).store_uint(timestamp, 32).end_cell().begin_parse());
}
return (inventories, ());
}
(cell, ()) ~take_pieces(cell inventories, int user_id, int piece_id, int amount, int timestamp) inline {
(slice user_slice, int user_found?) = inventories~udict::delete_get?(256, user_id);
;; if not user_found? exception during parsing will be raised automatically
cell inventory = user_slice~load_dict();
(slice pieces_data, int pieces_found?) = inventory~udict::delete_get?(32, piece_id);
int pieces_num = pieces_data~load_uint(32);
pieces_num -= amount;
if(pieces_num) { ;; if pieces_num=0 do not stor anything
inventory~udict::set(32, piece_id, begin_cell().store_uint(pieces_num, 32).end_cell().begin_parse());
;; NOTE if pieces_num < 0 exception will be raised automatically
}
ifnot(inventory.dict_empty?()) {
inventories~udict::set(256, user_id, begin_cell().store_dict(inventory).store_uint(timestamp, 32).end_cell().begin_parse());
}
return (inventories, ());
}
(cell, ()) ~burn_pieces(cell inventories, int user_id, int piece_id, int amount, int timestamp) inline {
if(amount) {
inventories~take_pieces(user_id, piece_id, amount, timestamp);
} else {
(slice user_slice, int user_found?) = inventories~udict::delete_get?(256, user_id);
cell inventory = user_slice~load_dict();
(slice pieces_data, int pieces_found?) = inventory~udict::delete_get?(32, piece_id);
ifnot(inventory.dict_empty?()) {
inventories~udict::set(256, user_id, begin_cell().store_dict(inventory).store_uint(timestamp, 32).end_cell().begin_parse());
}
}
return (inventories, ());
}
(cell, ()) ~burn_inventory(cell inventories, int user_id) inline {
(slice user_slice, int user_found?) = inventories~udict::delete_get?(256, user_id);
return (inventories, ());
}
(cell, ()) ~delete_unused(cell inventories, tuple delete_list, int num, int bound) inline {
repeat(num) {
int user_id = delete_list~list_next();
(slice user_slice, int user_found?) = inventories~udict::delete_get?(256, user_id);
if(user_found?) {
(int x, int last_time_used) = (user_slice~load_uint(1), user_slice~load_uint(32));
throw_if(54, last_time_used > bound);
}
}
return (inventories, ());
}
(cell, ()) ~transfer_pieces(cell inventories, int donor_id, int acceptor_id, int piece_id, int amount, int timestamp) inline {
inventories~take_pieces(donor_id, piece_id, amount, timestamp);
inventories~put_pieces(acceptor_id, piece_id, amount, timestamp);
return (inventories, ());
}
(cell, ()) ~simple_swap(cell inventories, int f_user_id, int f_piece_id, int s_user_id, int s_piece_id, int timestamp) inline {
inventories~transfer_pieces(f_user_id, s_user_id, f_piece_id, 1, timestamp);
inventories~transfer_pieces(s_user_id, f_user_id, s_piece_id, 1, timestamp);
return (inventories, ());
}
(cell, ()) ~set_specification_fast(cell specifications, int piece_id, slice ready_specification) inline {
specifications~udict::set(32, piece_id, ready_specification);
return (specifications, ());
}
(cell, ()) ~set_specification(cell specifications, int piece_id, int rarity, slice title, cell description, cell functional_properties) inline {
specifications~udict::set(32, piece_id, begin_cell()
.store_uint(32, rarity)
.store_uint(8, title.slice_bits() / 8)
.store_slice(title)
.store_ref(description)
.store_ref(functional_properties)
.end_cell()
.begin_parse());
return (specifications, ());
}
(slice, ()) ~^(slice s) inline {
if(s.slice_data_empty?()) {
ifnot(s.slice_refs_empty?()) {
s = s~load_ref().begin_parse();
}
}
return (s,());
}
() recv_external (slice in_msg) {
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
ifnot(pubkey) {
pubkey = in_msg~load_uint(256);
accept_message();
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
return ();
}
in_msg~^(); ;; smc-guidelines: whole message_body in ref
slice signature = in_msg~load_bits(512);
throw_unless(34, check_signature(slice_hash(in_msg), signature, pubkey));
in_msg~^();
( int op, int query_id) = (in_msg~load_uint(8), in_msg~load_uint(64));
(_, int has_query) = pending_queries.udict::get?(64, query_id);
throw_if(47, has_query);
var bound = (now() << 32);
throw_if(48, query_id < bound);
throw_if(49, query_id > (bound + 281474976710656)); ;; Do not process orders which should be stored too long
;; 281474976710656 = 2**48 = 65536 * 2**32 ~ 18 hours
pending_queries~remember_query(query_id);
;; Reason of accepting message and saving data that early is as follows:
;; operations with dictionaries are potentially expensive, with sufficient
;; number of users gas credit will be exhausted before full checks are done
;; At the same time we cannot accept message, but not commit: if exception
;; will be thrown after accept message but before commit we will lost whole
;; balance due to replays
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
accept_message();
commit();
if( op == 0 ) { ;; issue new piece
(int acceptor_id, int piece_id, int amount) = (in_msg~load_uint(64), in_msg~load_uint(32), in_msg~load_uint(32));
inventories~put_pieces(acceptor_id, piece_id, amount, now());
}
if( op == 1 ) { ;; transfer piece
(int donor_id, int acceptor_id, int piece_id, int amount) = (in_msg~load_uint(64),
in_msg~load_uint(256),
in_msg~load_uint(32),
in_msg~load_uint(32));
inventories~transfer_pieces(donor_id, acceptor_id, piece_id, amount, now());
}
if( op == 2 ) { ;; burn pieces or inventory
(int burn_type, int user_id) = (in_msg~load_uint(8), in_msg~load_uint(64));
if (burn_type == 0) { ;; one thing burn
(int piece_id, int amount) = (in_msg~load_uint(32),in_msg~load_uint(32));
;; amount == 0 means burn all
inventories~burn_pieces(user_id, piece_id, amount, now());
}
if (burn_type == 1) { ;; burn whole inventory
inventories~burn_inventory(user_id);
}
}
if ( op == 3 ) { ;; Upgrade contract
ifnot(in_msg.slice_refs_empty?()) {
cell code = in_msg~load_ref();
set_code(code);
}
ifnot(in_msg.slice_refs_empty?()) {
cell data = in_msg~load_ref();
set_data(data);
}
return ();
}
if ( op == 4 ) { ;; Set description
int piece_id = in_msg~load_uint(32);
specifications~set_specification_fast(piece_id, in_msg);
}
if ( op == 5 ) { ;; Delete selected old
int num_to_delete = in_msg~load_uint(16);
int cont? = num_to_delete;
var delete_list = nil;
do {
cont? = in_msg.slice_bits();
if (cont?) {
delete_list = cons(in_msg~load_uint(256), delete_list);
} else {
cont? = in_msg.slice_refs();
if(cont?) {
in_msg = in_msg~load_ref().begin_parse();
}
}
} until ( ~ cont? );
inventories~delete_unused(delete_list, num_to_delete, now() - 3600 * 24 * 365);
}
if ( op == 6 ) { ;; Send message
int mode = in_msg~load_uint(8);
cell msg = in_msg~load_ref();
send_raw_message(msg, mode);
}
if ( op == 7 ) { ;; Swap
(int first_id, int second_id, int f_piece_id, int s_piece_id) = (in_msg~load_uint(64),
in_msg~load_uint(64),
in_msg~load_uint(32),
in_msg~load_uint(32));
inventories~simple_swap(first_id, f_piece_id, second_id, s_piece_id, now());
}
pending_queries~clean_expired_queries(bound);
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
}
() recv_internal (int msg_value, cell in_msg_cell, slice in_msg) {
var cs = in_msg_cell.begin_parse();
var flags = cs~load_uint(4); ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
if (flags & 1) {
;; ignore all bounced messages
return ();
}
var s_addr = cs~load_msg_addr();
if (in_msg.slice_empty?()) {
;; keep money as donation
return ();
}
int op = in_msg~load_uint(32);
if ( op == 0) {
return ();
}
int query_id = in_msg~load_uint(64);
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
if (op == 1) {
int sender_id = slice_hash(s_addr);
(int acceptor_id, int piece_id, int amount) = (in_msg~load_uint(64), in_msg~load_uint(32), in_msg~load_uint(32));
inventories~transfer_pieces(sender_id, acceptor_id, piece_id, amount, now());
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
bounce_message(s_addr, op, query_id, 50000000, msg_value);
return ();
}
if (op == 2) { ;; get full inventory
int user_id = in_msg~load_uint(256);
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
;; if not user_found? exception during parsing will be raised automatically
cell inventory = user_slice~load_dict();
bounce_message_with_data(s_addr, op, query_id, 0, msg_value, inventory.begin_parse());
return ();
}
if (op == 3) { ;; get amount of piece_id owned by user_id
int user_id = in_msg~load_uint(256);
int piece_id = in_msg~load_uint(32);
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
;; if not user_found? exception during parsing will be raised automatically
cell inventory = user_slice~load_dict();
(slice pieces_data, int pieces_found?) = inventory.udict::get?(32, piece_id);
bounce_message_with_data(s_addr, op, query_id, 0, msg_value, pieces_data);
return ();
}
if (op == 4) { ;; touch
int user_id = in_msg~load_uint(256);
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
;; if not user_found? exception during parsing will be raised automatically
cell inventory = user_slice~load_dict();
inventories~udict::set(256, user_id, begin_cell().store_dict(inventory).store_uint(now(), 32).end_cell().begin_parse());
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
bounce_message_with_data(s_addr, op, query_id, 50000000, msg_value, begin_cell().store_int(-1, 1).end_cell().begin_parse());
return ();
}
if (op == 127) { ;; clean up
;; It is possible (while not probable) that pending_queries become large
;; enough that gas_credit in external message processing will be exhausted
;; during checking wether query alredy presenting in pending_queries or not
;; In this case no external messages will be processed ever. Here we may
;; manually clean up expired queries, to prevent this situation.
pending_queries~clean_expired_queries((now() << 32));
save_data(pubkey, specifications, inventories, pending_queries, manipulation_data);
return ();
}
bounce_message( s_addr, 0xffffffff, 0, 0, msg_value); ;; Unknown operation
}
(cell) get_inventory (int user_id) method_id {
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
ifnot(user_found?) {
return null();
}
return user_slice~load_dict();
}
(int) get_amount_of_pieces (int user_id, int piece_id) method_id {
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
ifnot(user_found?) {
return 0;
}
cell inventory = user_slice~load_dict();
(slice pieces_data, int pieces_found?) = inventory.udict::get?(32, piece_id);
ifnot(pieces_found?) {
return 0;
}
return pieces_data~load_uint(32);
}
(cell, cell) get_inventory_with_specifications (int user_id) method_id {
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
(slice user_slice, int user_found?) = inventories.udict::get?(256, user_id);
ifnot(user_found?) {
return (null(), null());
}
cell inventory = user_slice~load_dict();
cell inventory' = inventory;
cell specs_to_return = null();
do {
var (piece_id, _, f) = inventory'~udict::delete_get_min(32);
f~touch();
if (f) {
(slice specification, int sf) = specifications.udict::get?(32, piece_id);
if(sf) {
specs_to_return~udict::set(32, piece_id, specification);
}
}
} until (~ f);
return (inventory, specs_to_return);
}
(slice) get_specification(int piece_id) method_id {
(int pubkey, cell specifications, cell inventories, cell pending_queries, cell manipulation_data) = load_data();
(slice specification, int sf) = specifications.udict::get?(32, piece_id);
return specification;
}