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