-
Notifications
You must be signed in to change notification settings - Fork 88
/
nft-fixprice-sale-v3r3.fc
419 lines (360 loc) · 9.57 KB
/
nft-fixprice-sale-v3r3.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
;; NFT sale smart contract v3r3
;; It's a v3r2 but with returning query_id, handling code 32, allow change price
;; see https://github.com/getgems-io/nft-contracts
#include "imports/stdlib.fc";
#include "op-codes.fc";
int min_gas_amount() asm "100000000 PUSHINT"; ;; 0.1 TON
slice msg::cancel_msg() asm "<b 124 word cancel| $, b> <s PUSHSLICE";
() check_std_addr(slice s) impure asm "REWRITESTDADDR" "DROP2";
_ load_data() inline {
var ds = get_data().begin_parse();
return (
ds~load_uint(1), ;; is_complete
ds~load_uint(32), ;; created_at
ds~load_msg_addr(), ;; marketplace_address
ds~load_msg_addr(), ;; nft_address
ds~load_msg_addr(), ;; nft_owner_address
ds~load_coins(), ;; full_price
ds~load_ref(), ;; fees_cell
ds~load_uint(32), ;; sold_at
ds~load_uint(64) ;; query_id
);
}
_ load_fees(cell fees_cell) inline {
var ds = fees_cell.begin_parse();
return (
ds~load_msg_addr(), ;; marketplace_fee_address
ds~load_coins(), ;; marketplace_fee,
ds~load_msg_addr(), ;; royalty_address
ds~load_coins() ;; royalty_amount
);
}
() save_data(int is_complete, int created_at, slice marketplace_address, slice nft_address, slice nft_owner_address, int full_price, cell fees_cell, int sold_at, int query_id) impure inline {
set_data(
begin_cell()
.store_uint(is_complete, 1)
.store_uint(created_at, 32)
.store_slice(marketplace_address)
.store_slice(nft_address)
.store_slice(nft_owner_address)
.store_coins(full_price)
.store_ref(fees_cell)
.store_uint(sold_at, 32)
.store_uint(query_id, 64)
.end_cell()
);
}
() send_money(slice address, int amount) impure inline {
check_std_addr(address);
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)
.end_cell();
send_raw_message(msg, 1);
}
() buy(var args) impure {
(
int created_at,
slice marketplace_address,
slice nft_address,
slice nft_owner_address,
int full_price,
cell fees_cell,
int my_balance,
int msg_value,
slice sender_address,
int query_id
) = args;
throw_unless(450, msg_value >= full_price + min_gas_amount());
var (
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
) = load_fees(fees_cell);
;; Owner message
send_money(
nft_owner_address,
full_price - marketplace_fee - royalty_amount + (my_balance - msg_value)
);
;; Royalty message
if ((royalty_amount > 0) & (royalty_address.slice_bits() > 2)) {
send_money(
royalty_address,
royalty_amount
);
}
;; Marketplace fee message
send_money(
marketplace_fee_address,
marketplace_fee
);
builder nft_transfer = begin_cell()
.store_uint(op::transfer(), 32)
.store_uint(query_id, 64)
.store_slice(sender_address) ;; new_owner_address
.store_slice(sender_address) ;; response_address
.store_int(0, 1) ;; empty custom_payload
.store_coins(30000000) ;; forward amount to new_owner_address 0.03 TON
.store_int(0, 1); ;; empty 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());
check_std_addr(nft_address);
send_raw_message(nft_msg.end_cell(), 128);
;; Set sale as complete
save_data(
1,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
now(),
query_id
);
}
() 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,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
sold_at,
query_id
) = load_data();
int op = 0;
int query_id = 0;
if (in_msg_body.slice_empty?() == false) {
op = in_msg_body~load_uint(32);
}
if (op != 0) {
query_id = in_msg_body~load_uint(64);
} else {
if (equal_slices(msg::cancel_msg(), in_msg_body)) {
op = 3;
}
}
if (op == 1) {
;; just accept coins
return ();
}
var is_initialized = nft_owner_address.slice_bits() > 2; ;; not initialized if null address
if ((op == 555) & ((is_complete == 1) | (~ is_initialized)) & equal_slices(sender_address, marketplace_address)) {
;; way to fix unexpected troubles with sale 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(405, mode & 32);
if (sold_at != 0) {
int ten_min = 10 * 60;
throw_if(406, (now() > (sold_at - ten_min)) & (now() < (sold_at + ten_min)));
}
send_raw_message(msg~load_ref(), mode);
return ();
}
;; Throw if sale is complete
throw_if(404, is_complete == 1);
if ((op == 0x6c6c2080) & equal_slices(sender_address, nft_owner_address)) {
var (new_full_price, new_marketplace_fee, new_royalty_amount) = (in_msg_body~load_coins(), in_msg_body~load_coins(), in_msg_body~load_coins());
int profit_price = new_full_price - new_marketplace_fee - new_royalty_amount;
throw_if(408, profit_price < 1);
var (
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
) = load_fees(fees_cell);
var new_fees_cell = begin_cell()
.store_slice(marketplace_fee_address)
.store_coins(new_marketplace_fee)
.store_slice(royalty_address)
.store_coins(new_royalty_amount)
.end_cell();
save_data(
is_complete,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
new_full_price,
new_fees_cell,
0,
0
);
return ();
}
if (~ is_initialized) {
if (equal_slices(sender_address, marketplace_address)) {
return (); ;; just accept coins on deploy
}
throw_unless(500, equal_slices(sender_address, nft_address));
throw_unless(501, op == op::ownership_assigned());
slice prev_owner_address = in_msg_body~load_msg_addr();
save_data(
is_complete,
created_at,
marketplace_address,
nft_address,
prev_owner_address,
full_price,
fees_cell,
sold_at,
query_id
);
return ();
}
if (op == 3) {
;; cancel sale
throw_unless(457, msg_value >= min_gas_amount());
throw_unless(458, equal_slices(sender_address, nft_owner_address) | equal_slices(sender_address, marketplace_address));
var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(nft_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::transfer(), 32)
.store_uint(query_id, 64)
.store_slice(nft_owner_address) ;; new_owner_address
.store_slice(nft_owner_address) ;; response_address;
.store_int(0, 1) ;; empty custom_payload
.store_coins(0) ;; forward amount to new_owner_address
.store_int(0, 1); ;; empty forward_payload
send_raw_message(msg.end_cell(), 128);
save_data(
1,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
0,
0
);
return ();
}
if (op == 0) {
buy(
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
my_balance,
msg_value,
sender_address,
0
);
return ();
}
if (op == 2) {
;; buy
buy(
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
my_balance,
msg_value,
sender_address,
query_id
);
return ();
}
throw(0xffff);
}
(int, int, int, slice, slice, slice, int, slice, int, slice, int) get_sale_data() method_id {
var (
is_complete,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
sold_at,
query_id
) = load_data();
var (
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
) = load_fees(fees_cell);
int profit_price = full_price - marketplace_fee - royalty_amount;
throw_if(409, profit_price < 1);
check_std_addr(marketplace_address);
check_std_addr(nft_address);
check_std_addr(marketplace_fee_address);
return (
0x46495850, ;; fix price sale ("FIXP")
is_complete == 1,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
);
}
(int, int, slice, slice, slice, int, slice, int, slice, int, int, int) get_fix_price_data() method_id {
var (
is_complete,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
sold_at,
query_id
) = load_data();
var (
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
) = load_fees(fees_cell);
int profit_price = full_price - marketplace_fee - royalty_amount;
throw_if(410, profit_price < 1);
check_std_addr(marketplace_address);
check_std_addr(nft_address);
check_std_addr(marketplace_fee_address);
return (
is_complete == 1,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount,
sold_at,
query_id
);
}