forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.rs
213 lines (182 loc) · 6.61 KB
/
item.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
use core::marker::PhantomData;
use crate::{asset_strategies::Attribute, Item as ItemStorage, *};
use frame_support::{
dispatch::DispatchResult,
ensure,
traits::tokens::asset_ops::{
common_strategies::{
Bytes, CanTransfer, CheckOrigin, CheckState, IfOwnedBy, Owned, Ownership, PredefinedId,
To, Unchecked,
},
AssetDefinition, Create, Inspect, Restore, Stash, Transfer,
},
BoundedSlice,
};
use frame_system::ensure_signed;
use sp_runtime::DispatchError;
pub struct Item<PalletInstance>(PhantomData<PalletInstance>);
impl<T: Config<I>, I: 'static> AssetDefinition for Item<Pallet<T, I>> {
type Id = (T::CollectionId, T::ItemId);
}
impl<T: Config<I>, I: 'static> Inspect<Ownership<T::AccountId>> for Item<Pallet<T, I>> {
fn inspect(
(collection, item): &Self::Id,
_ownership: Ownership<T::AccountId>,
) -> Result<T::AccountId, DispatchError> {
ItemStorage::<T, I>::get(collection, item)
.map(|a| a.owner)
.ok_or(Error::<T, I>::UnknownItem.into())
}
}
impl<T: Config<I>, I: 'static> Inspect<Bytes> for Item<Pallet<T, I>> {
fn inspect((collection, item): &Self::Id, _bytes: Bytes) -> Result<Vec<u8>, DispatchError> {
ItemMetadataOf::<T, I>::get(collection, item)
.map(|m| m.data.into())
.ok_or(Error::<T, I>::NoMetadata.into())
}
}
impl<'a, T: Config<I>, I: 'static> Inspect<Bytes<Attribute<'a>>> for Item<Pallet<T, I>> {
fn inspect(
(collection, item): &Self::Id,
strategy: Bytes<Attribute>,
) -> Result<Vec<u8>, DispatchError> {
let Bytes(Attribute(attribute)) = strategy;
let attribute =
BoundedSlice::try_from(attribute).map_err(|_| Error::<T, I>::WrongAttribute)?;
crate::Attribute::<T, I>::get((collection, Some(item), attribute))
.map(|a| a.0.into())
.ok_or(Error::<T, I>::AttributeNotFound.into())
}
}
impl<T: Config<I>, I: 'static> Inspect<CanTransfer> for Item<Pallet<T, I>> {
fn inspect(
(collection, item): &Self::Id,
_can_transfer: CanTransfer,
) -> Result<bool, DispatchError> {
match (Collection::<T, I>::get(collection), ItemStorage::<T, I>::get(collection, item)) {
(Some(cd), Some(id)) => Ok(!cd.is_frozen && !id.is_frozen),
_ => Err(Error::<T, I>::UnknownItem.into()),
}
}
}
impl<T: Config<I>, I: 'static>
Create<Owned<T::AccountId, PredefinedId<(T::CollectionId, T::ItemId)>>> for Item<Pallet<T, I>>
{
fn create(
strategy: Owned<T::AccountId, PredefinedId<(T::CollectionId, T::ItemId)>>,
) -> Result<(T::CollectionId, T::ItemId), DispatchError> {
let Owned { owner, id_assignment, .. } = strategy;
let (collection, item) = id_assignment.params;
<Pallet<T, I>>::do_mint(collection.clone(), item, owner, |_| Ok(()))?;
Ok((collection, item))
}
}
impl<T: Config<I>, I: 'static>
Create<
CheckOrigin<
T::RuntimeOrigin,
Owned<T::AccountId, PredefinedId<(T::CollectionId, T::ItemId)>>,
>,
> for Item<Pallet<T, I>>
{
fn create(
strategy: CheckOrigin<
T::RuntimeOrigin,
Owned<T::AccountId, PredefinedId<(T::CollectionId, T::ItemId)>>,
>,
) -> Result<(T::CollectionId, T::ItemId), DispatchError> {
let CheckOrigin(origin, Owned { owner, id_assignment, .. }) = strategy;
let (collection, item) = id_assignment.params;
let signer = ensure_signed(origin)?;
<Pallet<T, I>>::do_mint(collection.clone(), item, owner, |collection_details| {
ensure!(collection_details.issuer == signer, Error::<T, I>::NoPermission);
Ok(())
})?;
Ok((collection, item))
}
}
impl<T: Config<I>, I: 'static> Transfer<To<T::AccountId>> for Item<Pallet<T, I>> {
fn transfer((collection, item): &Self::Id, strategy: To<T::AccountId>) -> DispatchResult {
let To(dest) = strategy;
<Pallet<T, I>>::do_transfer(collection.clone(), *item, dest, |_, _| Ok(()))
}
}
impl<T: Config<I>, I: 'static> Transfer<CheckOrigin<T::RuntimeOrigin, To<T::AccountId>>>
for Item<Pallet<T, I>>
{
fn transfer(
(collection, item): &Self::Id,
strategy: CheckOrigin<T::RuntimeOrigin, To<T::AccountId>>,
) -> DispatchResult {
let CheckOrigin(origin, To(dest)) = strategy;
let signer = ensure_signed(origin)?;
<Pallet<T, I>>::do_transfer(
collection.clone(),
*item,
dest.clone(),
|collection_details, details| {
if details.owner != signer && collection_details.admin != signer {
let approved = details.approved.take().map_or(false, |i| i == signer);
ensure!(approved, Error::<T, I>::NoPermission);
}
Ok(())
},
)
}
}
impl<T: Config<I>, I: 'static> Transfer<IfOwnedBy<T::AccountId, To<T::AccountId>>>
for Item<Pallet<T, I>>
{
fn transfer(
(collection, item): &Self::Id,
strategy: IfOwnedBy<T::AccountId, To<T::AccountId>>,
) -> DispatchResult {
let CheckState(from, To(to)) = strategy;
<Pallet<T, I>>::do_transfer(collection.clone(), *item, to.clone(), |_, details| {
ensure!(details.owner == from, Error::<T, I>::WrongOwner);
Ok(())
})
}
}
impl<T: Config<I>, I: 'static> Stash<Unchecked> for Item<Pallet<T, I>> {
fn stash((collection, item): &Self::Id, _strategy: Unchecked) -> DispatchResult {
<Pallet<T, I>>::do_burn(collection.clone(), *item, |_, _| Ok(()))
}
}
impl<T: Config<I>, I: 'static> Stash<CheckOrigin<T::RuntimeOrigin>> for Item<Pallet<T, I>> {
fn stash(
(collection, item): &Self::Id,
strategy: CheckOrigin<T::RuntimeOrigin>,
) -> DispatchResult {
let CheckOrigin(origin, ..) = strategy;
let signer = ensure_signed(origin)?;
<Pallet<T, I>>::do_burn(collection.clone(), *item, |collection_details, details| {
let is_permitted = collection_details.admin == signer || details.owner == signer;
ensure!(is_permitted, Error::<T, I>::NoPermission);
Ok(())
})
}
}
impl<T: Config<I>, I: 'static> Stash<IfOwnedBy<T::AccountId>> for Item<Pallet<T, I>> {
fn stash((collection, item): &Self::Id, strategy: IfOwnedBy<T::AccountId>) -> DispatchResult {
let CheckState(who, ..) = strategy;
<Pallet<T, I>>::do_burn(collection.clone(), *item, |_, d| {
ensure!(d.owner == who, Error::<T, I>::NoPermission);
Ok(())
})
}
}
// NOTE: pallet-uniques create and restore operations are equivalent.
// If an NFT was burned, it can be "re-created" (equivalently, "restored").
// It will be "re-created" with all the data still bound to it.
// If an NFT is minted for the first time, it can be regarded as "restored" with an empty data
// because it is indistinguishable from a burned empty NFT from the chain's perspective.
impl<T: Config<I>, I: 'static> Restore<To<T::AccountId>> for Item<Pallet<T, I>> {
fn restore((collection, item): &Self::Id, strategy: To<T::AccountId>) -> DispatchResult {
let To(owner) = strategy;
let item_exists = ItemStorage::<T, I>::contains_key(collection, item);
ensure!(!item_exists, Error::<T, I>::InUse);
Self::create(Owned::new(owner, PredefinedId::from((collection.clone(), item.clone()))))?;
Ok(())
}
}