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
use super::*;
use frame_support::{ensure, traits::Get};
use sp_runtime::{DispatchError, DispatchResult};
impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub(crate) fn do_transfer(
class: T::ClassId,
instance: T::InstanceId,
dest: T::AccountId,
with_details: impl FnOnce(
&ClassDetailsFor<T, I>,
&mut InstanceDetailsFor<T, I>,
) -> DispatchResult,
) -> DispatchResult {
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
ensure!(!class_details.is_frozen, Error::<T, I>::Frozen);
let mut details = Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
ensure!(!details.is_frozen, Error::<T, I>::Frozen);
with_details(&class_details, &mut details)?;
Account::<T, I>::remove((&details.owner, &class, &instance));
Account::<T, I>::insert((&dest, &class, &instance), ());
let origin = details.owner;
details.owner = dest;
Asset::<T, I>::insert(&class, &instance, &details);
Self::deposit_event(Event::Transferred(class, instance, origin, details.owner));
Ok(())
}
pub(super) fn do_create_class(
class: T::ClassId,
owner: T::AccountId,
admin: T::AccountId,
deposit: DepositBalanceOf<T, I>,
free_holding: bool,
event: Event<T, I>,
) -> DispatchResult {
ensure!(!Class::<T, I>::contains_key(class), Error::<T, I>::InUse);
T::Currency::reserve(&owner, deposit)?;
Class::<T, I>::insert(
class,
ClassDetails {
owner: owner.clone(),
issuer: admin.clone(),
admin: admin.clone(),
freezer: admin.clone(),
total_deposit: deposit,
free_holding,
instances: 0,
instance_metadatas: 0,
attributes: 0,
is_frozen: false,
},
);
Self::deposit_event(event);
Ok(())
}
pub(super) fn do_mint(
class: T::ClassId,
instance: T::InstanceId,
owner: T::AccountId,
with_details: impl FnOnce(&ClassDetailsFor<T, I>) -> DispatchResult,
) -> DispatchResult {
ensure!(!Asset::<T, I>::contains_key(class, instance), Error::<T, I>::AlreadyExists);
Class::<T, I>::try_mutate(&class, |maybe_class_details| -> DispatchResult {
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
with_details(&class_details)?;
let instances =
class_details.instances.checked_add(1).ok_or(ArithmeticError::Overflow)?;
class_details.instances = instances;
let deposit = match class_details.free_holding {
true => Zero::zero(),
false => T::InstanceDeposit::get(),
};
T::Currency::reserve(&class_details.owner, deposit)?;
class_details.total_deposit += deposit;
let owner = owner.clone();
Account::<T, I>::insert((&owner, &class, &instance), ());
let details = InstanceDetails { owner, approved: None, is_frozen: false, deposit };
Asset::<T, I>::insert(&class, &instance, details);
Ok(())
})?;
Self::deposit_event(Event::Issued(class, instance, owner));
Ok(())
}
pub(super) fn do_burn(
class: T::ClassId,
instance: T::InstanceId,
with_details: impl FnOnce(&ClassDetailsFor<T, I>, &InstanceDetailsFor<T, I>) -> DispatchResult,
) -> DispatchResult {
let owner = Class::<T, I>::try_mutate(
&class,
|maybe_class_details| -> Result<T::AccountId, DispatchError> {
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
let details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
with_details(&class_details, &details)?;
T::Currency::unreserve(&class_details.owner, details.deposit);
class_details.total_deposit.saturating_reduce(details.deposit);
class_details.instances.saturating_dec();
Ok(details.owner)
},
)?;
Asset::<T, I>::remove(&class, &instance);
Account::<T, I>::remove((&owner, &class, &instance));
Self::deposit_event(Event::Burned(class, instance, owner));
Ok(())
}
}