-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfreelancers_nft.fc
More file actions
255 lines (204 loc) · 8.84 KB
/
freelancers_nft.fc
File metadata and controls
255 lines (204 loc) · 8.84 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
;;
;; TON SBT-Editable Item Smart Contract
;;
#pragma version >=0.4.0;
#include "imports/stdlib.fc";
#include "imports/messages.fc";
#include "utils/constants.fc";
#include "utils/params.fc";
#include "utils/flags.fc";
#include "utils/op-codes.fc";
int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
;;
;; Storage
;;
;; uint64 index
;; MsgAddressInt collection_address
;; MsgAddressInt owner_address
;; cell content
;; MsgAddressInt authority_address
;; MsgAddressInt editor_address
;; uint64 revoked_at
(int, int, slice, slice, cell, slice, slice, int) load_data() inline_ref {
slice ds = get_data().begin_parse();
var (index, collection_address) = (ds~load_uint(64), ds~load_msg_addr());
if (ds.slice_bits() > 0) {
return (-1, index, collection_address, ds~load_msg_addr(), ds~load_ref(), ds~load_msg_addr(), ds~load_msg_addr(), ds~load_uint(64));
} else {
return (0, index, collection_address, null(), null(), null(), null(), null()); ;; nft not initialized yet
}
}
() store_data(int index, slice collection_address, slice owner_address, cell content, slice authority_address, slice editor_address, int revoked_at) impure {
set_data(
begin_cell()
.store_uint(index, 64)
.store_slice(collection_address)
.store_slice(owner_address)
.store_ref(content)
.store_slice(authority_address)
.store_slice(editor_address)
.store_uint(revoked_at, 64)
.end_cell()
);
}
() send_msg(int flag, slice to_address, int amount, int op, int query_id, builder payload, int send_mode) impure inline {
var msg = begin_cell()
.store_uint(flag, 6)
.store_slice(to_address)
.store_coins(amount)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op, 32)
.store_uint(query_id, 64);
if (~ builder_null?(payload)) {
msg = msg.store_builder(payload);
}
send_raw_message(msg.end_cell(), send_mode);
}
() transfer_editorship(int my_balance, int index, slice collection_address, slice owner_address, cell content, slice editor_address, slice sender_address, int query_id, slice in_msg_body, int fwd_fees) impure inline {
throw_unless(401, equal_slices(sender_address, editor_address));
(_, _, _, _, _, slice authority_address, _, int revoked_at) = load_data();
slice new_editor_address = in_msg_body~load_msg_addr();
force_chain(new_editor_address);
slice response_destination = in_msg_body~load_msg_addr();
in_msg_body~load_int(1);
int forward_amount = in_msg_body~load_coins();
int rest_amount = my_balance - min_tons_for_storage();
if (forward_amount) {
rest_amount -= (forward_amount + fwd_fees);
}
int need_response = response_destination.preload_uint(2) != 0;
if (need_response) {
rest_amount -= fwd_fees;
}
throw_unless(402, rest_amount >= 0);
if (forward_amount) {
send_msg(flag::regular(), new_editor_address, forward_amount, op::editorship_assigned, query_id, begin_cell().store_slice(editor_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
}
if (need_response) {
force_chain(response_destination);
send_msg(flag::regular(), response_destination, rest_amount, op::excesses, query_id, null(), PAID_EXTERNALLY);
}
store_data(index, collection_address, owner_address, content, authority_address, new_editor_address, revoked_at);
}
() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) {
return ();
}
slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
slice sender_address = cs~load_msg_addr();
cs~load_msg_addr();
cs~load_coins();
cs~skip_bits(1);
cs~load_coins();
int fwd_fee = muldiv(cs~load_coins(), 3, 2);
(int init?, int index, slice collection_address, slice owner_address, cell content, slice authority_address, slice editor_address, int revoked_at) = load_data();
if (~ init?) {
throw_unless(405, equal_slices(collection_address, sender_address));
store_data(index, collection_address, in_msg_body~load_msg_addr(), in_msg_body~load_ref(), in_msg_body~load_msg_addr(), in_msg_body~load_msg_addr(), 0);
return ();
}
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
if (flags & 1) { ;; route all prove_ownership bounced messages to owner
;; first op was 0xffffffff, because of bounced, now we need to read real one
op = in_msg_body~load_uint(32);
if (op == op::ownership_proof) {
send_msg(flag::regular(), owner_address, ZERO_AMOUNT, op::ownership_proof_bounced, query_id, null(), CARRY_REMAINING_GAS);
}
return ();
}
if (op == op::request_owner) {
slice dest = in_msg_body~load_msg_addr();
cell body = in_msg_body~load_ref();
int with_content = in_msg_body~load_uint(1);
var msg = begin_cell()
.store_uint(index, 256)
.store_slice(sender_address)
.store_slice(owner_address)
.store_ref(body)
.store_uint(revoked_at, 64)
.store_uint(with_content, 1);
if (with_content != 0) {
msg = msg.store_ref(content);
}
send_msg(flag::regular() | flag::bounce(), dest, ZERO_AMOUNT, op::owner_info, query_id, msg, CARRY_REMAINING_GAS);
return ();
}
if (op == op::prove_ownership) {
throw_unless(401, equal_slices(owner_address, sender_address));
slice dest = in_msg_body~load_msg_addr();
cell body = in_msg_body~load_ref();
int with_content = in_msg_body~load_uint(1);
var msg = begin_cell()
.store_uint(index, 256)
.store_slice(owner_address)
.store_ref(body)
.store_uint(revoked_at, 64)
.store_uint(with_content, 1);
if (with_content != 0) {
msg = msg.store_ref(content);
}
send_msg(flag::regular() | flag::bounce(), dest, ZERO_AMOUNT, op::ownership_proof, query_id, msg, CARRY_REMAINING_GAS);
return ();
}
if (op == op::get_static_data) {
var msg = begin_cell().store_uint(index, 256).store_slice(collection_address);
send_msg(flag::regular(), sender_address, ZERO_AMOUNT, op::report_static_data, query_id, msg, CARRY_REMAINING_GAS);
return ();
}
if (op == op::destroy) {
throw_unless(401, equal_slices(owner_address, sender_address));
send_msg(flag::regular(), sender_address, ZERO_AMOUNT, op::excesses, query_id, null(), CARRY_ALL_BALANCE);
owner_address = null_addr();
authority_address = null_addr();
store_data(index, collection_address, owner_address, content, authority_address, editor_address, revoked_at);
return ();
}
if (op == op::revoke) {
throw_unless(401, equal_slices(authority_address, sender_address));
throw_unless(403, revoked_at == 0);
revoked_at = now();
store_data(index, collection_address, owner_address, content, authority_address, editor_address, revoked_at);
return ();
}
if (op == op::take_excess) {
throw_unless(401, equal_slices(owner_address, sender_address));
raw_reserve(min_tons_for_storage(), 0);
send_msg(flag::regular(), sender_address, ZERO_AMOUNT, op::excesses, query_id, null(), CARRY_ALL_BALANCE);
return ();
}
if (op == op::edit_content) {
throw_unless(410, equal_slices(sender_address, editor_address));
content = in_msg_body~load_ref();
store_data(index, collection_address, owner_address, content, authority_address, editor_address, revoked_at);
return ();
}
if (op == op::transfer_editorship) {
transfer_editorship(my_balance, index, collection_address, owner_address, content, editor_address, sender_address, query_id, in_msg_body, fwd_fee);
return ();
}
if (op == op::transfer) {
throw(413);
}
throw(0xffff);
}
;;
;; GET Methods
;;
(int, int, slice, slice, cell, slice, slice, int) get_nft_data() method_id {
(int init?, int index, slice collection_address, slice owner_address, cell content, slice authority_address, slice editor_address, int revoked_at) = load_data();
return (init?, index, collection_address, owner_address, content, authority_address, editor_address, revoked_at);
}
slice get_authority_address() method_id {
(_, _, _, _, _, slice authority_address, _, _) = load_data();
return authority_address;
}
int get_revoked_time() method_id {
(_, _, _, _, _, _, _, int revoked_at) = load_data();
return revoked_at;
}
slice get_editor_address() method_id {
(_, _, _, _, _, _, slice editor_address, _) = load_data();
return editor_address;
}