-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
152 lines (129 loc) · 3.98 KB
/
lib.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
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
/// The log target of this pallet.
pub const LOG_TARGET: &str = "runtime::nft_computing";
// Syntactic sugar for logging.
#[macro_export]
macro_rules! log {
($level:tt, $patter:expr $(, $values:expr)* $(,)?) => {
log::$level!(
target: $crate::LOG_TARGET,
concat!("[{:?}] ", $patter), <frame_system::Pallet<T>>::block_number() $(, $values)*
)
};
}
use frame_support::{
traits::{
tokens::nonfungibles_v2::{self, *},
Currency, ReservableCurrency,
},
};
use pallet_nfts::{
MintType, CollectionSettings, CollectionSetting, ItemSettings, ItemSetting,
ItemConfig
};
pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub(crate) type CollectionIdOf<T> =
<<T as Config>::NFTCollection as nonfungibles_v2::Inspect<<T as frame_system::Config>::AccountId>>::CollectionId;
pub(crate) type CollectionConfigOf<T> = pallet_nfts::CollectionConfig<
BalanceOf<T>,
<T as frame_system::Config>::BlockNumber,
CollectionIdOf<T>
>;
pub(crate) type MintSettingsOf<T> = pallet_nfts::MintSettings<
BalanceOf<T>,
<T as frame_system::Config>::BlockNumber,
CollectionIdOf<T>
>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The system's currency for payment.
type Currency: ReservableCurrency<Self::AccountId>;
type NFTCollection: nonfungibles_v2::Create<Self::AccountId, CollectionConfigOf<Self>> +
nonfungibles_v2::Destroy<Self::AccountId> +
nonfungibles_v2::Mutate<Self::AccountId, ItemConfig>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
CollectionCreated { who: T::AccountId, collection_id: CollectionIdOf<T> },
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
NotTheOwner,
WorkerNotExists,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn create_collection(
origin: OriginFor<T>
) -> DispatchResult {
let who = ensure_signed(origin)?;
let collection_config = CollectionConfigOf::<T> {
settings: CollectionSettings::from_disabled(
CollectionSetting::TransferableItems |
CollectionSetting::UnlockedMetadata |
CollectionSetting::UnlockedAttributes |
CollectionSetting::UnlockedMaxSupply
),
max_supply: None,
mint_settings: MintSettingsOf::<T> {
mint_type: MintType::Public,
price: None,
start_block: None,
end_block: None,
default_item_settings: ItemSettings::from_disabled(
ItemSetting::Transferable |
ItemSetting::UnlockedMetadata |
ItemSetting::UnlockedAttributes
),
}
};
let collection_id =
T::NFTCollection::create_collection(&who, &who, &collection_config)?;
// TODO: add a mapping
// TODO: CollectionId need Debug and Clone, need PR to Substrate
Self::deposit_event(Event::CollectionCreated { who: who.clone(), collection_id });
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn mint(
origin: OriginFor<T>,
collection: CollectionIdOf<T>
) -> DispatchResult {
let who = ensure_signed(origin)?;
let config = ItemConfig::default();
T::NFTCollection::mint_into(
collection,
1u32.into(),
&who,
&config,
false,
)?;
Ok(())
}
}
}