forked from getgems-io/nft-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft-offer-v1r3.fc
489 lines (423 loc) · 12.6 KB
/
nft-offer-v1r3.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
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
;; NFT offer contract v1r3
;; the same v1r2 but with lower fee, profit check and swap_at
;; https://github.com/getgems-io/nft-contracts
#include "imports/stdlib.fc";
#include "op-codes.fc";
slice msg::cancel() asm "<b 124 word cancel| $, b> <s PUSHSLICE";
slice msg::profit() asm "<b 124 word Profit| $, b> <s PUSHSLICE";
slice msg::royalties() asm "<b 124 word Royalty| $, b> <s PUSHSLICE";
slice msg::fee() asm "<b 124 word Marketplace fee| $, b> <s PUSHSLICE";
slice msg::cancel_fee() asm "<b 124 word Offer cancel fee| $, b> <s PUSHSLICE";
int min_acceptable_profit() asm "5000000 PUSHINT"; ;; 0.005 TON
int division(int a, int b) {
;; division with factor
return muldiv(a, 1000000000 {- 1e9 -}, b);
}
int multiply(int a, int b) {
;; multiply with factor
return muldiv (a, b, 1000000000 {- 1e9 -});
}
int get_percent(int a, int percent, int factor) {
if (factor == 0) {
return 0;
} else {
return division(multiply(a, percent), factor);
}
}
(int, int, int) calc_profit(int full_price, int royalty_factor, int marketplace_factor, int marketplace_base, int royalty_base) {
int royalty_amount = get_percent(full_price, royalty_factor, royalty_base);
int marketplace_fee = get_percent(full_price, marketplace_factor, marketplace_base);
int profit_price = full_price - royalty_amount - marketplace_fee;
return (
royalty_amount,
marketplace_fee,
profit_price
);
}
_ load_data() inline {
var ds = get_data().begin_parse();
return (
ds~load_uint(1), ;; is_complete 1
ds~load_uint(32), ;; created_at 32
ds~load_uint(32), ;; finish_at 32
ds~load_uint(32), ;; finish_at 32
ds~load_msg_addr(), ;; marketplace_address 267
ds~load_msg_addr(), ;; nft_address 267
ds~load_msg_addr(), ;; offer_owner_address 267
ds~load_coins(), ;; full_price 127
ds~load_ref(), ;; fees_cell
ds~load_uint(1) ;; can_deploy
);
}
_ load_fees(cell fees_cell) inline {
var ds = fees_cell.begin_parse();
return (
ds~load_msg_addr(), ;; marketplace_fee_address
ds~load_uint(32), ;; marketplace_factor,
ds~load_uint(32), ;; marketplace_base,
ds~load_msg_addr(), ;; royalty_address
ds~load_uint(32), ;; royalty_factor,
ds~load_uint(32) ;; royalty_base,
);
}
() save_data(int is_complete, int created_at, int finish_at, int swap_at, slice marketplace_address, slice nft_address, slice offer_owner_address, int full_price, cell fees_cell) impure inline {
set_data(
begin_cell()
.store_uint(is_complete, 1)
.store_uint(created_at, 32)
.store_uint(finish_at, 32)
.store_uint(swap_at, 32)
.store_slice(marketplace_address)
.store_slice(nft_address)
.store_slice(offer_owner_address)
.store_coins(full_price)
.store_ref(fees_cell)
.store_uint(0, 1) ;; can_deploy
.end_cell()
);
}
() send_money(slice address, int amount, slice msg) impure inline {
var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(address)
.store_coins(amount)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(msg)
.end_cell();
send_raw_message(msg, 1);
}
() swap_nft(var args) impure {
(
int created_at,
int finish_at,
slice marketplace_address,
slice nft_address,
slice offer_owner_address,
int full_price,
cell fees_cell,
int my_balance,
int msg_value,
slice nft_owner_address,
int query_id
) = args;
var (
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base
) = load_fees(fees_cell);
int royalty_amount = get_percent(full_price, royalty_factor, royalty_base);
int marketplace_fee = get_percent(full_price, marketplace_factor, marketplace_base);
;; nft owner got offer value
send_money(nft_owner_address, full_price - marketplace_fee - royalty_amount, msg::profit());
;; Royalty message
if ((royalty_amount > 0) & (royalty_address.slice_bits() > 2)) {
send_money(royalty_address, royalty_amount, msg::royalties());
}
;; Marketplace fee message
if (marketplace_fee > 0) {
send_money(marketplace_fee_address, marketplace_fee, msg::fee());
}
builder nft_transfer = begin_cell()
.store_uint(op::transfer(), 32)
.store_uint(query_id, 64)
.store_slice(offer_owner_address) ;; new_owner_address
.store_slice(nft_owner_address) ;; response_address
.store_int(0, 1) ;; empty custom_payload
.store_coins(10000000) ;; forward amount to new_owner_address 0.01 TON
.store_int(0, 1); ;; no forward_payload
var nft_msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(nft_address)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(nft_transfer.end_cell());
raw_reserve(1000000, 0);
send_raw_message(nft_msg.end_cell(), 128);
;; Set sale as complete
save_data(
1,
created_at, finish_at, now(),
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell
);
}
() recv_internal(int my_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);
if (flags & 1) {
;; ignore all bounced messages
return ();
}
slice sender_address = cs~load_msg_addr();
var (
is_complete,
created_at,
finish_at,
swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell,
can_deploy
) = load_data();
if (can_deploy == 1) {
var (
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base
) = load_fees(fees_cell);
var (
royalty_amount, marketplace_fee, profit_price
) = calc_profit(full_price, royalty_factor, marketplace_factor, marketplace_base, royalty_base);
throw_if(407, profit_price < min_acceptable_profit());
save_data(
is_complete,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
msg_value,
fees_cell
);
return ();
}
int op = -1;
if (in_msg_body.slice_data_empty?() == false) {
op = in_msg_body~load_uint(32);
if ((op == 0) & equal_slices(in_msg_body, msg::cancel())) {
op = 3;
}
}
if ((op == 555) & (is_complete == 1) & equal_slices(sender_address, marketplace_address)) {
;; way to fix unexpected troubles with contract
var msg = in_msg_body~load_ref().begin_parse();
var mode = msg~load_uint(8);
if (swap_at != 0) {
int ten_min = 10 * 60;
throw_if(406, (now() > (swap_at - ten_min)) & (now() < (swap_at + ten_min)));
}
throw_if(405, mode & 32);
send_raw_message(msg~load_ref(), mode);
return ();
}
;; received nft
if (op == op::ownership_assigned()) {
var query_id = in_msg_body~load_uint(64);
slice prev_owner = in_msg_body~load_msg_addr();
if (sender_address.equal_slices(nft_address) & (is_complete == 0) & (msg_value >= 100000000)) {
swap_nft(
(
created_at,
finish_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell,
my_balance,
msg_value,
prev_owner,
query_id
)
);
} else {
;; should return nft back
builder nft_transfer = begin_cell()
.store_uint(op::transfer(), 32)
.store_uint(query_id, 64) ;; query_id
.store_slice(prev_owner) ;; new_owner_address
.store_slice(prev_owner) ;; response_address
.store_int(0, 1) ;; empty custom_payload
.store_coins(0) ;; forward amount to new_owner_address 0.00 TON
.store_int(0, 1); ;; empty forward_payload
var nft_msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(sender_address)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(nft_transfer.end_cell());
send_raw_message(nft_msg.end_cell(), 64);
}
return ();
}
;; Throw if offer is complete
throw_if(404, is_complete == 1);
if ((op == 0) & equal_slices(sender_address, offer_owner_address)) {
;; add value to offer
int new_full_price = msg_value + full_price;
save_data(
is_complete,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
new_full_price,
fees_cell
);
return ();
}
if (op == 3) {
;; cancel offer
throw_unless(458, equal_slices(sender_address, offer_owner_address) | equal_slices(sender_address, marketplace_address));
raw_reserve(1000000, 0); ;; 0.001 TON
if (equal_slices(sender_address, marketplace_address)) {
var coins = in_msg_body~load_coins();
if (coins > 500000000) {
;; MAX 0.5 TON can stole
coins = 500000000;
}
send_money(marketplace_address, coins, msg::cancel_fee()); ;; fee for mp can send message for cancel
var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(offer_owner_address)
.store_coins(0)
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_ref(in_msg_body~load_ref())
.end_cell();
send_raw_message(msg, 128);
} else {
var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(offer_owner_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.end_cell();
send_raw_message(msg, 128);
}
save_data(
1,
created_at, finish_at, 0,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell
);
return ();
}
throw(0xffff);
}
() recv_external(slice in_msg) impure {
var (
is_complete,
created_at,
finish_at,
swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell,
can_deploy
) = load_data();
throw_if(404, is_complete == 1);
if (now() >= finish_at) {
var op = in_msg~load_uint(32);
throw_if(4003, op != 0);
throw_if(4004, slice_refs(in_msg) != 0);
throw_if(4005, slice_bits(in_msg) > 500);
accept_message();
raw_reserve(1000000, 0); ;; 0.001 TON
var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(offer_owner_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(0, 32)
.store_slice(in_msg)
.end_cell();
send_raw_message(msg, 128);
save_data(
1,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell
);
return ();
}
throw(0xfffe);
}
(int, int, int, int, slice, slice, slice, int, slice, int, int, slice, int, int, int) get_offer_data() method_id {
var (
is_complete,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell,
can_deploy
) = load_data();
throw_if(408, can_deploy == 1);
var (
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base
) = load_fees(fees_cell);
var (
royalty_amount, marketplace_fee, profit_price
) = calc_profit(full_price, royalty_factor, marketplace_factor, marketplace_base, royalty_base);
throw_if(409, profit_price < min_acceptable_profit());
return (
0x4f46464552, ;; offer ("OFFER")
is_complete == 1,
created_at, finish_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base,
profit_price
);
}
(int, int, int, int, int, slice, slice, slice, int, slice, int, int, slice, int, int, int) get_offer_data_v2() method_id {
var (
is_complete,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
fees_cell,
can_deploy
) = load_data();
throw_if(408, can_deploy == 1);
var (
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base
) = load_fees(fees_cell);
var (
royalty_amount, marketplace_fee, profit_price
) = calc_profit(full_price, royalty_factor, marketplace_factor, marketplace_base, royalty_base);
throw_if(409, profit_price < min_acceptable_profit());
return (
0x4f46464552, ;; offer ("OFFER")
is_complete == 1,
created_at, finish_at, swap_at,
marketplace_address,
nft_address,
offer_owner_address,
full_price,
marketplace_fee_address,
marketplace_factor, marketplace_base,
royalty_address,
royalty_factor, royalty_base,
profit_price
);
}