-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathblockchain_program.fc
More file actions
377 lines (233 loc) · 9.81 KB
/
blockchain_program.fc
File metadata and controls
377 lines (233 loc) · 9.81 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
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
;; Storage
;;
;; int32 seqno
;; int256 public_key
;; slice dapp_owner
;; int64 next_collection_index
;; cell collections_dict
#pragma version >=0.4.0;
#include "imports/stdlib.fc";
#include "imports/messages.fc";
#include "imports/test-libs/message_helpers.fc";
#include "imports/utils.func";
#include "utils/op-codes.fc";
#include "utils/constants.fc";
#include "utils/params.fc";
(slice, int, cell) load_data() inline_ref {
slice cs = get_data().begin_parse();
return (
cs~load_msg_addr(), ;; slice dapp_owner
cs~load_uint(64), ;; int64 next_collection_index
(cs.slice_bits() > 0 ? cs~load_dict() : new_dict()) ;; cell collections_dict
);
}
() save_data(slice dapp_owner, int next_collection_index, cell collections_dict) impure inline {
set_data(
begin_cell()
.store_slice(dapp_owner) ;; slice dapp_owner
.store_uint(next_collection_index, 64) ;; int256 public_key
.store_dict(collections_dict) ;; cell collections_dict
.end_cell()
);
}
slice calculate_collection_address(cell state_init) {
return begin_cell().store_uint(4, 3)
.store_int(workchain, 8)
.store_uint(cell_hash(state_init), 256)
.end_cell()
.begin_parse();
}
() change_owner(slice new_owner_addr) impure {
(slice dapp_owner, int next_collection_index, cell collections_dict) = load_data();
force_chain(new_owner_addr);
save_data(new_owner_addr, next_collection_index, collections_dict);
}
() withdraw_funds(int my_balance, int withdraw_amount) impure {
throw_unless(407, my_balance + MIN_TONS_FOR_STORAGE > withdraw_amount);
(slice dapp_owner, _, _) = load_data();
messages::send_empty(withdraw_amount, dapp_owner, NORMAL + 1);
}
() edit_code(cell new_code) impure {
set_code(new_code);
cont old_code = get_c3();
set_c3(new_code.begin_parse().bless());
throw(0);
}
(slice) return_collection_addr_by_id (int collection_id) inline_ref {
(_, _, cell collections_dict) = load_data();
(slice value, int f) = udict_get?(collections_dict, 256, collection_id);
throw_unless(666, f);
return value;
}
() deploy_collection (cell collection_code, cell collection_data) impure {
(slice dapp_owner, int next_collection_index, cell collections_dict) = load_data();
cell state_init = (generate_init_state_with_data_no_library(collection_code, collection_data)).end_cell();
slice collection_addr = calculate_collection_address(state_init);
messages::send_with_stateinit(50000000, collection_addr, state_init, (begin_cell().end_cell()), NORMAL);
cell new_collections_dict = collections_dict.udict_set(256, next_collection_index, collection_addr);
next_collection_index += 1;
save_data(dapp_owner, next_collection_index, new_collections_dict);
}
() deploy_item (int collection_id, int query_id, int item_index, int amount, cell nft_content) impure {
slice collection_address = return_collection_addr_by_id(collection_id);
slice in_msg_body = begin_cell()
.store_uint(op::deploy_nft_item, 32)
.store_uint(query_id, 64)
.store_uint(item_index, 64)
.store_coins(amount)
.store_ref(nft_content)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, collection_address, msg_body, CARRY_REMAINING_GAS);
}
() batch_nft_deploy (int collection_id, int query_id, cell deploy_list) impure {
slice collection_address = return_collection_addr_by_id(collection_id);
slice in_msg_body = begin_cell()
.store_uint(op::batch_nft_deploy, 32)
.store_uint(query_id, 64)
.store_ref(deploy_list)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, collection_address, msg_body, CARRY_REMAINING_GAS);
}
() edit_collection_content (int collection_id, int query_id, cell new_content, cell royalty_params) impure {
slice collection_address = return_collection_addr_by_id(collection_id);
slice in_msg_body = begin_cell()
.store_uint(op::edit_collection_content, 32)
.store_uint(query_id, 64)
.store_ref(new_content)
.store_ref(royalty_params)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, collection_address, msg_body, CARRY_REMAINING_GAS);
}
() change_collection_owner (int collection_id, int query_id, slice new_owner_addr) impure {
slice collection_address = return_collection_addr_by_id(collection_id);
slice in_msg_body = begin_cell()
.store_uint(op::change_collection_owner, 32)
.store_uint(query_id, 64)
.store_slice(new_owner_addr)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, collection_address, msg_body, CARRY_REMAINING_GAS);
}
() edit_item_content (slice item_address, int query_id, cell content) impure {
slice in_msg_body = begin_cell()
.store_uint(op::edit_content, 32)
.store_uint(query_id, 64)
.store_dict(content)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, item_address, msg_body, CARRY_REMAINING_GAS);
}
() destroy_sbt_item (slice item_address, int query_id) impure {
slice in_msg_body = begin_cell()
.store_uint(op::destroy, 32)
.store_uint(query_id, 64)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, item_address, msg_body, CARRY_REMAINING_GAS);
}
() revoke_sbt_item (slice item_address, int query_id) impure {
slice in_msg_body = begin_cell()
.store_uint(op::revoke, 32)
.store_uint(query_id, 64)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, item_address, msg_body, CARRY_REMAINING_GAS);
}
() transfer_item (slice item_address, int query_id, slice new_owner_address, slice resp_address, int fwd_amount) impure {
slice in_msg_body = begin_cell()
.store_uint(op::transfer, 32)
.store_uint(query_id, 64)
.store_slice(new_owner_address)
.store_slice(resp_address)
.store_uint(0, 1)
.store_coins(fwd_amount)
.end_cell().begin_parse();
cell msg_body = begin_cell().store_slice(in_msg_body).end_cell();
messages::send_via_proxy(ZERO_AMOUNT, item_address, msg_body, CARRY_REMAINING_GAS);
}
() recv_internal(int balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
slice sender_address = cs~load_msg_addr();
if (flags & 1) {
return ();
}
if (in_msg_body.slice_empty?()) {
return ();
}
(slice dapp_owner, int next_collection_index, cell collections_dict) = load_data();
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
if (op == op::mint_notification) { ;; nft has just been initialized
return ();
}
if (op == op::edit_notification) {
return ();
}
if (op == op::excesses) {
return ();
}
throw_unless(405, equal_slices(dapp_owner, sender_address));
;; Deploy Collection
if (op == op::deploy_collection) {
deploy_collection(in_msg_body~load_ref(), in_msg_body~load_ref());
return ();
}
if (op == op::deploy_nft_item) {
int collection_id = in_msg_body~load_uint(64);
deploy_item(collection_id, query_id, in_msg_body~load_uint(64), MIN_DEPLOY_AMOUNT, in_msg_body~load_ref());
return ();
}
if (op == op::batch_nft_deploy) {
int collection_id = in_msg_body~load_uint(64);
batch_nft_deploy(collection_id, query_id, in_msg_body~load_ref());
return ();
}
if (op == op::change_collection_owner) {
int collection_id = in_msg_body~load_uint(64);
change_collection_owner(collection_id, query_id, in_msg_body~load_msg_addr());
return ();
}
if (op == op::edit_collection_content) {
int collection_id = in_msg_body~load_uint(64);
edit_collection_content(collection_id, query_id, in_msg_body~load_ref(), in_msg_body~load_ref());
return ();
}
if (op == op::edit_item_content) {
edit_item_content(in_msg_body~load_msg_addr(), query_id, in_msg_body~load_ref());
return ();
}
if (op == op::destroy_sbt_item) {
destroy_sbt_item(in_msg_body~load_msg_addr(), query_id);
return ();
}
if (op == op::transfer_item) { ;; transfer order nft to freelancer
transfer_item(in_msg_body~load_msg_addr(), query_id, in_msg_body~load_msg_addr(), in_msg_body~load_msg_addr(), in_msg_body~load_coins());
return ();
}
if (op == op::change_owner) {
change_owner(cs~load_msg_addr());
return ();
}
if (op == op::withdraw_funds) {
withdraw_funds(balance, cs~load_coins());
return ();
}
if (op == op::edit_dapp_code) { ;; edit dapp code
edit_code(cs~load_ref());
return ();
}
throw(0xffffff);
}
;; Getters
(slice, int, cell) get_dapp_data() method_id {
(slice dapp_owner, int next_collection_index, cell collections_dict) = load_data();
return (
dapp_owner,
next_collection_index,
collections_dict
);
}