-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.rs
2912 lines (2583 loc) · 107 KB
/
main.rs
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![no_std]
#![no_main]
#[cfg(not(target_arch = "wasm32"))]
compile_error!("target arch should be wasm32: compile with '--target wasm32-unknown-unknown'");
mod constants;
mod error;
mod events;
mod metadata;
mod modalities;
mod utils;
extern crate alloc;
use alloc::{
boxed::Box,
collections::BTreeMap,
format,
string::{String, ToString},
vec,
vec::Vec,
};
use casper_contract::{
contract_api::{
runtime::{self, call_contract, revert},
storage::{self},
},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::{
contracts::NamedKeys, runtime_args, CLType, CLValue, ContractHash, ContractPackageHash,
EntryPoint, EntryPointAccess, EntryPointType, EntryPoints, Key, KeyTag, Parameter, RuntimeArgs,
Tagged,
};
use constants::{
ACCESS_KEY_NAME_1_0_0, ACL_PACKAGE_MODE, ACL_WHITELIST, ALLOW_MINTING, APPROVED,
ARG_ACCESS_KEY_NAME_1_0_0, ARG_ACL_PACKAGE_MODE, ARG_ACL_WHITELIST,
ARG_ADDITIONAL_REQUIRED_METADATA, ARG_ALLOW_MINTING, ARG_APPROVE_ALL, ARG_BURN_MODE,
ARG_COLLECTION_NAME, ARG_COLLECTION_SYMBOL, ARG_CONTRACT_WHITELIST, ARG_EVENTS_MODE,
ARG_HASH_KEY_NAME_1_0_0, ARG_HOLDER_MODE, ARG_IDENTIFIER_MODE, ARG_JSON_SCHEMA,
ARG_METADATA_MUTABILITY, ARG_MINTING_MODE, ARG_NAMED_KEY_CONVENTION, ARG_NFT_KIND,
ARG_NFT_METADATA_KIND, ARG_NFT_PACKAGE_KEY, ARG_OPERATOR, ARG_OPERATOR_BURN_MODE,
ARG_OPTIONAL_METADATA, ARG_OWNERSHIP_MODE, ARG_OWNER_LOOKUP_MODE, ARG_PACKAGE_OPERATOR_MODE,
ARG_RECEIPT_NAME, ARG_SOURCE_KEY, ARG_SPENDER, ARG_TARGET_KEY, ARG_TOKEN_HASH, ARG_TOKEN_ID,
ARG_TOKEN_META_DATA, ARG_TOKEN_OWNER, ARG_TOTAL_TOKEN_SUPPLY, ARG_TRANSFER_FILTER_CONTRACT,
ARG_WHITELIST_MODE, BURNT_TOKENS, BURN_MODE, COLLECTION_NAME, COLLECTION_SYMBOL,
ENTRY_POINT_APPROVE, ENTRY_POINT_BALANCE_OF, ENTRY_POINT_BURN, ENTRY_POINT_GET_APPROVED,
ENTRY_POINT_INIT, ENTRY_POINT_IS_APPROVED_FOR_ALL, ENTRY_POINT_METADATA, ENTRY_POINT_MIGRATE,
ENTRY_POINT_MINT, ENTRY_POINT_OWNER_OF, ENTRY_POINT_REGISTER_OWNER, ENTRY_POINT_REVOKE,
ENTRY_POINT_SET_APPROVALL_FOR_ALL, ENTRY_POINT_SET_TOKEN_METADATA, ENTRY_POINT_SET_VARIABLES,
ENTRY_POINT_TRANSFER, ENTRY_POINT_UPDATED_RECEIPTS, EVENTS_MODE, HASH_BY_INDEX,
HASH_KEY_NAME_1_0_0, HOLDER_MODE, IDENTIFIER_MODE, INDEX_BY_HASH, INSTALLER, JSON_SCHEMA,
MAX_TOTAL_TOKEN_SUPPLY, METADATA_CEP78, METADATA_CUSTOM_VALIDATED, METADATA_MUTABILITY,
METADATA_NFT721, METADATA_RAW, MINTING_MODE, NFT_KIND, NFT_METADATA_KIND, NFT_METADATA_KINDS,
NUMBER_OF_MINTED_TOKENS, OPERATOR, OPERATORS, OPERATOR_BURN_MODE, OWNED_TOKENS, OWNERSHIP_MODE,
PACKAGE_OPERATOR_MODE, PAGE_LIMIT, PAGE_TABLE, PREFIX_ACCESS_KEY_NAME, PREFIX_CEP78,
PREFIX_CONTRACT_NAME, PREFIX_CONTRACT_VERSION, PREFIX_HASH_KEY_NAME, PREFIX_PAGE_DICTIONARY,
RECEIPT_NAME, REPORTING_MODE, RLO_MFLAG, TOKEN_COUNT, TOKEN_ISSUERS, TOKEN_OWNERS,
TOTAL_TOKEN_SUPPLY, TRANSFER_FILTER_CONTRACT, TRANSFER_FILTER_CONTRACT_METHOD,
UNMATCHED_HASH_COUNT, WHITELIST_MODE,
};
use core::convert::{TryFrom, TryInto};
use error::NFTCoreError;
use events::{
events_cep47::{record_cep47_event_dictionary, CEP47Event},
events_ces::{
Approval, ApprovalForAll, ApprovalRevoked, Burn, MetadataUpdated, Migration, Mint,
RevokedForAll, Transfer, VariablesSet,
},
};
use metadata::CustomMetadataSchema;
use modalities::{
BurnMode, EventsMode, MetadataMutability, MintingMode, NFTHolderMode, NFTIdentifierMode,
NFTKind, NFTMetadataKind, NamedKeyConventionMode, OwnerReverseLookupMode, OwnershipMode,
Requirement, TokenIdentifier, TransferFilterContractResult, WhitelistMode,
};
use utils::Caller;
#[no_mangle]
pub extern "C" fn init() {
// We only allow the init() entrypoint to be called once.
// If COLLECTION_NAME uref already exists we revert since this implies that
// the init() entrypoint has already been called.
if utils::named_uref_exists(COLLECTION_NAME) {
runtime::revert(NFTCoreError::ContractAlreadyInitialized);
}
// Only the installing account may call this method. All other callers are erroneous.
let installing_account = utils::get_account_hash(
INSTALLER,
NFTCoreError::MissingInstaller,
NFTCoreError::InvalidInstaller,
);
// We revert if caller is not the managing installing account
if installing_account != runtime::get_caller() {
runtime::revert(NFTCoreError::InvalidAccount)
}
// Start collecting the runtime arguments.
let collection_name: String = utils::get_named_arg_with_user_errors(
ARG_COLLECTION_NAME,
NFTCoreError::MissingCollectionName,
NFTCoreError::InvalidCollectionName,
)
.unwrap_or_revert();
let collection_symbol: String = utils::get_named_arg_with_user_errors(
ARG_COLLECTION_SYMBOL,
NFTCoreError::MissingCollectionSymbol,
NFTCoreError::InvalidCollectionSymbol,
)
.unwrap_or_revert();
let total_token_supply: u64 = utils::get_named_arg_with_user_errors(
ARG_TOTAL_TOKEN_SUPPLY,
NFTCoreError::MissingTotalTokenSupply,
NFTCoreError::InvalidTotalTokenSupply,
)
.unwrap_or_revert();
if total_token_supply > MAX_TOTAL_TOKEN_SUPPLY {
runtime::revert(NFTCoreError::ExceededMaxTotalSupply)
}
let allow_minting: bool = utils::get_named_arg_with_user_errors(
ARG_ALLOW_MINTING,
NFTCoreError::MissingMintingStatus,
NFTCoreError::InvalidMintingStatus,
)
.unwrap_or_revert();
let minting_mode: MintingMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_MINTING_MODE,
NFTCoreError::MissingMintingMode,
NFTCoreError::InvalidMintingMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let ownership_mode: OwnershipMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_OWNERSHIP_MODE,
NFTCoreError::MissingOwnershipMode,
NFTCoreError::InvalidOwnershipMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let nft_kind: NFTKind = utils::get_named_arg_with_user_errors::<u8>(
ARG_NFT_KIND,
NFTCoreError::MissingNftKind,
NFTCoreError::InvalidNftKind,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let holder_mode: NFTHolderMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_HOLDER_MODE,
NFTCoreError::MissingHolderMode,
NFTCoreError::InvalidHolderMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let whitelist_mode: WhitelistMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_WHITELIST_MODE,
NFTCoreError::MissingWhitelistMode,
NFTCoreError::InvalidWhitelistMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let acl_whitelist = utils::get_named_arg_with_user_errors::<Vec<Key>>(
ARG_ACL_WHITELIST,
NFTCoreError::MissingACLWhiteList,
NFTCoreError::InvalidACLWhitelist,
)
.unwrap_or_revert();
// Revert if minting mode is not ACL and acl list is not empty
if MintingMode::Acl != minting_mode && !acl_whitelist.is_empty() {
runtime::revert(NFTCoreError::InvalidMintingMode)
}
// Revert if minting mode is ACL or holder_mode is contracts and acl list is locked and empty
if MintingMode::Acl == minting_mode
&& acl_whitelist.is_empty()
&& WhitelistMode::Locked == whitelist_mode
{
runtime::revert(NFTCoreError::EmptyACLWhitelist)
}
let acl_package_mode: bool = utils::get_named_arg_with_user_errors(
ARG_ACL_PACKAGE_MODE,
NFTCoreError::MissingACLPackageMode,
NFTCoreError::InvalidACLPackageMode,
)
.unwrap_or_revert();
let package_operator_mode: bool = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_PACKAGE_OPERATOR_MODE,
NFTCoreError::InvalidPackageOperatorMode,
)
.unwrap_or_default();
let receipt_name: String = utils::get_named_arg_with_user_errors(
ARG_RECEIPT_NAME,
NFTCoreError::MissingReceiptName,
NFTCoreError::InvalidReceiptName,
)
.unwrap_or_revert();
let package_hash = utils::get_named_arg_with_user_errors::<String>(
ARG_NFT_PACKAGE_KEY,
NFTCoreError::MissingCep78PackageHash,
NFTCoreError::InvalidCep78InvalidHash,
)
.unwrap_or_revert();
let base_metadata_kind: NFTMetadataKind = utils::get_named_arg_with_user_errors::<u8>(
ARG_NFT_METADATA_KIND,
NFTCoreError::MissingNFTMetadataKind,
NFTCoreError::InvalidNFTMetadataKind,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let optional_metadata = utils::get_named_arg_with_user_errors::<Vec<u8>>(
ARG_OPTIONAL_METADATA,
NFTCoreError::MissingOptionalNFTMetadataKind,
NFTCoreError::InvalidOptionalNFTMetadataKind,
)
.unwrap_or_revert();
let additional_required_metadata = utils::get_named_arg_with_user_errors::<Vec<u8>>(
ARG_ADDITIONAL_REQUIRED_METADATA,
NFTCoreError::MissingAdditionalNFTMetadataKind,
NFTCoreError::InvalidAdditionalNFTMetadataKind,
)
.unwrap_or_revert();
let nft_metadata_kinds = utils::create_metadata_requirements(
base_metadata_kind,
additional_required_metadata,
optional_metadata,
);
let json_schema: String = utils::get_named_arg_with_user_errors(
ARG_JSON_SCHEMA,
NFTCoreError::MissingJsonSchema,
NFTCoreError::InvalidJsonSchema,
)
.unwrap_or_revert();
// Check if schema is missing before checking its validity
if base_metadata_kind == NFTMetadataKind::CustomValidated && json_schema.is_empty() {
runtime::revert(NFTCoreError::MissingJsonSchema)
}
// Attempt to parse the provided schema if the CustomValidated metadata kind is required or
// optional and fail installation if the schema cannot be parsed.
if let Some(required_or_optional) = nft_metadata_kinds.get(&NFTMetadataKind::CustomValidated) {
if required_or_optional == &Requirement::Required
|| required_or_optional == &Requirement::Optional
{
serde_json_wasm::from_str::<CustomMetadataSchema>(&json_schema)
.map_err(|_| NFTCoreError::InvalidJsonSchema)
.unwrap_or_revert();
}
}
let identifier_mode: NFTIdentifierMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_IDENTIFIER_MODE,
NFTCoreError::MissingIdentifierMode,
NFTCoreError::InvalidIdentifierMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let metadata_mutability: MetadataMutability = utils::get_named_arg_with_user_errors::<u8>(
ARG_METADATA_MUTABILITY,
NFTCoreError::MissingMetadataMutability,
NFTCoreError::InvalidMetadataMutability,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let burn_mode: BurnMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_BURN_MODE,
NFTCoreError::MissingBurnMode,
NFTCoreError::InvalidBurnMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
let operator_burn_mode: bool = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_OPERATOR_BURN_MODE,
NFTCoreError::InvalidOperatorBurnMode,
)
.unwrap_or_default();
let reporting_mode: OwnerReverseLookupMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_OWNER_LOOKUP_MODE,
NFTCoreError::MissingReportingMode,
NFTCoreError::InvalidReportingMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
// OwnerReverseLookup TransfersOnly mode should be Transferable
if OwnerReverseLookupMode::TransfersOnly == reporting_mode
&& OwnershipMode::Transferable != ownership_mode
{
runtime::revert(NFTCoreError::OwnerReverseLookupModeNotTransferable)
}
let transfer_filter_contract_contract_key =
utils::get_optional_named_arg_with_user_errors::<Option<Key>>(
ARG_TRANSFER_FILTER_CONTRACT,
NFTCoreError::InvalidTransferFilterContract,
)
.unwrap_or_default();
let transfer_filter_contract_contract_hash: Option<ContractHash> =
transfer_filter_contract_contract_key.map(|transfer_filter_contract_contract_key| {
ContractHash::from(
transfer_filter_contract_contract_key
.into_hash()
.unwrap_or_revert(),
)
});
if ownership_mode != OwnershipMode::Transferable
&& transfer_filter_contract_contract_hash.is_some()
{
runtime::revert(NFTCoreError::TransferFilterContractNeedsTransferableMode)
}
// Put all created URefs into the contract's context (necessary to retain access rights,
// for future use).
//
// Initialize contract with URefs for all invariant values, which can never be changed.
runtime::put_key(
COLLECTION_NAME,
storage::new_uref(collection_name.clone()).into(),
);
runtime::put_key(
COLLECTION_SYMBOL,
storage::new_uref(collection_symbol).into(),
);
runtime::put_key(
TOTAL_TOKEN_SUPPLY,
storage::new_uref(total_token_supply).into(),
);
runtime::put_key(
OWNERSHIP_MODE,
storage::new_uref(ownership_mode as u8).into(),
);
runtime::put_key(NFT_KIND, storage::new_uref(nft_kind as u8).into());
runtime::put_key(JSON_SCHEMA, storage::new_uref(json_schema).into());
runtime::put_key(MINTING_MODE, storage::new_uref(minting_mode as u8).into());
runtime::put_key(HOLDER_MODE, storage::new_uref(holder_mode as u8).into());
runtime::put_key(
WHITELIST_MODE,
storage::new_uref(whitelist_mode as u8).into(),
);
runtime::put_key(RECEIPT_NAME, storage::new_uref(receipt_name).into());
runtime::put_key(
&format!("{PREFIX_CEP78}_{collection_name}"),
storage::new_uref(package_hash).into(),
);
runtime::put_key(
NFT_METADATA_KIND,
storage::new_uref(base_metadata_kind).into(),
);
runtime::put_key(
NFT_METADATA_KINDS,
storage::new_uref(nft_metadata_kinds).into(),
);
runtime::put_key(
IDENTIFIER_MODE,
storage::new_uref(identifier_mode as u8).into(),
);
runtime::put_key(
METADATA_MUTABILITY,
storage::new_uref(metadata_mutability as u8).into(),
);
runtime::put_key(BURN_MODE, storage::new_uref(burn_mode as u8).into());
runtime::put_key(
OPERATOR_BURN_MODE,
storage::new_uref(operator_burn_mode).into(),
);
let events_mode: EventsMode = utils::get_named_arg_with_user_errors::<u8>(
ARG_EVENTS_MODE,
NFTCoreError::MissingEventsMode,
NFTCoreError::InvalidEventsMode,
)
.unwrap_or_revert()
.try_into()
.unwrap_or_revert();
// Initialize events structures for CES.
if let EventsMode::CES = events_mode {
utils::init_events();
}
runtime::put_key(EVENTS_MODE, storage::new_uref(events_mode as u8).into());
// Initialize contract with variables which must be present but maybe set to
// different values after initialization.
runtime::put_key(ALLOW_MINTING, storage::new_uref(allow_minting).into());
// This is an internal variable that the installing account cannot change
// but is incremented by the contract itself.
runtime::put_key(NUMBER_OF_MINTED_TOKENS, storage::new_uref(0u64).into());
// Create the data dictionaries to store essential values, topically.
storage::new_dictionary(TOKEN_OWNERS)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(TOKEN_ISSUERS)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(APPROVED).unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(OPERATORS)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(BURNT_TOKENS)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(TOKEN_COUNT)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(METADATA_CUSTOM_VALIDATED)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(METADATA_CEP78)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(METADATA_NFT721)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(METADATA_RAW)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(HASH_BY_INDEX)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(INDEX_BY_HASH)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(PAGE_TABLE)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
storage::new_dictionary(ACL_WHITELIST)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
for key in acl_whitelist.iter() {
utils::upsert_dictionary_value_from_key(
ACL_WHITELIST,
&utils::encode_dictionary_item_key(*key),
true,
);
}
runtime::put_key(ACL_PACKAGE_MODE, storage::new_uref(acl_package_mode).into());
runtime::put_key(
PACKAGE_OPERATOR_MODE,
storage::new_uref(package_operator_mode).into(),
);
if [
OwnerReverseLookupMode::Complete,
OwnerReverseLookupMode::TransfersOnly,
]
.contains(&reporting_mode)
{
let page_table_width = utils::max_number_of_pages(total_token_supply);
runtime::put_key(PAGE_LIMIT, storage::new_uref(page_table_width).into());
}
runtime::put_key(
REPORTING_MODE,
storage::new_uref(reporting_mode as u8).into(),
);
runtime::put_key(RLO_MFLAG, storage::new_uref(false).into());
if let Some(transfer_filter_contract) = transfer_filter_contract_contract_hash {
runtime::put_key(
TRANSFER_FILTER_CONTRACT,
storage::new_uref(transfer_filter_contract).into(),
);
}
}
// set_variables allows the user to set any variable or any combination of variables simultaneously.
// set variables defines what variables are mutable and immutable.
#[no_mangle]
pub extern "C" fn set_variables() {
let installer = utils::get_account_hash(
INSTALLER,
NFTCoreError::MissingInstaller,
NFTCoreError::InvalidInstaller,
);
// Only the installing account can change the mutable variables.
if installer != runtime::get_caller() {
runtime::revert(NFTCoreError::InvalidAccount);
}
if let Some(allow_minting) = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_ALLOW_MINTING,
NFTCoreError::InvalidAllowMinting,
) {
let allow_minting_uref = utils::get_uref(
ALLOW_MINTING,
NFTCoreError::MissingAllowMinting,
NFTCoreError::InvalidAllowMinting,
);
storage::write(allow_minting_uref, allow_minting);
}
if let Some(acl_package_mode) = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_ACL_PACKAGE_MODE,
NFTCoreError::MissingACLPackageMode,
) {
let acl_package_mode_uref = utils::get_uref(
ACL_PACKAGE_MODE,
NFTCoreError::MissingACLPackageMode,
NFTCoreError::InvalidACLPackageMode,
);
storage::write(acl_package_mode_uref, acl_package_mode);
}
if let Some(package_operator_mode) = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_PACKAGE_OPERATOR_MODE,
NFTCoreError::MissingACLPackageMode,
) {
let package_operator_mode_uref = utils::get_uref(
PACKAGE_OPERATOR_MODE,
NFTCoreError::MissingPackageOperatorMode,
NFTCoreError::InvalidPackageOperatorMode,
);
storage::write(package_operator_mode_uref, package_operator_mode);
}
if let Some(operator_burn_mode) = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_OPERATOR_BURN_MODE,
NFTCoreError::MissingOperatorBurnMode,
) {
let operator_burn_mode_uref = utils::get_uref(
OPERATOR_BURN_MODE,
NFTCoreError::MissingOperatorBurnMode,
NFTCoreError::InvalidOperatorBurnMode,
);
storage::write(operator_burn_mode_uref, operator_burn_mode);
}
let mut new_acl_whitelist = utils::get_optional_named_arg_with_user_errors::<Vec<Key>>(
ARG_ACL_WHITELIST,
NFTCoreError::InvalidACLWhitelist,
)
.unwrap_or_default();
// Deprecated in 1.4 in favor of above ARG_ACL_WHITELIST
let new_contract_whitelist =
utils::get_optional_named_arg_with_user_errors::<Vec<ContractHash>>(
ARG_CONTRACT_WHITELIST,
NFTCoreError::InvalidContractWhitelist,
)
.unwrap_or_default();
for contract_hash in new_contract_whitelist.iter() {
new_acl_whitelist.push(Key::from(*contract_hash));
}
if !new_acl_whitelist.is_empty() {
let whitelist_mode: WhitelistMode = utils::get_stored_value_with_user_errors::<u8>(
WHITELIST_MODE,
NFTCoreError::MissingWhitelistMode,
NFTCoreError::InvalidWhitelistMode,
)
.try_into()
.unwrap_or_revert();
match whitelist_mode {
WhitelistMode::Unlocked => {
// Clear acl whitelist
runtime::remove_key(ACL_WHITELIST);
storage::new_dictionary(ACL_WHITELIST)
.unwrap_or_revert_with(NFTCoreError::FailedToCreateDictionary);
// Update acl whitelist
for key in new_acl_whitelist.iter() {
utils::upsert_dictionary_value_from_key(
ACL_WHITELIST,
&utils::encode_dictionary_item_key(*key),
true,
);
}
}
WhitelistMode::Locked => runtime::revert(NFTCoreError::InvalidWhitelistMode),
}
}
let events_mode: EventsMode = utils::get_stored_value_with_user_errors::<u8>(
EVENTS_MODE,
NFTCoreError::MissingEventsMode,
NFTCoreError::InvalidEventsMode,
)
.try_into()
.unwrap_or_revert();
// Emit VariablesSet event.
match events_mode {
EventsMode::NoEvents => {}
EventsMode::CEP47 => record_cep47_event_dictionary(CEP47Event::VariablesSet),
EventsMode::CES => casper_event_standard::emit(VariablesSet::new()),
}
}
// Mints a new token. Minting will fail if allow_minting is set to false.
#[no_mangle]
pub extern "C" fn mint() {
// The contract owner can toggle the minting behavior on and off over time.
// The contract is toggled on by default.
let minting_status = utils::get_stored_value_with_user_errors::<bool>(
ALLOW_MINTING,
NFTCoreError::MissingAllowMinting,
NFTCoreError::InvalidAllowMinting,
);
// If contract minting behavior is currently toggled off we revert.
if !minting_status {
runtime::revert(NFTCoreError::MintingIsPaused);
}
let total_token_supply = utils::get_stored_value_with_user_errors::<u64>(
TOTAL_TOKEN_SUPPLY,
NFTCoreError::MissingTotalTokenSupply,
NFTCoreError::InvalidTotalTokenSupply,
);
// The minted_tokens_count is the number of minted tokens so far.
let minted_tokens_count = utils::get_stored_value_with_user_errors::<u64>(
NUMBER_OF_MINTED_TOKENS,
NFTCoreError::MissingNumberOfMintedTokens,
NFTCoreError::InvalidNumberOfMintedTokens,
);
// Revert if the token supply has been exhausted.
if minted_tokens_count >= total_token_supply {
runtime::revert(NFTCoreError::TokenSupplyDepleted);
}
let minting_mode: MintingMode = utils::get_stored_value_with_user_errors::<u8>(
MINTING_MODE,
NFTCoreError::MissingMintingMode,
NFTCoreError::InvalidMintingMode,
)
.try_into()
.unwrap_or_revert();
let (caller, contract_package): (Key, Option<Key>) =
match utils::get_verified_caller().unwrap_or_revert() {
Caller::Session(account_hash) => (account_hash.into(), None),
Caller::StoredCaller(contract_hash, contract_package_hash) => {
(contract_hash.into(), Some(contract_package_hash.into()))
}
};
// Revert if minting is private and caller is not installer.
if MintingMode::Installer == minting_mode {
match caller.tag() {
KeyTag::Account => {
let installer_account = runtime::get_key(INSTALLER)
.unwrap_or_revert_with(NFTCoreError::MissingInstallerKey)
.into_account()
.unwrap_or_revert_with(NFTCoreError::FailedToConvertToAccountHash);
// Revert if private minting is required and caller is not installer.
if runtime::get_caller() != installer_account {
runtime::revert(NFTCoreError::InvalidMinter)
}
}
_ => runtime::revert(NFTCoreError::InvalidKey),
}
}
// Revert if minting is acl and caller is not whitelisted.
if MintingMode::Acl == minting_mode {
let acl_package_mode: bool = utils::get_stored_value_with_user_errors::<bool>(
ACL_PACKAGE_MODE,
NFTCoreError::MissingACLPackageMode,
NFTCoreError::InvalidACLPackageMode,
);
let is_whitelisted = match (acl_package_mode, contract_package) {
(true, Some(contract_package)) => utils::get_dictionary_value_from_key::<bool>(
ACL_WHITELIST,
&utils::encode_dictionary_item_key(contract_package),
)
.unwrap_or_default(),
_ => utils::get_dictionary_value_from_key::<bool>(
ACL_WHITELIST,
&utils::encode_dictionary_item_key(caller),
)
.unwrap_or_default(),
};
match caller.tag() {
KeyTag::Hash => {
if !is_whitelisted {
runtime::revert(NFTCoreError::UnlistedContractHash)
}
}
KeyTag::Account => {
if !is_whitelisted {
runtime::revert(NFTCoreError::InvalidMinter);
}
}
_ => runtime::revert(NFTCoreError::InvalidKey),
}
}
let metadata_kinds: BTreeMap<NFTMetadataKind, Requirement> =
utils::get_stored_value_with_user_errors(
NFT_METADATA_KINDS,
NFTCoreError::MissingNFTMetadataKind,
NFTCoreError::InvalidNFTMetadataKind,
);
let token_metadata = utils::get_named_arg_with_user_errors::<String>(
ARG_TOKEN_META_DATA,
NFTCoreError::MissingTokenMetaData,
NFTCoreError::InvalidTokenMetaData,
)
.unwrap_or_revert();
let identifier_mode: NFTIdentifierMode = utils::get_stored_value_with_user_errors::<u8>(
IDENTIFIER_MODE,
NFTCoreError::MissingIdentifierMode,
NFTCoreError::InvalidIdentifierMode,
)
.try_into()
.unwrap_or_revert();
// This is the token ID.
let optional_token_hash: String = utils::get_optional_named_arg_with_user_errors::<String>(
ARG_TOKEN_HASH,
NFTCoreError::InvalidIdentifier,
)
.unwrap_or_default();
let token_identifier: TokenIdentifier = match identifier_mode {
NFTIdentifierMode::Ordinal => TokenIdentifier::Index(minted_tokens_count),
NFTIdentifierMode::Hash => TokenIdentifier::Hash(if optional_token_hash.is_empty() {
base16::encode_lower(&runtime::blake2b(token_metadata.clone()))
} else {
optional_token_hash
}),
};
for (metadata_kind, required) in metadata_kinds {
if required == Requirement::Unneeded {
continue;
}
let token_metadata_validation =
metadata::validate_metadata(&metadata_kind, token_metadata.clone());
match token_metadata_validation {
Ok(validated_token_metadata) => {
utils::upsert_dictionary_value_from_key(
&metadata::get_metadata_dictionary_name(&metadata_kind),
&token_identifier.get_dictionary_item_key(),
validated_token_metadata,
);
}
Err(err) => {
if required == Requirement::Required {
runtime::revert(err);
}
}
}
}
// The contract's ownership behavior (determined at installation) determines,
// who owns the NFT we are about to mint.()
let ownership_mode = utils::get_ownership_mode().unwrap_or_revert();
let token_owner_key: Key =
if let OwnershipMode::Assigned | OwnershipMode::Transferable = ownership_mode {
runtime::get_named_arg(ARG_TOKEN_OWNER)
} else {
caller
};
utils::upsert_dictionary_value_from_key(
TOKEN_OWNERS,
&token_identifier.get_dictionary_item_key(),
token_owner_key,
);
utils::upsert_dictionary_value_from_key(
TOKEN_ISSUERS,
&token_identifier.get_dictionary_item_key(),
caller,
);
let owned_tokens_item_key = utils::encode_dictionary_item_key(token_owner_key);
if let NFTIdentifierMode::Hash = identifier_mode {
// Update the forward and reverse trackers
utils::insert_hash_id_lookups(minted_tokens_count, token_identifier.clone());
}
//Increment the count of owned tokens.
let updated_token_count =
match utils::get_dictionary_value_from_key::<u64>(TOKEN_COUNT, &owned_tokens_item_key) {
Some(balance) => balance + 1u64,
None => 1u64,
};
utils::upsert_dictionary_value_from_key(
TOKEN_COUNT,
&owned_tokens_item_key,
updated_token_count,
);
// Increment number_of_minted_tokens by one
let number_of_minted_tokens_uref = utils::get_uref(
NUMBER_OF_MINTED_TOKENS,
NFTCoreError::MissingTotalTokenSupply,
NFTCoreError::InvalidTotalTokenSupply,
);
storage::write(number_of_minted_tokens_uref, minted_tokens_count + 1u64);
// Emit Mint event.
let events_mode: EventsMode =
EventsMode::try_from(utils::get_stored_value_with_user_errors::<u8>(
EVENTS_MODE,
NFTCoreError::MissingEventsMode,
NFTCoreError::InvalidEventsMode,
))
.unwrap_or_revert();
match events_mode {
EventsMode::NoEvents => {}
EventsMode::CES => casper_event_standard::emit(Mint::new(
token_owner_key,
token_identifier.clone(),
token_metadata,
)),
EventsMode::CEP47 => record_cep47_event_dictionary(CEP47Event::Mint {
recipient: token_owner_key,
token_id: token_identifier.clone(),
}),
}
if let OwnerReverseLookupMode::Complete = utils::get_reporting_mode() {
if (NFTIdentifierMode::Hash == identifier_mode)
&& runtime::get_key(OWNED_TOKENS).is_some()
&& utils::should_migrate_token_hashes(token_owner_key)
{
utils::migrate_token_hashes(token_owner_key)
}
let (page_table_entry, page_uref) = utils::add_page_entry_and_page_record(
minted_tokens_count,
&owned_tokens_item_key,
true,
);
let receipt_string = utils::get_receipt_name(page_table_entry);
let receipt_address = Key::dictionary(page_uref, owned_tokens_item_key.as_bytes());
let token_identifier_string = token_identifier.get_dictionary_item_key();
let receipt = CLValue::from_t((receipt_string, receipt_address, token_identifier_string))
.unwrap_or_revert_with(NFTCoreError::FailedToConvertToCLValue);
runtime::ret(receipt)
}
}
// Marks token as burnt. This blocks any future call to transfer token.
#[no_mangle]
pub extern "C" fn burn() {
if let BurnMode::NonBurnable = utils::get_burn_mode() {
runtime::revert(NFTCoreError::InvalidBurnMode)
}
let identifier_mode: NFTIdentifierMode = utils::get_stored_value_with_user_errors::<u8>(
IDENTIFIER_MODE,
NFTCoreError::MissingIdentifierMode,
NFTCoreError::InvalidIdentifierMode,
)
.try_into()
.unwrap_or_revert();
let token_identifier = utils::get_token_identifier_from_runtime_args(&identifier_mode);
let (caller, contract_package): (Key, Option<Key>) =
match utils::get_verified_caller().unwrap_or_revert() {
Caller::Session(account_hash) => (account_hash.into(), None),
Caller::StoredCaller(contract_hash, contract_package_hash) => {
(contract_hash.into(), Some(contract_package_hash.into()))
}
};
let token_owner = match utils::get_dictionary_value_from_key::<Key>(
TOKEN_OWNERS,
&token_identifier.get_dictionary_item_key(),
) {
Some(owner) => owner,
None => runtime::revert(NFTCoreError::MissingOwnerTokenIdentifierKey),
};
// Check if caller is owner
let is_owner = token_owner == caller;
// Check if caller is operator to execute burn
let is_operator = if !is_owner {
let owner_operator_item_key = utils::encode_key_and_value(&token_owner, &caller);
utils::get_dictionary_value_from_key::<bool>(OPERATORS, &owner_operator_item_key)
.unwrap_or_default()
} else {
false
};
// With operator package mode check if caller's package is operator to let contract execute burn
let is_package_operator = if !is_owner && !is_operator {
match (
utils::get_stored_value_with_user_errors::<bool>(
PACKAGE_OPERATOR_MODE,
NFTCoreError::MissingPackageOperatorMode,
NFTCoreError::InvalidPackageOperatorMode,
),
contract_package,
) {
(true, Some(contract_package)) => {
let owner_operator_item_key =
utils::encode_key_and_value(&token_owner, &contract_package);
utils::get_dictionary_value_from_key::<bool>(OPERATORS, &owner_operator_item_key)
.unwrap_or_default()
}
_ => false,
}
} else {
false
};
// Revert if caller is not token_owner nor operator for the owner
if !is_owner && !is_operator && !is_package_operator {
runtime::revert(NFTCoreError::InvalidTokenOwner)
};
// It makes sense to keep this token as owned by the caller. It just happens that the caller
// owns a burnt token. That's all. Similarly, we should probably also not change the
// owned_tokens dictionary.
if utils::is_token_burned(&token_identifier) {
runtime::revert(NFTCoreError::PreviouslyBurntToken)
}
// Mark the token as burnt by adding the token_id to the burnt tokens dictionary.
utils::upsert_dictionary_value_from_key::<()>(
BURNT_TOKENS,
&token_identifier.get_dictionary_item_key(),
(),
);
let owned_tokens_item_key = utils::encode_dictionary_item_key(token_owner);
let updated_balance =
match utils::get_dictionary_value_from_key::<u64>(TOKEN_COUNT, &owned_tokens_item_key) {
Some(balance) => {
if balance > 0u64 {
balance - 1u64
} else {
// This should never happen if contract is implemented correctly.
runtime::revert(NFTCoreError::FatalTokenIdDuplication);
}
}
None => {
// This should never happen if contract is implemented correctly.
runtime::revert(NFTCoreError::FatalTokenIdDuplication);
}
};
utils::upsert_dictionary_value_from_key(TOKEN_COUNT, &owned_tokens_item_key, updated_balance);
// Emit Burn event.
let events_mode: EventsMode =
EventsMode::try_from(utils::get_stored_value_with_user_errors::<u8>(
EVENTS_MODE,
NFTCoreError::MissingEventsMode,
NFTCoreError::InvalidEventsMode,
))
.unwrap_or_revert();
match events_mode {
EventsMode::NoEvents => {}
EventsMode::CES => {
casper_event_standard::emit(Burn::new(token_owner, token_identifier, caller))
}
EventsMode::CEP47 => record_cep47_event_dictionary(CEP47Event::Burn {
owner: token_owner,
token_id: token_identifier,
burner: caller,
}),
}
}
// Marks an account as approved for an identified token transfer