// This file is part of Substrate. // Copyright (C) 2017-2022 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Implementations for fungibles trait. use super::*; impl, I: 'static> fungibles::Inspect<::AccountId> for Pallet { type AssetId = T::AssetId; type Balance = T::Balance; fn total_issuance(asset: Self::AssetId) -> Self::Balance { Asset::::get(asset).map(|x| x.supply).unwrap_or_else(Zero::zero) } fn minimum_balance(asset: Self::AssetId) -> Self::Balance { Asset::::get(asset).map(|x| x.min_balance).unwrap_or_else(Zero::zero) } fn balance(asset: Self::AssetId, who: &::AccountId) -> Self::Balance { Pallet::::balance(asset, who) } fn reducible_balance( asset: Self::AssetId, who: &::AccountId, keep_alive: bool, ) -> Self::Balance { Pallet::::reducible_balance(asset, who, keep_alive).unwrap_or(Zero::zero()) } fn can_deposit( asset: Self::AssetId, who: &::AccountId, amount: Self::Balance, mint: bool, ) -> DepositConsequence { Pallet::::can_increase(asset, who, amount, mint) } fn can_withdraw( asset: Self::AssetId, who: &::AccountId, amount: Self::Balance, ) -> WithdrawConsequence { Pallet::::can_decrease(asset, who, amount, false) } } impl, I: 'static> fungibles::InspectMetadata<::AccountId> for Pallet { /// Return the name of an asset. fn name(asset: &Self::AssetId) -> Vec { Metadata::::get(asset).name.to_vec() } /// Return the symbol of an asset. fn symbol(asset: &Self::AssetId) -> Vec { Metadata::::get(asset).symbol.to_vec() } /// Return the decimals of an asset. fn decimals(asset: &Self::AssetId) -> u8 { Metadata::::get(asset).decimals } } impl, I: 'static> fungibles::Mutate<::AccountId> for Pallet { fn mint_into( asset: Self::AssetId, who: &::AccountId, amount: Self::Balance, ) -> DispatchResult { Self::do_mint(asset, who, amount, None) } fn burn_from( asset: Self::AssetId, who: &::AccountId, amount: Self::Balance, ) -> Result { let f = DebitFlags { keep_alive: false, best_effort: false }; Self::do_burn(asset, who, amount, None, f) } fn slash( asset: Self::AssetId, who: &::AccountId, amount: Self::Balance, ) -> Result { let f = DebitFlags { keep_alive: false, best_effort: true }; Self::do_burn(asset, who, amount, None, f) } } impl, I: 'static> fungibles::Transfer for Pallet { fn transfer( asset: Self::AssetId, source: &T::AccountId, dest: &T::AccountId, amount: T::Balance, keep_alive: bool, ) -> Result { let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false }; Self::do_transfer(asset, source, dest, amount, None, f) } } impl, I: 'static> fungibles::Unbalanced for Pallet { fn set_balance(_: Self::AssetId, _: &T::AccountId, _: Self::Balance) -> DispatchResult { unreachable!("set_balance is not used if other functions are impl'd"); } fn set_total_issuance(id: T::AssetId, amount: Self::Balance) { Asset::::mutate_exists(id, |maybe_asset| { if let Some(ref mut asset) = maybe_asset { asset.supply = amount } }); } fn decrease_balance( asset: T::AssetId, who: &T::AccountId, amount: Self::Balance, ) -> Result { let f = DebitFlags { keep_alive: false, best_effort: false }; Self::decrease_balance(asset, who, amount, f, |_, _| Ok(())) } fn decrease_balance_at_most( asset: T::AssetId, who: &T::AccountId, amount: Self::Balance, ) -> Self::Balance { let f = DebitFlags { keep_alive: false, best_effort: true }; Self::decrease_balance(asset, who, amount, f, |_, _| Ok(())).unwrap_or(Zero::zero()) } fn increase_balance( asset: T::AssetId, who: &T::AccountId, amount: Self::Balance, ) -> Result { Self::increase_balance(asset, who, amount, |_| Ok(()))?; Ok(amount) } fn increase_balance_at_most( asset: T::AssetId, who: &T::AccountId, amount: Self::Balance, ) -> Self::Balance { match Self::increase_balance(asset, who, amount, |_| Ok(())) { Ok(()) => amount, Err(_) => Zero::zero(), } } } impl, I: 'static> fungibles::Create for Pallet { fn create( id: T::AssetId, admin: T::AccountId, is_sufficient: bool, min_balance: Self::Balance, ) -> DispatchResult { Self::do_force_create(id, admin, is_sufficient, min_balance) } } impl, I: 'static> fungibles::Destroy for Pallet { type DestroyWitness = DestroyWitness; fn get_destroy_witness(asset: &T::AssetId) -> Option { Asset::::get(asset).map(|asset_details| asset_details.destroy_witness()) } fn destroy( id: T::AssetId, witness: Self::DestroyWitness, maybe_check_owner: Option, ) -> Result { Self::do_destroy(id, witness, maybe_check_owner) } } impl, I: 'static> fungibles::metadata::Inspect<::AccountId> for Pallet { fn name(asset: T::AssetId) -> Vec { Metadata::::get(asset).name.to_vec() } fn symbol(asset: T::AssetId) -> Vec { Metadata::::get(asset).symbol.to_vec() } fn decimals(asset: T::AssetId) -> u8 { Metadata::::get(asset).decimals } } impl, I: 'static> fungibles::metadata::Mutate<::AccountId> for Pallet { fn set( asset: T::AssetId, from: &::AccountId, name: Vec, symbol: Vec, decimals: u8, ) -> DispatchResult { Self::do_set_metadata(asset, from, name, symbol, decimals) } } impl, I: 'static> fungibles::approvals::Inspect<::AccountId> for Pallet { // Check the amount approved to be spent by an owner to a delegate fn allowance( asset: T::AssetId, owner: &::AccountId, delegate: &::AccountId, ) -> T::Balance { Approvals::::get((asset, &owner, &delegate)) .map(|x| x.amount) .unwrap_or_else(Zero::zero) } } impl, I: 'static> fungibles::approvals::Mutate<::AccountId> for Pallet { fn approve( asset: T::AssetId, owner: &::AccountId, delegate: &::AccountId, amount: T::Balance, ) -> DispatchResult { Self::do_approve_transfer(asset, owner, delegate, amount) } // Aprove spending tokens from a given account fn transfer_from( asset: T::AssetId, owner: &::AccountId, delegate: &::AccountId, dest: &::AccountId, amount: T::Balance, ) -> DispatchResult { Self::do_transfer_approved(asset, owner, delegate, dest, amount) } } impl, I: 'static> fungibles::roles::Inspect<::AccountId> for Pallet { fn owner(asset: T::AssetId) -> Option<::AccountId> { Asset::::get(asset).map(|x| x.owner) } fn issuer(asset: T::AssetId) -> Option<::AccountId> { Asset::::get(asset).map(|x| x.issuer) } fn admin(asset: T::AssetId) -> Option<::AccountId> { Asset::::get(asset).map(|x| x.admin) } fn freezer(asset: T::AssetId) -> Option<::AccountId> { Asset::::get(asset).map(|x| x.freezer) } }