1use alloc::vec::Vec;
21use frame_support::{
22 defensive,
23 traits::tokens::{
24 Fortitude,
25 Precision::{self, BestEffort},
26 Preservation::{self, Expendable},
27 Provenance::{self, Minted},
28 },
29};
30
31use super::*;
32
33impl<T: Config<I>, I: 'static> fungibles::Inspect<<T as SystemConfig>::AccountId> for Pallet<T, I> {
34 type AssetId = T::AssetId;
35 type Balance = T::Balance;
36
37 fn total_issuance(asset: Self::AssetId) -> Self::Balance {
38 Asset::<T, I>::get(asset).map(|x| x.supply).unwrap_or_else(Zero::zero)
39 }
40
41 fn minimum_balance(asset: Self::AssetId) -> Self::Balance {
42 Asset::<T, I>::get(asset).map(|x| x.min_balance).unwrap_or_else(Zero::zero)
43 }
44
45 fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {
46 Pallet::<T, I>::balance(asset, who)
47 }
48
49 fn total_balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {
50 Pallet::<T, I>::balance(asset.clone(), who)
51 .saturating_add(T::Holder::balance_on_hold(asset, who).unwrap_or_default())
52 }
53
54 fn reducible_balance(
55 asset: Self::AssetId,
56 who: &<T as SystemConfig>::AccountId,
57 preservation: Preservation,
58 _: Fortitude,
59 ) -> Self::Balance {
60 Pallet::<T, I>::reducible_balance(asset, who, !matches!(preservation, Expendable))
61 .unwrap_or(Zero::zero())
62 }
63
64 fn can_deposit(
65 asset: Self::AssetId,
66 who: &<T as SystemConfig>::AccountId,
67 amount: Self::Balance,
68 provenance: Provenance,
69 ) -> DepositConsequence {
70 Pallet::<T, I>::can_increase(asset, who, amount, provenance == Minted)
71 }
72
73 fn can_withdraw(
74 asset: Self::AssetId,
75 who: &<T as SystemConfig>::AccountId,
76 amount: Self::Balance,
77 ) -> WithdrawConsequence<Self::Balance> {
78 Pallet::<T, I>::can_decrease(asset, who, amount, false)
79 }
80
81 fn asset_exists(asset: Self::AssetId) -> bool {
82 Asset::<T, I>::contains_key(asset)
83 }
84}
85
86impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
87 fn done_mint_into(
88 asset_id: Self::AssetId,
89 beneficiary: &<T as SystemConfig>::AccountId,
90 amount: Self::Balance,
91 ) {
92 Self::deposit_event(Event::Issued { asset_id, owner: beneficiary.clone(), amount })
93 }
94
95 fn done_burn_from(
96 asset_id: Self::AssetId,
97 target: &<T as SystemConfig>::AccountId,
98 balance: Self::Balance,
99 ) {
100 Self::deposit_event(Event::Burned { asset_id, owner: target.clone(), balance });
101 }
102
103 fn done_transfer(
104 asset_id: Self::AssetId,
105 source: &<T as SystemConfig>::AccountId,
106 dest: &<T as SystemConfig>::AccountId,
107 amount: Self::Balance,
108 ) {
109 Self::deposit_event(Event::Transferred {
110 asset_id,
111 from: source.clone(),
112 to: dest.clone(),
113 amount,
114 });
115 }
116}
117
118impl<T: Config<I>, I: 'static> fungibles::Balanced<<T as SystemConfig>::AccountId>
119 for Pallet<T, I>
120{
121 type OnDropCredit = fungibles::DecreaseIssuance<T::AccountId, Self>;
122 type OnDropDebt = fungibles::IncreaseIssuance<T::AccountId, Self>;
123
124 fn done_deposit(
125 asset_id: Self::AssetId,
126 who: &<T as SystemConfig>::AccountId,
127 amount: Self::Balance,
128 ) {
129 Self::deposit_event(Event::Deposited { asset_id, who: who.clone(), amount })
130 }
131
132 fn done_withdraw(
133 asset_id: Self::AssetId,
134 who: &<T as SystemConfig>::AccountId,
135 amount: Self::Balance,
136 ) {
137 Self::deposit_event(Event::Withdrawn { asset_id, who: who.clone(), amount })
138 }
139}
140
141impl<T: Config<I>, I: 'static> fungibles::Unbalanced<T::AccountId> for Pallet<T, I> {
142 fn handle_raw_dust(_: Self::AssetId, _: Self::Balance) {}
143 fn handle_dust(_: fungibles::Dust<T::AccountId, Self>) {
144 defensive!("`decrease_balance` and `increase_balance` have non-default impls; nothing else calls this; qed");
145 }
146 fn write_balance(
147 _: Self::AssetId,
148 _: &T::AccountId,
149 _: Self::Balance,
150 ) -> Result<Option<Self::Balance>, DispatchError> {
151 defensive!("write_balance is not used if other functions are impl'd");
152 Err(DispatchError::Unavailable)
153 }
154 fn set_total_issuance(id: T::AssetId, amount: Self::Balance) {
155 Asset::<T, I>::mutate_exists(id, |maybe_asset| {
156 if let Some(ref mut asset) = maybe_asset {
157 asset.supply = amount
158 }
159 });
160 }
161 fn decrease_balance(
162 asset: T::AssetId,
163 who: &T::AccountId,
164 amount: Self::Balance,
165 precision: Precision,
166 preservation: Preservation,
167 _: Fortitude,
168 ) -> Result<Self::Balance, DispatchError> {
169 let f = DebitFlags {
170 keep_alive: preservation != Expendable,
171 best_effort: precision == BestEffort,
172 };
173 Self::decrease_balance(asset, who, amount, f, |_, _| Ok(()))
174 }
175 fn increase_balance(
176 asset: T::AssetId,
177 who: &T::AccountId,
178 amount: Self::Balance,
179 _: Precision,
180 ) -> Result<Self::Balance, DispatchError> {
181 Self::increase_balance(asset, who, amount, |_| Ok(()))?;
182 Ok(amount)
183 }
184
185 }
187
188impl<T: Config<I>, I: 'static> fungibles::Create<T::AccountId> for Pallet<T, I> {
189 fn create(
190 id: T::AssetId,
191 admin: T::AccountId,
192 is_sufficient: bool,
193 min_balance: Self::Balance,
194 ) -> DispatchResult {
195 Self::do_force_create(id, admin, is_sufficient, min_balance)
196 }
197}
198
199impl<T: Config<I>, I: 'static> fungibles::Destroy<T::AccountId> for Pallet<T, I> {
200 fn start_destroy(id: T::AssetId, maybe_check_owner: Option<T::AccountId>) -> DispatchResult {
201 Self::do_start_destroy(id, maybe_check_owner)
202 }
203
204 fn destroy_accounts(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
205 Self::do_destroy_accounts(id, max_items)
206 }
207
208 fn destroy_approvals(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
209 Self::do_destroy_approvals(id, max_items)
210 }
211
212 fn finish_destroy(id: T::AssetId) -> DispatchResult {
213 Self::do_finish_destroy(id)
214 }
215}
216
217impl<T: Config<I>, I: 'static> fungibles::metadata::Inspect<<T as SystemConfig>::AccountId>
218 for Pallet<T, I>
219{
220 fn name(asset: T::AssetId) -> Vec<u8> {
221 Metadata::<T, I>::get(asset).name.to_vec()
222 }
223
224 fn symbol(asset: T::AssetId) -> Vec<u8> {
225 Metadata::<T, I>::get(asset).symbol.to_vec()
226 }
227
228 fn decimals(asset: T::AssetId) -> u8 {
229 Metadata::<T, I>::get(asset).decimals
230 }
231}
232
233impl<T: Config<I>, I: 'static> fungibles::metadata::Mutate<<T as SystemConfig>::AccountId>
234 for Pallet<T, I>
235{
236 fn set(
237 asset: T::AssetId,
238 from: &<T as SystemConfig>::AccountId,
239 name: Vec<u8>,
240 symbol: Vec<u8>,
241 decimals: u8,
242 ) -> DispatchResult {
243 Self::do_set_metadata(asset, from, name, symbol, decimals)
244 }
245}
246
247impl<T: Config<I>, I: 'static>
248 fungibles::metadata::MetadataDeposit<
249 <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance,
250 > for Pallet<T, I>
251{
252 fn calc_metadata_deposit(
253 name: &[u8],
254 symbol: &[u8],
255 ) -> <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance {
256 Self::calc_metadata_deposit(&name, &symbol)
257 }
258}
259
260impl<T: Config<I>, I: 'static> fungibles::approvals::Inspect<<T as SystemConfig>::AccountId>
261 for Pallet<T, I>
262{
263 fn allowance(
265 asset: T::AssetId,
266 owner: &<T as SystemConfig>::AccountId,
267 delegate: &<T as SystemConfig>::AccountId,
268 ) -> T::Balance {
269 Approvals::<T, I>::get((asset, &owner, &delegate))
270 .map(|x| x.amount)
271 .unwrap_or_else(Zero::zero)
272 }
273}
274
275impl<T: Config<I>, I: 'static> fungibles::approvals::Mutate<<T as SystemConfig>::AccountId>
276 for Pallet<T, I>
277{
278 fn approve(
280 asset: T::AssetId,
281 owner: &<T as SystemConfig>::AccountId,
282 delegate: &<T as SystemConfig>::AccountId,
283 amount: T::Balance,
284 ) -> DispatchResult {
285 Self::do_approve_transfer(asset, owner, delegate, amount)
286 }
287
288 fn transfer_from(
289 asset: T::AssetId,
290 owner: &<T as SystemConfig>::AccountId,
291 delegate: &<T as SystemConfig>::AccountId,
292 dest: &<T as SystemConfig>::AccountId,
293 amount: T::Balance,
294 ) -> DispatchResult {
295 Self::do_transfer_approved(asset, owner, delegate, dest, amount)
296 }
297}
298
299impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::AccountId>
300 for Pallet<T, I>
301{
302 fn owner(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
303 Asset::<T, I>::get(asset).map(|x| x.owner)
304 }
305
306 fn issuer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
307 Asset::<T, I>::get(asset).map(|x| x.issuer)
308 }
309
310 fn admin(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
311 Asset::<T, I>::get(asset).map(|x| x.admin)
312 }
313
314 fn freezer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
315 Asset::<T, I>::get(asset).map(|x| x.freezer)
316 }
317}
318
319impl<T: Config<I>, I: 'static> fungibles::InspectEnumerable<T::AccountId> for Pallet<T, I> {
320 type AssetsIterator = KeyPrefixIterator<<T as Config<I>>::AssetId>;
321
322 fn asset_ids() -> Self::AssetsIterator {
326 Asset::<T, I>::iter_keys()
327 }
328}
329
330impl<T: Config<I>, I: 'static> fungibles::roles::ResetTeam<T::AccountId> for Pallet<T, I> {
331 fn reset_team(
332 id: T::AssetId,
333 owner: T::AccountId,
334 admin: T::AccountId,
335 issuer: T::AccountId,
336 freezer: T::AccountId,
337 ) -> DispatchResult {
338 Self::do_reset_team(id, owner, admin, issuer, freezer)
339 }
340}
341
342impl<T: Config<I>, I: 'static> fungibles::Refund<T::AccountId> for Pallet<T, I> {
343 type AssetId = T::AssetId;
344 type Balance = DepositBalanceOf<T, I>;
345 fn deposit_held(id: Self::AssetId, who: T::AccountId) -> Option<(T::AccountId, Self::Balance)> {
346 use ExistenceReason::*;
347 match Account::<T, I>::get(&id, &who).ok_or(Error::<T, I>::NoDeposit).ok()?.reason {
348 DepositHeld(b) => Some((who, b)),
349 DepositFrom(d, b) => Some((d, b)),
350 _ => None,
351 }
352 }
353 fn refund(id: Self::AssetId, who: T::AccountId) -> DispatchResult {
354 match Self::deposit_held(id.clone(), who.clone()) {
355 Some((d, _)) if d == who => Self::do_refund(id, who, false),
356 Some(..) => Self::do_refund_other(id, &who, None),
357 None => Err(Error::<T, I>::NoDeposit.into()),
358 }
359 }
360}