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
118pub struct IncreaseIssuanceWithEvent<T, I>(PhantomData<(T, I)>);
121impl<T: Config<I>, I: 'static>
122 fungibles::HandleImbalanceDrop<<T as Config<I>>::AssetId, <T as Config<I>>::Balance>
123 for IncreaseIssuanceWithEvent<T, I>
124{
125 fn handle(asset_id: <T as Config<I>>::AssetId, amount: <T as Config<I>>::Balance) {
126 fungibles::IncreaseIssuance::<T::AccountId, Pallet<T, I>>::handle(asset_id.clone(), amount);
127 Pallet::<T, I>::deposit_event(Event::BurnedDebt { asset_id, amount });
128 }
129}
130
131pub struct DecreaseIssuanceWithEvent<T, I>(PhantomData<(T, I)>);
134impl<T: Config<I>, I: 'static>
135 fungibles::HandleImbalanceDrop<<T as Config<I>>::AssetId, <T as Config<I>>::Balance>
136 for DecreaseIssuanceWithEvent<T, I>
137{
138 fn handle(asset_id: <T as Config<I>>::AssetId, amount: <T as Config<I>>::Balance) {
139 fungibles::DecreaseIssuance::<T::AccountId, Pallet<T, I>>::handle(asset_id.clone(), amount);
140 Pallet::<T, I>::deposit_event(Event::BurnedCredit { asset_id, amount });
141 }
142}
143
144impl<T: Config<I>, I: 'static> fungibles::Balanced<<T as SystemConfig>::AccountId>
145 for Pallet<T, I>
146{
147 type OnDropCredit = DecreaseIssuanceWithEvent<T, I>;
148 type OnDropDebt = IncreaseIssuanceWithEvent<T, I>;
149
150 fn done_deposit(
151 asset_id: Self::AssetId,
152 who: &<T as SystemConfig>::AccountId,
153 amount: Self::Balance,
154 ) {
155 Self::deposit_event(Event::Deposited { asset_id, who: who.clone(), amount })
156 }
157
158 fn done_withdraw(
159 asset_id: Self::AssetId,
160 who: &<T as SystemConfig>::AccountId,
161 amount: Self::Balance,
162 ) {
163 Self::deposit_event(Event::Withdrawn { asset_id, who: who.clone(), amount })
164 }
165
166 fn done_rescind(asset_id: Self::AssetId, amount: Self::Balance) {
167 Self::deposit_event(Event::IssuedDebt { asset_id, amount })
168 }
169
170 fn done_issue(asset_id: Self::AssetId, amount: Self::Balance) {
171 Self::deposit_event(Event::IssuedCredit { asset_id, amount })
172 }
173}
174
175impl<T: Config<I>, I: 'static> fungibles::Unbalanced<T::AccountId> for Pallet<T, I> {
176 fn handle_raw_dust(_: Self::AssetId, _: Self::Balance) {}
177 fn handle_dust(_: fungibles::Dust<T::AccountId, Self>) {
178 defensive!("`decrease_balance` and `increase_balance` have non-default impls; nothing else calls this; qed");
179 }
180 fn write_balance(
181 _: Self::AssetId,
182 _: &T::AccountId,
183 _: Self::Balance,
184 ) -> Result<Option<Self::Balance>, DispatchError> {
185 defensive!("write_balance is not used if other functions are impl'd");
186 Err(DispatchError::Unavailable)
187 }
188 fn set_total_issuance(id: T::AssetId, amount: Self::Balance) {
189 Asset::<T, I>::mutate_exists(id, |maybe_asset| {
190 if let Some(ref mut asset) = maybe_asset {
191 asset.supply = amount
192 }
193 });
194 }
195 fn decrease_balance(
196 asset: T::AssetId,
197 who: &T::AccountId,
198 amount: Self::Balance,
199 precision: Precision,
200 preservation: Preservation,
201 _: Fortitude,
202 ) -> Result<Self::Balance, DispatchError> {
203 let f = DebitFlags {
204 keep_alive: preservation != Expendable,
205 best_effort: precision == BestEffort,
206 };
207 Self::decrease_balance(asset, who, amount, f, |_, _| Ok(()))
208 }
209 fn increase_balance(
210 asset: T::AssetId,
211 who: &T::AccountId,
212 amount: Self::Balance,
213 _: Precision,
214 ) -> Result<Self::Balance, DispatchError> {
215 Self::increase_balance(asset, who, amount, |_| Ok(()))?;
216 Ok(amount)
217 }
218
219 }
221
222impl<T: Config<I>, I: 'static> fungibles::Create<T::AccountId> for Pallet<T, I> {
223 fn create(
224 id: T::AssetId,
225 admin: T::AccountId,
226 is_sufficient: bool,
227 min_balance: Self::Balance,
228 ) -> DispatchResult {
229 Self::do_force_create(id, admin, is_sufficient, min_balance, true)
231 }
232}
233
234impl<T: Config<I>, I: 'static> fungibles::Destroy<T::AccountId> for Pallet<T, I> {
235 fn start_destroy(id: T::AssetId, maybe_check_owner: Option<T::AccountId>) -> DispatchResult {
236 Self::do_start_destroy(id, maybe_check_owner)
237 }
238
239 fn destroy_accounts(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
240 Self::do_destroy_accounts(id, max_items)
241 }
242
243 fn destroy_approvals(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
244 Self::do_destroy_approvals(id, max_items)
245 }
246
247 fn finish_destroy(id: T::AssetId) -> DispatchResult {
248 Self::do_finish_destroy(id)
249 }
250}
251
252impl<T: Config<I>, I: 'static> fungibles::metadata::Inspect<<T as SystemConfig>::AccountId>
253 for Pallet<T, I>
254{
255 fn name(asset: T::AssetId) -> Vec<u8> {
256 Metadata::<T, I>::get(asset).name.to_vec()
257 }
258
259 fn symbol(asset: T::AssetId) -> Vec<u8> {
260 Metadata::<T, I>::get(asset).symbol.to_vec()
261 }
262
263 fn decimals(asset: T::AssetId) -> u8 {
264 Metadata::<T, I>::get(asset).decimals
265 }
266}
267
268impl<T: Config<I>, I: 'static> fungibles::metadata::Mutate<<T as SystemConfig>::AccountId>
269 for Pallet<T, I>
270{
271 fn set(
272 asset: T::AssetId,
273 from: &<T as SystemConfig>::AccountId,
274 name: Vec<u8>,
275 symbol: Vec<u8>,
276 decimals: u8,
277 ) -> DispatchResult {
278 Self::do_set_metadata(asset, from, name, symbol, decimals)
279 }
280}
281
282impl<T: Config<I>, I: 'static>
283 fungibles::metadata::MetadataDeposit<
284 <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance,
285 > for Pallet<T, I>
286{
287 fn calc_metadata_deposit(
288 name: &[u8],
289 symbol: &[u8],
290 ) -> <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance {
291 Self::calc_metadata_deposit(&name, &symbol)
292 }
293}
294
295impl<T: Config<I>, I: 'static> fungibles::approvals::Inspect<<T as SystemConfig>::AccountId>
296 for Pallet<T, I>
297{
298 fn allowance(
300 asset: T::AssetId,
301 owner: &<T as SystemConfig>::AccountId,
302 delegate: &<T as SystemConfig>::AccountId,
303 ) -> T::Balance {
304 Approvals::<T, I>::get((asset, &owner, &delegate))
305 .map(|x| x.amount)
306 .unwrap_or_else(Zero::zero)
307 }
308}
309
310impl<T: Config<I>, I: 'static> fungibles::approvals::Mutate<<T as SystemConfig>::AccountId>
311 for Pallet<T, I>
312{
313 fn approve(
315 asset: T::AssetId,
316 owner: &<T as SystemConfig>::AccountId,
317 delegate: &<T as SystemConfig>::AccountId,
318 amount: T::Balance,
319 ) -> DispatchResult {
320 Self::do_approve_transfer(asset, owner, delegate, amount)
321 }
322
323 fn transfer_from(
324 asset: T::AssetId,
325 owner: &<T as SystemConfig>::AccountId,
326 delegate: &<T as SystemConfig>::AccountId,
327 dest: &<T as SystemConfig>::AccountId,
328 amount: T::Balance,
329 ) -> DispatchResult {
330 Self::do_transfer_approved(asset, owner, delegate, dest, amount)
331 }
332}
333
334impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::AccountId>
335 for Pallet<T, I>
336{
337 fn owner(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
338 Asset::<T, I>::get(asset).map(|x| x.owner)
339 }
340
341 fn issuer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
342 Asset::<T, I>::get(asset).map(|x| x.issuer)
343 }
344
345 fn admin(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
346 Asset::<T, I>::get(asset).map(|x| x.admin)
347 }
348
349 fn freezer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
350 Asset::<T, I>::get(asset).map(|x| x.freezer)
351 }
352}
353
354impl<T: Config<I>, I: 'static> fungibles::InspectEnumerable<T::AccountId> for Pallet<T, I> {
355 type AssetsIterator = KeyPrefixIterator<<T as Config<I>>::AssetId>;
356
357 fn asset_ids() -> Self::AssetsIterator {
361 Asset::<T, I>::iter_keys()
362 }
363}
364
365impl<T: Config<I>, I: 'static> fungibles::roles::ResetTeam<T::AccountId> for Pallet<T, I> {
366 fn reset_team(
367 id: T::AssetId,
368 owner: T::AccountId,
369 admin: T::AccountId,
370 issuer: T::AccountId,
371 freezer: T::AccountId,
372 ) -> DispatchResult {
373 Self::do_reset_team(id, owner, admin, issuer, freezer)
374 }
375}
376
377impl<T: Config<I>, I: 'static> fungibles::Refund<T::AccountId> for Pallet<T, I> {
378 type AssetId = T::AssetId;
379 type Balance = DepositBalanceOf<T, I>;
380 fn deposit_held(id: Self::AssetId, who: T::AccountId) -> Option<(T::AccountId, Self::Balance)> {
381 use ExistenceReason::*;
382 match Account::<T, I>::get(&id, &who).ok_or(Error::<T, I>::NoDeposit).ok()?.reason {
383 DepositHeld(b) => Some((who, b)),
384 DepositFrom(d, b) => Some((d, b)),
385 _ => None,
386 }
387 }
388 fn refund(id: Self::AssetId, who: T::AccountId) -> DispatchResult {
389 match Self::deposit_held(id.clone(), who.clone()) {
390 Some((d, _)) if d == who => Self::do_refund(id, who, false),
391 Some(..) => Self::do_refund_other(id, &who, None),
392 None => Err(Error::<T, I>::NoDeposit.into()),
393 }
394 }
395}