referrerpolicy=no-referrer-when-downgrade

pallet_assets/
impl_fungibles.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Implementations for fungibles trait.
19
20use 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
118/// Simple handler for an imbalance drop which increases the total issuance of the system by the
119/// imbalance amount. Used for leftover debt. Emits event.
120pub 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
131/// Simple handler for an imbalance drop which decreases the total issuance of the system by the
132/// imbalance amount. Used for leftover credit. Emits event.
133pub 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	// TODO: #13196 implement deactivate/reactivate once we have inactive balance tracking.
220}
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)
230	}
231}
232
233impl<T: Config<I>, I: 'static> fungibles::Destroy<T::AccountId> for Pallet<T, I> {
234	fn start_destroy(id: T::AssetId, maybe_check_owner: Option<T::AccountId>) -> DispatchResult {
235		Self::do_start_destroy(id, maybe_check_owner)
236	}
237
238	fn destroy_accounts(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
239		Self::do_destroy_accounts(id, max_items)
240	}
241
242	fn destroy_approvals(id: T::AssetId, max_items: u32) -> Result<u32, DispatchError> {
243		Self::do_destroy_approvals(id, max_items)
244	}
245
246	fn finish_destroy(id: T::AssetId) -> DispatchResult {
247		Self::do_finish_destroy(id)
248	}
249}
250
251impl<T: Config<I>, I: 'static> fungibles::metadata::Inspect<<T as SystemConfig>::AccountId>
252	for Pallet<T, I>
253{
254	fn name(asset: T::AssetId) -> Vec<u8> {
255		Metadata::<T, I>::get(asset).name.to_vec()
256	}
257
258	fn symbol(asset: T::AssetId) -> Vec<u8> {
259		Metadata::<T, I>::get(asset).symbol.to_vec()
260	}
261
262	fn decimals(asset: T::AssetId) -> u8 {
263		Metadata::<T, I>::get(asset).decimals
264	}
265}
266
267impl<T: Config<I>, I: 'static> fungibles::metadata::Mutate<<T as SystemConfig>::AccountId>
268	for Pallet<T, I>
269{
270	fn set(
271		asset: T::AssetId,
272		from: &<T as SystemConfig>::AccountId,
273		name: Vec<u8>,
274		symbol: Vec<u8>,
275		decimals: u8,
276	) -> DispatchResult {
277		Self::do_set_metadata(asset, from, name, symbol, decimals)
278	}
279}
280
281impl<T: Config<I>, I: 'static>
282	fungibles::metadata::MetadataDeposit<
283		<T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance,
284	> for Pallet<T, I>
285{
286	fn calc_metadata_deposit(
287		name: &[u8],
288		symbol: &[u8],
289	) -> <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance {
290		Self::calc_metadata_deposit(&name, &symbol)
291	}
292}
293
294impl<T: Config<I>, I: 'static> fungibles::approvals::Inspect<<T as SystemConfig>::AccountId>
295	for Pallet<T, I>
296{
297	// Check the amount approved to be spent by an owner to a delegate
298	fn allowance(
299		asset: T::AssetId,
300		owner: &<T as SystemConfig>::AccountId,
301		delegate: &<T as SystemConfig>::AccountId,
302	) -> T::Balance {
303		Approvals::<T, I>::get((asset, &owner, &delegate))
304			.map(|x| x.amount)
305			.unwrap_or_else(Zero::zero)
306	}
307}
308
309impl<T: Config<I>, I: 'static> fungibles::approvals::Mutate<<T as SystemConfig>::AccountId>
310	for Pallet<T, I>
311{
312	// Approve spending tokens from a given account
313	fn approve(
314		asset: T::AssetId,
315		owner: &<T as SystemConfig>::AccountId,
316		delegate: &<T as SystemConfig>::AccountId,
317		amount: T::Balance,
318	) -> DispatchResult {
319		Self::do_approve_transfer(asset, owner, delegate, amount)
320	}
321
322	fn transfer_from(
323		asset: T::AssetId,
324		owner: &<T as SystemConfig>::AccountId,
325		delegate: &<T as SystemConfig>::AccountId,
326		dest: &<T as SystemConfig>::AccountId,
327		amount: T::Balance,
328	) -> DispatchResult {
329		Self::do_transfer_approved(asset, owner, delegate, dest, amount)
330	}
331}
332
333impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::AccountId>
334	for Pallet<T, I>
335{
336	fn owner(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
337		Asset::<T, I>::get(asset).map(|x| x.owner)
338	}
339
340	fn issuer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
341		Asset::<T, I>::get(asset).map(|x| x.issuer)
342	}
343
344	fn admin(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
345		Asset::<T, I>::get(asset).map(|x| x.admin)
346	}
347
348	fn freezer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
349		Asset::<T, I>::get(asset).map(|x| x.freezer)
350	}
351}
352
353impl<T: Config<I>, I: 'static> fungibles::InspectEnumerable<T::AccountId> for Pallet<T, I> {
354	type AssetsIterator = KeyPrefixIterator<<T as Config<I>>::AssetId>;
355
356	/// Returns an iterator of the assets in existence.
357	///
358	/// NOTE: iterating this list invokes a storage read per item.
359	fn asset_ids() -> Self::AssetsIterator {
360		Asset::<T, I>::iter_keys()
361	}
362}
363
364impl<T: Config<I>, I: 'static> fungibles::roles::ResetTeam<T::AccountId> for Pallet<T, I> {
365	fn reset_team(
366		id: T::AssetId,
367		owner: T::AccountId,
368		admin: T::AccountId,
369		issuer: T::AccountId,
370		freezer: T::AccountId,
371	) -> DispatchResult {
372		Self::do_reset_team(id, owner, admin, issuer, freezer)
373	}
374}
375
376impl<T: Config<I>, I: 'static> fungibles::Refund<T::AccountId> for Pallet<T, I> {
377	type AssetId = T::AssetId;
378	type Balance = DepositBalanceOf<T, I>;
379	fn deposit_held(id: Self::AssetId, who: T::AccountId) -> Option<(T::AccountId, Self::Balance)> {
380		use ExistenceReason::*;
381		match Account::<T, I>::get(&id, &who).ok_or(Error::<T, I>::NoDeposit).ok()?.reason {
382			DepositHeld(b) => Some((who, b)),
383			DepositFrom(d, b) => Some((d, b)),
384			_ => None,
385		}
386	}
387	fn refund(id: Self::AssetId, who: T::AccountId) -> DispatchResult {
388		match Self::deposit_held(id.clone(), who.clone()) {
389			Some((d, _)) if d == who => Self::do_refund(id, who, false),
390			Some(..) => Self::do_refund_other(id, &who, None),
391			None => Err(Error::<T, I>::NoDeposit.into()),
392		}
393	}
394}