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
use alloc::string::{String, ToString};
use casper_contract::{
contract_api::{
runtime::{self, get_caller},
storage,
},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::{account::AccountHash, ApiError, CLValue, Key, URef};
use crate::{
constants::{
NAMED_KEY_APPROVED_CNT, NAMED_KEY_DICT_OWNERS_NAME, RUNTIME_ARG_HOLDER_ID,
RUNTIME_ARG_PRODUCER_ACCOUNT_HASH,
},
constants::{
NAMED_KEY_DICT_APPROVED_NAME, NAMED_KEY_DICT_HOLDERS_NAME,
NAMED_KEY_DICT_PRODAPPROVED_NAME, NAMED_KEY_DICT_PROD_REQS,
NAMED_KEY_DICT_PUBAPPROVED_NAME, NAMED_KEY_DICT_PUB_REQS, NAMED_KEY_DICT_REQ_OBJ,
NAMED_KEY_REQ_CNT, RUNTIME_ARG_AMOUNT, RUNTIME_ARG_APPROVED_ID, RUNTIME_ARG_REQUEST_ID,
RUNTIME_ARG_SPENDER,
},
event::{emit, DropLinkedEvent},
ndpc_types::{self, ApprovedNFT, AsStrized, NFTHolder, PublishRequest, U64list},
ndpc_utils::{self, get_holder_by_id, get_holder_ids, get_request_by_id},
Error,
};
fn get_approve_dicts() -> (URef, URef, URef, URef, URef, URef, URef, URef, URef) {
(
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_REQ_OBJ),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PROD_REQS),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PUB_REQS),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_OWNERS_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_HOLDERS_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PUBAPPROVED_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PRODAPPROVED_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_APPROVED_CNT),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_APPROVED_NAME),
)
}
#[no_mangle]
pub extern "C" fn approve() {
let (
requests_dict,
prod_reqs_dict,
pub_reqs_dict,
owners_dict,
holders_dict,
publishers_approved_dict,
producers_approved_dict,
approved_cnt_uref,
approved_dict,
) = get_approve_dicts();
let request_id: u64 = runtime::get_named_arg(RUNTIME_ARG_REQUEST_ID);
let request_obj = get_request_by_id(requests_dict, request_id);
let amount: u64 = request_obj.amount;
let holder_id: u64 = request_obj.holder_id;
let spender_acc: AccountHash = request_obj.publisher;
let spender: String = request_obj.publisher.as_string();
let caller_account = runtime::get_caller();
let caller: String = caller_account.as_string();
let caller_holder_ids =
get_holder_ids(owners_dict, &caller).unwrap_or_revert_with(Error::HolderDoesentExist);
if !caller_holder_ids.contains(holder_id) {
runtime::revert(ApiError::from(Error::NotOwnerOfHolderId));
}
let holder: NFTHolder = storage::dictionary_get(holders_dict, holder_id.to_string().as_str())
.unwrap_or_revert()
.unwrap_or_revert();
let approved_holder = ApprovedNFT::new(
holder_id,
amount,
caller_account,
spender_acc,
holder.token_id,
);
storage::dictionary_put(holders_dict, holder_id.to_string().as_str(), holder);
let approved_cnt: u64 = storage::read(approved_cnt_uref)
.unwrap_or_revert()
.unwrap_or_revert();
let new_approved_cnt = approved_cnt + 1;
storage::write(approved_cnt_uref, new_approved_cnt);
let approved_id = new_approved_cnt;
storage::dictionary_put(
approved_dict,
approved_id.to_string().as_str(),
approved_holder,
);
let publisher_approved_holders_opt =
storage::dictionary_get(publishers_approved_dict, &spender).unwrap_or_revert();
if publisher_approved_holders_opt.is_none() {
let mut new_list = ndpc_types::U64list::new();
new_list.list.insert(approved_id);
storage::dictionary_put(publishers_approved_dict, &spender, new_list);
} else {
let mut publisher_approved_holders: ndpc_types::U64list =
publisher_approved_holders_opt.unwrap_or_revert();
publisher_approved_holders.list.insert(approved_id);
storage::dictionary_put(
publishers_approved_dict,
&spender,
publisher_approved_holders,
);
}
let producer_approved_holders_opt =
storage::dictionary_get(producers_approved_dict, &caller).unwrap_or_revert();
if producer_approved_holders_opt.is_none() {
let mut new_list = ndpc_types::U64list::new();
new_list.list.insert(approved_id);
storage::dictionary_put(producers_approved_dict, &caller, new_list);
} else {
let mut producer_approved_holders: ndpc_types::U64list =
producer_approved_holders_opt.unwrap_or_revert();
producer_approved_holders.list.insert(approved_id);
storage::dictionary_put(producers_approved_dict, &caller, producer_approved_holders);
}
let publisher_requests_opt =
storage::dictionary_get::<U64list>(pub_reqs_dict, &spender).unwrap_or_revert();
let mut publisher_requests: U64list = publisher_requests_opt.unwrap_or_revert();
publisher_requests.remove(request_id);
storage::dictionary_put(pub_reqs_dict, &spender, publisher_requests);
let producer_requests_opt =
storage::dictionary_get::<U64list>(prod_reqs_dict, &caller).unwrap_or_revert();
let mut producer_requests: U64list = producer_requests_opt.unwrap_or_revert();
producer_requests.remove(request_id);
storage::dictionary_put(prod_reqs_dict, &caller, producer_requests);
let ret = CLValue::from_t(approved_id).unwrap_or_revert();
emit(DropLinkedEvent::ApprovedPublish {
request_id,
approved_id,
});
runtime::ret(ret);
}
#[no_mangle]
pub extern "C" fn disapprove() {
let amount: u64 = runtime::get_named_arg(RUNTIME_ARG_AMOUNT);
let approved_id: u64 = runtime::get_named_arg(RUNTIME_ARG_APPROVED_ID);
let spender_key: Key = runtime::get_named_arg(RUNTIME_ARG_SPENDER);
let spender_acc = spender_key
.into_account()
.unwrap_or_revert_with(ApiError::from(Error::NotAccountHash));
let spender: String = spender_acc.as_string();
let approved_dict = ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_APPROVED_NAME);
let publishers_approved_dict =
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PUBAPPROVED_NAME);
let producers_approved_dict =
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PRODAPPROVED_NAME);
let holders_dict = ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_HOLDERS_NAME);
let mut approved_holder =
storage::dictionary_get::<ApprovedNFT>(approved_dict, approved_id.to_string().as_str())
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::ApprovedHolderDoesentExist));
let caller = runtime::get_caller();
if caller != approved_holder.owneraccount {
runtime::revert(ApiError::from(Error::NotOwnerOfHolderId));
}
let caller_string = caller.as_string();
if approved_holder.amount < amount {
runtime::revert(ApiError::from(Error::NotEnoughAmount));
}
approved_holder.amount -= amount;
if approved_holder.amount == 0 {
{
let mut publisher_approved_holders =
storage::dictionary_get::<ndpc_types::U64list>(publishers_approved_dict, &spender)
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::PublisherHasNoApprovedHolders));
publisher_approved_holders.remove(approved_id);
storage::dictionary_put(
publishers_approved_dict,
&spender,
publisher_approved_holders,
);
}
{
let mut producer_approved_holders = storage::dictionary_get::<ndpc_types::U64list>(
producers_approved_dict,
caller_string.as_str(),
)
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::ProducerHasNoApprovedHolders));
producer_approved_holders.remove(approved_id);
storage::dictionary_put(
producers_approved_dict,
caller_string.as_str(),
producer_approved_holders,
);
}
}
let holder_id = approved_holder.holder_id;
storage::dictionary_put(
approved_dict,
approved_id.to_string().as_str(),
approved_holder,
);
let holder = storage::dictionary_get::<NFTHolder>(holders_dict, holder_id.to_string().as_str())
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::HolderDoesentExist));
storage::dictionary_put(holders_dict, holder_id.to_string().as_str(), holder);
emit(DropLinkedEvent::DisapprovedPublish { approved_id });
}
fn get_publish_request_storage() -> (URef, URef, URef, URef, URef, URef) {
(
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_HOLDERS_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_OWNERS_NAME),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_REQ_OBJ),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PROD_REQS),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PUB_REQS),
ndpc_utils::get_named_key_by_name(NAMED_KEY_REQ_CNT),
)
}
fn get_publish_request_runtime_args() -> (AccountHash, u64, u64) {
(
runtime::get_named_arg::<Key>(RUNTIME_ARG_PRODUCER_ACCOUNT_HASH)
.into_account()
.unwrap_or_revert(),
runtime::get_named_arg::<u64>(RUNTIME_ARG_HOLDER_ID),
runtime::get_named_arg::<u64>(RUNTIME_ARG_AMOUNT),
)
}
#[no_mangle]
pub extern "C" fn publish_request() {
let (holders_dict, owners_dict, requests_dict, prod_reqs_dict, pub_reqs_dict, tokens_cnt_uref) =
get_publish_request_storage();
let (producer_account_hash, holder_id, amount) = get_publish_request_runtime_args();
let caller = get_caller();
let producer_string = producer_account_hash.as_string();
let holder = get_holder_by_id(holders_dict, holder_id);
if holder.amount < amount {
runtime::revert(ApiError::from(Error::NotEnoughAmount));
}
let prod_list = storage::dictionary_get::<U64list>(owners_dict, producer_string.as_str())
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::EmptyOwnerShipList));
if !prod_list.contains(holder_id) {
runtime::revert(ApiError::from(Error::NotOwnerOfHolderId));
}
let publish_request =
ndpc_types::PublishRequest::new(holder_id, amount, producer_account_hash, caller);
let request_cnt = storage::read::<u64>(tokens_cnt_uref)
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::EmptyRequestCnt));
let request_id = request_cnt + 1;
storage::write(tokens_cnt_uref, request_id);
storage::dictionary_put(
requests_dict,
request_id.to_string().as_str(),
publish_request,
);
let prod_reqs_opt =
storage::dictionary_get::<U64list>(prod_reqs_dict, producer_string.as_str())
.unwrap_or_revert();
let mut prod_reqs = match prod_reqs_opt {
Some(reqs) => reqs,
None => U64list::new(),
};
prod_reqs.list.insert(request_id);
storage::dictionary_put(prod_reqs_dict, producer_string.as_str(), prod_reqs);
let pub_reqs_opt =
storage::dictionary_get::<U64list>(pub_reqs_dict, caller.to_string().as_str())
.unwrap_or_revert();
let mut pub_reqs = match pub_reqs_opt {
Some(reqs) => reqs,
None => U64list::new(),
};
pub_reqs.list.insert(request_id);
storage::dictionary_put(pub_reqs_dict, caller.to_string().as_str(), pub_reqs);
let ret = CLValue::from_t(request_id).unwrap_or_revert();
emit(DropLinkedEvent::PublishRequest {
owner: producer_account_hash,
publisher: caller,
amount,
holder_id,
request_id,
});
runtime::ret(ret);
}
fn get_cancel_request_storage() -> (URef, URef, URef) {
(
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_REQ_OBJ),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PROD_REQS),
ndpc_utils::get_named_key_by_name(NAMED_KEY_DICT_PUB_REQS),
)
}
#[no_mangle]
pub extern "C" fn cancel_request() {
let (requests_dict, prod_reqs_dict, pub_reqs_dict) = get_cancel_request_storage();
let request_id: u64 = runtime::get_named_arg(RUNTIME_ARG_REQUEST_ID);
let caller: String = get_caller().as_string();
let request_obj: PublishRequest = get_request_by_id(requests_dict, request_id);
if request_obj.publisher != get_caller() {
runtime::revert(ApiError::from(Error::AccessDenied));
}
let mut pub_reqs = storage::dictionary_get::<U64list>(
pub_reqs_dict,
request_obj.publisher.as_string().as_str(),
)
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::EmptyU64List));
let mut prod_reqs = storage::dictionary_get::<U64list>(
prod_reqs_dict,
request_obj.producer.as_string().as_str(),
)
.unwrap_or_revert()
.unwrap_or_revert_with(ApiError::from(Error::EmptyU64List));
pub_reqs.remove(request_id);
prod_reqs.remove(request_id);
storage::dictionary_put(pub_reqs_dict, caller.as_str(), pub_reqs);
storage::dictionary_put(
prod_reqs_dict,
request_obj.producer.as_string().as_str(),
prod_reqs,
);
emit(DropLinkedEvent::CancelRequest { request_id });
}