-
Notifications
You must be signed in to change notification settings - Fork 84
/
nft-auction-v3r3.func
503 lines (433 loc) · 14.9 KB
/
nft-auction-v3r3.func
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include "../stdlib.fc";
#include "struct/op-codes.func";
#include "struct/exit-codes.func";
#include "struct/math.func";
#include "struct/msg-utils.func";
;;
;; see https://github.com/getgems-io/nft-contracts
;;
;;
;; persistant and runtime storage description
;;
global int init?; ;; init_data safe check
global int end?; ;; end auction or not
global slice mp_addr; ;; the address of the marketplace from which the contract is deployed
global int activated?; ;; contract is activated by external message or by nft transfer
global int created_at?; ;; timestamp of created acution
global int is_canceled?; ;; auction was cancelled by owner
global cell fees_cell;
global cell constant_cell;
;; bids info cell (ref)
global int min_bid; ;; minimal bid
global int max_bid; ;; maximum bid
global int min_step; ;; minimum step (can be 0)
global slice last_member; ;; last member address
global int last_bid; ;; last bid amount
global int last_bid_at; ;; timestamp of last bid
global int last_query_id; ;; last processed query id
global int end_time; ;; unix end time
global int step_time; ;; by how much the time increases with the new bid (e.g. 30)
global int mp_fee_factor;
global int mp_fee_base;
global int royalty_fee_factor;
global int royalty_fee_base;
;; nft info cell (ref)
global slice nft_owner; ;; nft owner addres (should be sent nft if auction canceled or money from auction)
global slice nft_addr; ;; nft address
() pack_data() impure inline_ref {
set_data(
begin_cell()
.store_int(end?, 1) ;; + stc 1
.store_int(is_canceled?, 1) ;; 1
.store_slice(last_member) ;; + max 267 ($10 with Anycast = 0)
.store_coins(last_bid) ;; + max 124
.store_uint(last_bid_at, 32) ;; + stc 32
.store_uint(end_time, 32) ;; + stc 32
.store_slice(nft_owner) ;; 267
.store_uint(last_query_id, 64)
.store_uint(mp_fee_factor, 32)
.store_uint(mp_fee_base, 32)
.store_uint(royalty_fee_factor, 32)
.store_uint(royalty_fee_base, 32)
.store_ref(fees_cell) ;; + ref
.store_ref(constant_cell) ;; + ref
.end_cell() ;; total 267 + 124 + 32 + 32 + 267 + 1 + 1 + 1 + 64 = 789
);
}
(slice, slice) get_fees_addresses() inline_ref {
slice fees = fees_cell.begin_parse();
slice mp_fee_addr = fees~load_msg_addr();
slice royalty_fee_addr = fees~load_msg_addr();
return (
mp_fee_addr,
royalty_fee_addr
);
}
() init_data() impure inline_ref {- save for get methods -} {
ifnot(null?(init?)) { return ();}
slice ds = get_data().begin_parse();
end? = ds~load_int(1);
is_canceled? = ds~load_int(1);
last_member = ds~load_msg_addr();
last_bid = ds~load_coins();
last_bid_at = ds~load_uint(32);
end_time = ds~load_uint(32);
nft_owner = ds~load_msg_addr();
last_query_id = ds~load_uint(64);
mp_fee_factor = ds~load_uint(32);
mp_fee_base = ds~load_uint(32);
royalty_fee_factor = ds~load_uint(32);
royalty_fee_base = ds~load_uint(32);
activated? = nft_owner.slice_bits() > 2;
fees_cell = ds~load_ref();
constant_cell = ds~load_ref();
slice constants = constant_cell.begin_parse();
mp_addr = constants~load_msg_addr();
min_bid = constants~load_coins();
max_bid = constants~load_coins();
min_step = constants~load_uint(7);
step_time = constants~load_uint(17);
nft_addr = constants~load_msg_addr();
created_at? = constants~load_uint(32);
init? = true;
}
{-
SHOULD
[+] check init auction or not
[+] check op
[+] change nft owner
[+] change auction status
-}
() handle::try_init_auction(slice sender_addr, slice in_msg_body) impure inline_ref {
throw_if(exit::auction_init(), nft_owner.slice_bits() > 2); ;; throw if auction already init
throw_unless(exit::no_transfer(), in_msg_body~load_uint(32) == op::ownership_assigned()); ;; throw if it`s not ownership assigned
in_msg_body~skip_bits(64); ;; query id
nft_owner = in_msg_body~load_msg_addr();
end? = false;
activated? = true;
pack_data();
}
() handle::cancel(slice sender_addr) impure inline_ref {
builder nft_transfer_body = begin_cell()
.store_uint(op::transfer(), 32)
.store_uint(cur_lt(), 64) ;; query id
.store_slice(nft_owner) ;; return nft no creator
.store_slice(sender_addr) ;; response_destination
.store_uint(0, 1) ;; custom payload
.store_coins(0) ;; forward amount
.store_uint(0, 1); ;; forward payload
builder nft_return_msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(nft_addr)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(nft_transfer_body.end_cell());
send_raw_message(nft_return_msg.end_cell(), 130);
end? = true;
is_canceled? = true;
pack_data();
}
() handle::end_auction(slice sender_addr, int from_external) impure inline_ref {
if (last_bid == 0) { ;; just return nft
if (from_external == true) {
[int my_balance, _] = get_balance();
throw_if(exit::low_bid(), my_balance < 500000000); ;; 0.5 ton
accept_message();
}
handle::cancel(sender_addr);
return ();
}
var (
mp_fee_addr,
royalty_fee_addr
) = get_fees_addresses();
int mp_fee = math::get_percent(last_bid, mp_fee_factor, mp_fee_base);
int royalty_fee = math::get_percent(last_bid, royalty_fee_factor, royalty_fee_base);
int profit = last_bid - mp_fee - royalty_fee;
if (from_external == true) {
;; if profit less 0.5 TON then prevent end auc by external message
throw_if(exit::low_bid(), profit < 500000000);
;; we will send 0.5 ton to nft
;; and it returns to nft_owner
;; because when from_external == true sender_addr should be nft_owner
profit = profit - 500000000;
accept_message();
}
if (mp_fee > 0) {
builder mp_transfer = begin_cell()
.store_uint(0x10, 6) ;; 0 (int_msg_info) 1 (ihr_disabled) 1 (no bounces) 00 (address)
.store_slice(mp_fee_addr)
.store_coins(mp_fee)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(msg::mp_msg());
send_raw_message(mp_transfer.end_cell(), 2);
}
if (royalty_fee > 0) {
builder royalty_transfer = begin_cell()
.store_uint(0x10, 6) ;; 0 (int_msg_info) 1 (ihr_disabled) 1 (no bounces) 00 (address)
.store_slice(royalty_fee_addr)
.store_coins(royalty_fee)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(msg::royalty_msg());
send_raw_message(royalty_transfer.end_cell(), 2);
}
if (profit > 0) {
builder prev_owner_msg = begin_cell()
.store_uint(0x10, 6) ;; 0 (int_msg_info) 1 (ihr_disabled) 1 (no bounces) 00 (address)
.store_slice(nft_owner)
.store_coins(profit)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(msg::profit_msg());
send_raw_message(prev_owner_msg.end_cell(), 2);
}
builder nft_transfer_body = begin_cell()
.store_uint(op::transfer(), 32)
.store_uint(cur_lt(), 64) ;; query id
.store_slice(last_member) ;; new owner
.store_slice(sender_addr) ;; response_destination
.store_uint(0, 1) ;; custom payload
.store_coins(1) ;; forward amount 1 nano ton
.store_uint(0, 1); ;; forward payload
builder nft_transfer = begin_cell()
.store_uint(0x18, 6)
.store_slice(nft_addr)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(nft_transfer_body.end_cell());
send_raw_message(nft_transfer.end_cell(), 130); ;; 128 +2 for ignoring errors
end? = true;
end_time = now();
pack_data();
}
;;
;; main code
;;
() return_last_bid(int my_balance) impure inline_ref {
if (last_bid <= 0) {
return ();
}
int return_bid_amount = last_bid - 5577000; ;; 0,005577 TON magic gas price per bid processing
if (return_bid_amount > (my_balance - 10000000)) { ;; - 0.01 TON
return_bid_amount = my_balance - 10000000;
}
if (return_bid_amount > 0) {
slice msg = msg::bid_return();
builder return_prev_bid = begin_cell()
.store_uint(0x18, 6)
.store_slice(last_member)
.store_coins(return_bid_amount)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(msg);
send_raw_message(return_prev_bid.end_cell(), 2);
}
}
(int,int) get_command_code(slice s) inline_ref {
if (slice_empty?(s) == true) {
return (0,0);
}
int op = s~load_uint(32);
int query_id = 0;
if (equal_slices(msg::cancel_msg(), s)) {
return (1,query_id);
} elseif (equal_slices(msg::stop_msg(), s)) {
return (2,query_id);
} elseif (equal_slices(msg::finish_msg(), s)) {
return (2,query_id); ;; 2 its ok
} elseif (equal_slices(msg::deploy(), s)) {
return (3,query_id);
} else {
if (slice_bits(s) >= 64) {
query_id = s~load_uint(64);
}
return (op,query_id); ;; return op if it's not a message
}
}
int get_next_min_bid() {
return max(
last_bid + 100000000,
math::get_percent(last_bid, 100 + min_step, 100)
);
}
() recv_internal(int my_balance, int msg_value, cell in_msg_cell, slice in_msg_body) impure {
slice cs = in_msg_cell.begin_parse();
throw_if(0, cs~load_uint(4) & 1);
slice sender_addr = cs~load_msg_addr();
init_data();
if (equal_slices(sender_addr, nft_addr) & (end? == false)) {
handle::try_init_auction(sender_addr, in_msg_body);
return ();
}
var (op, query_id) = get_command_code(in_msg_body);
if (op == 555) {
throw_unless(exit::not_cancel(), ((end? == true) | (activated? == false)));
throw_unless(403, equal_slices(sender_addr, mp_addr));
;; way to fix unexpected troubles with auction contract
;; for example if some one transfer nft to this contract
var msg = in_msg_body~load_ref().begin_parse();
var mode = msg~load_uint(8);
throw_if(exit::bad_mode(), mode & 32);
int ten_min = 10 * 60;
throw_if(exit::last_bid_too_close(), (now() > (end_time - ten_min)) & (now() < (end_time + ten_min)));
if (last_bid_at != 0 ) {
throw_if(exit::last_bid_too_close(), (now() > (last_bid_at - ten_min)) & (now() < (last_bid_at + ten_min)));
}
send_raw_message(msg~load_ref(), mode);
return ();
}
if (op == 1) { ;; cancel command, return nft, return last bid
throw_if(exit::auction_end(), now() >= end_time); ;; after timeout can't cancel
throw_if(exit::auction_end(), end? == true); ;; already canceled/ended
throw_if(exit::not_activated_yet(), activated? == false);
throw_if(exit::low_amount(), msg_value < 100000000);
throw_if(exit::cant_cancel_bid(), last_bid > 0); ;; can't cancel if someone already placed a bid
throw_unless(403, equal_slices(sender_addr, nft_owner) | equal_slices(sender_addr, mp_addr));
handle::cancel(sender_addr);
return ();
}
if (op == 2) { ;; stop auction
throw_if(exit::auction_end(), end? == true); ;; end = true mean this action already executed
throw_if(exit::not_activated_yet(), activated? == false);
throw_if(exit::low_amount(), msg_value < 100000000);
throw_if(exit::cant_stop_time(), now() < end_time); ;; can't end auction in progress, only after end time
throw_unless(403, equal_slices(sender_addr, nft_owner) | equal_slices(sender_addr, mp_addr) | equal_slices(sender_addr, last_member));
handle::end_auction(sender_addr, false);
return ();
}
if (op == 3) {
;; just accept coins
return ();
}
if (activated? == false) {
throw(exit::not_activated_yet());
return ();
}
if ((end? == true) | (now() >= end_time)) {
throw(exit::auction_end());
return ();
}
;; new bid
var profitable = math::check_profitable(mp_fee_factor, mp_fee_base, royalty_fee_factor, royalty_fee_base);
throw_if(exit::non_profitable(), profitable == 0); ;; decline bids for invalid contract
int duration = end_time - now();
;; auction large than 20 days not allowed
throw_if(exit::its_too_log_auc(), duration > 60 * 60 * 24 * 20);
;; max bid buy nft
if ((msg_value >= max_bid + 100000000) & (max_bid > 0)) { ;; 0.1 TON
;; end aution for this bid
return_last_bid(my_balance);
last_member = sender_addr;
last_bid = max_bid;
last_bid_at = now();
last_query_id = query_id;
handle::end_auction(sender_addr, false);
return ();
}
;; prevent bid at last second
if ((end_time - step_time) < now()) {
end_time += step_time;
}
ifnot(last_bid) {
throw_if(exit::low_bid(), msg_value < min_bid);
last_bid = msg_value;
last_member = sender_addr;
last_bid_at = now();
last_query_id = query_id;
pack_data();
return ();
}
int new_min_bid = get_next_min_bid();
if (msg_value < new_min_bid) {
throw(exit::low_bid());
return ();
}
return_last_bid(my_balance);
last_member = sender_addr;
last_bid = msg_value;
last_bid_at = now();
last_query_id = query_id;
pack_data();
}
{-
Message for deploy contract external
-}
() recv_external(slice in_msg) impure {
init_data();
var (op, _) = get_command_code(in_msg);
if (op == 2) { ;; stop auction
throw_if(exit::not_activated_yet(), activated? == false);
throw_if(exit::auction_end(), end? == true); ;; end = true mean this action already executed
throw_if(exit::cant_stop_time(), now() < end_time); ;; can't end auction in progress, only after end time
handle::end_auction(nft_owner, true);
accept_message();
return ();
}
throw(0xffff);
}
;; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
(int, int, int, slice, slice, slice, int, slice, int, slice, int, int, slice, int, int, int, int, int, int, int) get_sale_data() method_id {
init_data();
var (
mp_fee_addr,
royalty_fee_addr
) = get_fees_addresses();
var profitable = math::check_profitable(mp_fee_factor, mp_fee_base, royalty_fee_factor, royalty_fee_base);
throw_if(exit::non_profitable(), profitable == 0);
return (
0x415543, ;; 1 nft aucion ("AUC")
end?, ;; 2
end_time, ;; 3
mp_addr, ;; 4
nft_addr, ;; 5
nft_owner, ;; 6
last_bid, ;; 7
last_member, ;; 8
min_step, ;; 9
mp_fee_addr, ;; 10
mp_fee_factor, mp_fee_base, ;; 11, 12
royalty_fee_addr, ;; 13
royalty_fee_factor, royalty_fee_base, ;; 14, 15
max_bid, ;; 16
min_bid, ;; 17
created_at?, ;; 18
last_bid_at, ;; 19
is_canceled? ;; 20
);
}
(int, int, int, slice, slice, slice, int, slice, int, slice, int, int, slice, int, int, int, int, int, int, int, int, int) get_auction_data() method_id {
init_data();
var (
mp_fee_addr,
royalty_fee_addr
) = get_fees_addresses();
var profitable = math::check_profitable(mp_fee_factor, mp_fee_base, royalty_fee_factor, royalty_fee_base);
throw_if(exit::non_profitable(), profitable == 0);
if (last_bid > 0) {
min_bid = get_next_min_bid();
}
return (
activated?, ;;1
end?, ;; 2
end_time, ;; 3
mp_addr, ;; 4
nft_addr, ;; 5
nft_owner, ;; 6
last_bid, ;; 7
last_member, ;; 8
min_step, ;; 9 min step
mp_fee_addr, ;; 10
mp_fee_factor, mp_fee_base, ;; 11, 12
royalty_fee_addr, ;; 13
royalty_fee_factor, royalty_fee_base, ;; 14, 15
max_bid, ;; 16
min_bid, ;; 17
created_at?, ;; 18
last_bid_at, ;; 19
is_canceled?, ;; 20
step_time, ;; 21
last_query_id ;; 22
);
}