-
Notifications
You must be signed in to change notification settings - Fork 88
/
nft-fixprice-sale-v2.fc
284 lines (241 loc) · 7.2 KB
/
nft-fixprice-sale-v2.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
;; NFT sale smart contract v2
int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
_ 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
);
}
_ 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) 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)
.end_cell()
);
}
() send_money(slice address, int amount) 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)
.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
);
var nft_msg = begin_cell()
.store_uint(0x18, 6)
.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(sender_address) ;; new_owner_address
.store_slice(sender_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(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
);
}
() 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
) = load_data();
;; Throw if sale is complete
throw_if(404, is_complete == 1);
var is_initialized = nft_owner_address.slice_bits() > 2; ;; not initialized if null address
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));
int op = in_msg_body~load_uint(32);
throw_unless(501, op == op::ownership_assigned());
int query_id = in_msg_body~load_uint(64);
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
);
return ();
}
if (in_msg_body.slice_empty?()) {
buy(
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell,
my_balance,
msg_value,
sender_address,
0
);
return ();
}
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
if (op == 1) { ;; just accept coins
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 ();
}
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(), 64);
save_data(
1,
created_at,
marketplace_address,
nft_address,
nft_owner_address,
full_price,
fees_cell
);
return ();
}
throw(0xffff);
}
() recv_external(slice in_msg) impure {
}
(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
) = load_data();
var (
marketplace_fee_address,
marketplace_fee,
royalty_address,
royalty_amount
) = load_fees(fees_cell);
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
);
}