-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
219 lines (194 loc) · 6.3 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
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
#![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::Currency;
use pallet_nfts::{
MintType, CollectionSettings, CollectionSetting, ItemSettings, ItemSetting,
ItemConfig,
Incrementable,
};
pub type BalanceOf<T, I = ()> =
<<T as pallet_nfts::Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type CollectionIdOf<T, I = ()> = <T as pallet_nfts::Config<I>>::CollectionId;
pub type ItemIdOf<T, I = ()> = <T as pallet_nfts::Config<I>>::ItemId;
pub type CollectionDepositOf<T, I = ()> = <T as pallet_nfts::Config<I>>::CollectionDeposit;
pub type CollectionConfigOf<T, I = ()> = pallet_nfts::CollectionConfig<
BalanceOf<T, I>,
<T as frame_system::Config>::BlockNumber,
CollectionIdOf<T, I>
>;
pub type MintSettingsOf<T, I = ()> = pallet_nfts::MintSettings<
BalanceOf<T, I>,
<T as frame_system::Config>::BlockNumber,
CollectionIdOf<T, I>
>;
pub type PalletNFT<T, I = ()> = pallet_nfts::Pallet<T, I>;
#[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, I = ()>(_);
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config + pallet_nfts::Config<I> {
/// Because this pallet emits events, it depends on the runtime definition of an event.
type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
CollectionCreated { worker: T::AccountId, collection_id: CollectionIdOf<T, I> },
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T, I = ()> {
NotTheOwner,
WorkerNotExists,
}
#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn create_collection(
origin: OriginFor<T>,
worker: T::AccountId
) -> DispatchResult {
let who = ensure_signed(origin)?;
let collection_config = CollectionConfigOf::<T, I> {
settings: CollectionSettings::from_disabled(
CollectionSetting::TransferableItems |
CollectionSetting::UnlockedMetadata |
CollectionSetting::UnlockedAttributes |
CollectionSetting::UnlockedMaxSupply
),
max_supply: None,
mint_settings: MintSettingsOf::<T, I> {
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 =
pallet_nfts::NextCollectionId::<T, I>::get().unwrap_or(CollectionIdOf::<T, I>::initial_value());
pallet_nfts::Pallet::<T, I>::do_create_collection(
collection,
who.clone(),
who.clone(),
collection_config,
CollectionDepositOf::<T, I>::get(),
pallet_nfts::Event::<T, I>::Created { collection, creator: who.clone(), owner: who.clone() },
)?;
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn mint(
origin: OriginFor<T>,
collection_id: CollectionIdOf<T, I>
) -> DispatchResult {
let who = ensure_signed(origin)?;
// let item_id: ItemIdOf<T, I> = 0u32;
let item_config = ItemConfig {
settings: ItemSettings::from_disabled(
ItemSetting::Transferable | ItemSetting::UnlockedMetadata
)
};
pallet_nfts::Pallet::<T, I>::do_mint(
collection_id,
0u32.into(),
Some(who.clone()),
who.clone(),
item_config,
|collection_details, collection_config| {
// // Issuer can mint regardless of mint settings
// if Self::has_role(&collection, &caller, CollectionRole::Issuer) {
// return Ok(())
// }
//
// let mint_settings = collection_config.mint_settings;
// let now = frame_system::Pallet::<T>::block_number();
//
// if let Some(start_block) = mint_settings.start_block {
// ensure!(start_block <= now, Error::<T, I>::MintNotStarted);
// }
// if let Some(end_block) = mint_settings.end_block {
// ensure!(end_block >= now, Error::<T, I>::MintEnded);
// }
//
// match mint_settings.mint_type {
// MintType::Issuer => return Err(Error::<T, I>::NoPermission.into()),
// MintType::HolderOf(collection_id) => {
// let MintWitness { owner_of_item } =
// witness_data.ok_or(Error::<T, I>::BadWitness)?;
//
// let has_item = Account::<T, I>::contains_key((
// &caller,
// &collection_id,
// &owner_of_item,
// ));
// ensure!(has_item, Error::<T, I>::BadWitness);
//
// let attribute_key = Self::construct_attribute_key(
// PalletAttributes::<T::CollectionId>::UsedToClaim(collection)
// .encode(),
// )?;
//
// let key = (
// &collection_id,
// Some(owner_of_item),
// AttributeNamespace::Pallet,
// &attribute_key,
// );
// let already_claimed = Attribute::<T, I>::contains_key(key.clone());
// ensure!(!already_claimed, Error::<T, I>::AlreadyClaimed);
//
// let value = Self::construct_attribute_value(vec![0])?;
// Attribute::<T, I>::insert(
// key,
// (value, AttributeDeposit { account: None, amount: Zero::zero() }),
// );
// },
// _ => {},
// }
// if let Some(price) = mint_settings.price {
// T::Currency::transfer(
// &caller,
// &collection_details.owner,
// price,
// ExistenceRequirement::KeepAlive,
// )?;
// }
Ok(())
},
)?;
Ok(())
}
}
}