-
Notifications
You must be signed in to change notification settings - Fork 1
/
redeemable-nft-collection.fc
187 lines (159 loc) · 5.56 KB
/
redeemable-nft-collection.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
;;
;; The Open Pass @ Hack-a-TONx
;; TON redeemable NFT Smart Contract - Collection
;; ---
;; Similar to royalty, our redeemable NFT allows issuers to predefine their utility NFT redemption rules in the contract.
;; To redeem their NFT, holders must sign a message authorizing the redemption.
;;
;;
;; Storage
;;
;; MsgAddressInt owner_address
;; uint64 next_item_index
;; cell content
;; cell nft_item_code
;; uint16 redeemable_index
;;
#include "utils/stdlib.fc";
#include "utils/op-codes.fc";
#include "utils/params.fc";
global slice storage::owner_address;
global int storage::next_item_index;
global cell storage::content;
global cell storage::nft_item_code;
global int storage::redeemable_index;
() load_data() inline {
var ds = get_data().begin_parse();
storage::owner_address = ds~load_msg_addr();
storage::next_item_index = ds~load_uint(64);
storage::content = ds~load_ref();
storage::nft_item_code = ds~load_ref();
storage::redeemable_index = ds~load_uint(16);
}
() save_data() impure inline {
set_data(
begin_cell().store_slice(storage::owner_address)
.store_uint(storage::next_item_index, 64)
.store_ref(storage::content)
.store_ref(storage::nft_item_code)
.store_uint(storage::redeemable_index, 16)
.end_cell()
);
}
cell calculate_nft_item_state_init(int item_index) {
cell data = begin_cell().store_uint(item_index, 64)
.store_slice(my_address())
.end_cell();
return begin_cell().store_uint(0, 2)
.store_dict(storage::nft_item_code)
.store_dict(data)
.store_uint(0, 1)
.end_cell();
}
slice calculate_nft_item_address(int wc, cell state_init) {
return begin_cell().store_uint(4, 3)
.store_int(wc, 8)
.store_uint(cell_hash(state_init), 256)
.end_cell()
.begin_parse();
}
() deploy_nft_item(int item_index, int amount, cell nft_content) impure {
cell state_init = calculate_nft_item_state_init(item_index);
slice nft_address = calculate_nft_item_address(workchain(), state_init);
var msg = begin_cell().store_uint(0x18, 6)
.store_slice(nft_address)
.store_coins(amount)
.store_uint(4 + 2 + 1, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1)
.store_ref(state_init)
.store_ref(nft_content);
send_raw_message(msg.end_cell(), 1); ;; pay transfer fees separately, revert on errors
}
() recv_internal(cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) { ;; ignore empty messages
return ();
}
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();
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);
;; unload msg from c4
load_data();
throw_unless(401, equal_slice_bits(sender_address, storage::owner_address));
if (op == 1) { ;; deploy new nft
int item_index = in_msg_body~load_uint(64); ;; unloaded index from c4
throw_unless(402, item_index <= storage::next_item_index);
var is_last = item_index == storage::next_item_index;
deploy_nft_item(
item_index,
in_msg_body~load_coins(),
in_msg_body~load_ref()
);
if (is_last) {
storage::next_item_index += 1;
save_data();
}
return ();
}
if (op == 2) { ;; batch deploy of new nfts
int counter = 0;
cell deploy_list = in_msg_body~load_ref();
do {
var (item_index, item, f?) = deploy_list~udict::delete_get_min(64);
if (f?) {
counter += 1;
if (counter >= 250) { ;; Limit due to limits of action list size
throw(399);
}
;; check that there is no confusion with indices,
;; that is, the current index is not greater than the next one
throw_unless(403 + counter, item_index <= storage::next_item_index);
deploy_nft_item(
item_index,
item~load_coins(),
item~load_ref()
);
if (item_index == storage::next_item_index) {
storage::next_item_index += 1;
}
}
} until ( ~ f?);
save_data();
return ();
}
if (op == 3) { ;; change owner
slice new_owner = in_msg_body~load_msg_addr();
save_data();
return ();
}
throw(0xffff);
}
;; Get methods
(int, cell, slice) get_collection_data() method_id {
load_data();
slice cs = storage::content.begin_parse();
return (storage::next_item_index, cs~load_ref(), storage::owner_address);
}
slice get_nft_address_by_index(int index) method_id {
load_data();
cell state_init = calculate_nft_item_state_init(index);
return calculate_nft_item_address(workchain(), state_init);
}
int redeemable_index() method_id {
load_data();
return storage::redeemable_index;
}
cell get_nft_content(int index, cell individual_nft_content) method_id {
load_data();
slice cs = storage::content.begin_parse();
cs~load_ref();
slice common_content = cs~load_ref().begin_parse();
return (begin_cell()
.store_uint(1, 8) ;; offchain tag
.store_slice(common_content)
.store_ref(individual_nft_content)
.end_cell());
}