difftreelog
feat replace Currency with fungible traits
in: master
4 files changed
pallets/balances-adapter/src/common.rsdiffbeforeafterboth1use alloc::{vec, vec::Vec};1use alloc::{vec, vec::Vec};2use core::marker::PhantomData;2use core::marker::PhantomData;3use crate::{Config, NativeFungibleHandle, Pallet};3use crate::{Config, NativeFungibleHandle, Pallet};4use frame_support::{fail, traits::Currency, weights::Weight};4use frame_support::{fail, weights::Weight};5use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};5use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};6use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo};6use pallet_common::{CommonCollectionOperations, CommonWeightInfo};7use up_data_structs::TokenId;7use up_data_structs::TokenId;889pub struct CommonWeights<T: Config>(PhantomData<T>);9pub struct CommonWeights<T: Config>(PhantomData<T>);250 fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}250 fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}251251252 fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {252 fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {253 let balance = <T as Config>::Currency::total_balance(account.as_sub());253 let balance = <Pallet<T>>::total_balance(&account);254 let balance: u128 = balance.into();255 if balance != 0 {254 if balance != 0 {256 vec![TokenId::default()]255 vec![TokenId::default()]257 } else {256 } else {302 1301 1303 }302 }304303305 fn account_balance(&self, account: <T>::CrossAccountId) -> u32 {304 fn account_balance(&self, account: T::CrossAccountId) -> u32 {306 let balance: u128 = <T as Config>::Currency::free_balance(account.as_sub()).into();305 let balance = <Pallet<T>>::balance_of(&account);307 (balance != 0).into()306 (balance != 0).into()308 }307 }309308310 fn balance(&self, account: <T>::CrossAccountId, token: TokenId) -> u128 {309 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {311 if token != TokenId::default() {310 if token != TokenId::default() {312 return 0;311 return 0;313 }312 }314 <T as Config>::Currency::free_balance(account.as_sub()).into()313 <Pallet<T>>::balance_of(&account)315 }314 }316315317 fn total_pieces(&self, token: TokenId) -> Option<u128> {316 fn total_pieces(&self, token: TokenId) -> Option<u128> {318 if token != TokenId::default() {317 if token != TokenId::default() {319 return None;318 return None;320 }319 }321 let total = <T as Config>::Currency::total_issuance();320 Some(<Pallet<T>>::total_issuance())322 Some(total.into())323 }321 }324322325 fn allowance(323 fn allowance(pallets/balances-adapter/src/erc.rsdiffbeforeafterboth1use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf};1use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf};2use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};2use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::{Currency};4use pallet_balances::WeightInfo;3use pallet_balances::WeightInfo;5use pallet_common::{4use pallet_common::{6 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},32 fn balance_of(&self, owner: Address) -> Result<U256> {31 fn balance_of(&self, owner: Address) -> Result<U256> {33 self.consume_store_reads(1)?;32 self.consume_store_reads(1)?;34 let owner = T::CrossAccountId::from_eth(owner);33 let owner = T::CrossAccountId::from_eth(owner);35 let balance = <T as Config>::Currency::free_balance(owner.as_sub());34 let balance = <Pallet<T>>::balance_of(&owner);36 Ok(balance.into())35 Ok(balance.into())37 }36 }3837504951 fn total_supply(&self) -> Result<U256> {50 fn total_supply(&self) -> Result<U256> {52 self.consume_store_reads(1)?;51 self.consume_store_reads(1)?;53 let total = <T as Config>::Currency::total_issuance();52 Ok(<Pallet<T>>::total_issuance().into())54 Ok(total.into())55 }53 }565457 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]55 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]98 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {96 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {99 self.consume_store_reads(1)?;97 self.consume_store_reads(1)?;100 let owner = owner.into_sub_cross_account::<T>()?;98 let owner = owner.into_sub_cross_account::<T>()?;101 let balance = <T as Config>::Currency::free_balance(owner.as_sub());99 let balance = <Pallet<T>>::balance_of(&owner);102 Ok(balance.into())100 Ok(balance.into())103 }101 }104102pallets/balances-adapter/src/lib.rsdiffbeforeafterboth55 dispatch::PostDispatchInfo,55 dispatch::PostDispatchInfo,56 ensure,56 ensure,57 pallet_prelude::{DispatchResultWithPostInfo, Pays},57 pallet_prelude::{DispatchResultWithPostInfo, Pays},58 traits::{Currency, ExistenceRequirement, Get},58 traits::{59 Get,60 fungible::{Inspect, Mutate},61 tokens::Preservation,62 },59 };63 };60 use pallet_balances::WeightInfo;64 use pallet_balances::WeightInfo;71 + pallet_common::Config75 + pallet_common::Config72 + pallet_structure::Config76 + pallet_structure::Config73 {77 {74 /// Currency from `pallet_balances`78 /// Inspect from `pallet_balances`75 type Currency: frame_support::traits::Currency<79 type Inspect: frame_support::traits::tokens::fungible::Inspect<76 Self::AccountId,80 Self::AccountId,77 Balance = Self::CurrencyBalance,81 Balance = Self::CurrencyBalance,78 >;82 >;8384 /// Mutate from `pallet_balances`85 type Mutate: frame_support::traits::tokens::fungible::Mutate<86 Self::AccountId,87 Balance = Self::CurrencyBalance,88 >;8979 /// Balance type of chain90 /// Balance type of chain80 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;91 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;93 pub struct Pallet<T>(_);104 pub struct Pallet<T>(_);9410595 impl<T: Config> Pallet<T> {106 impl<T: Config> Pallet<T> {107 pub fn balance_of(account: &T::CrossAccountId) -> u128 {108 T::Inspect::balance(account.as_sub()).into()109 }110111 pub fn total_balance(account: &T::CrossAccountId) -> u128 {112 T::Inspect::total_balance(account.as_sub()).into()113 }114115 pub fn total_issuance() -> u128 {116 T::Inspect::total_issuance().into()117 }11896 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.119 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.97 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.120 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.121 return Ok(0);144 return Ok(0);122 }145 }123146124 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())147 Ok(Self::balance_of(from))125 }148 }126149127 /// Transfers the specified amount of tokens. Will check that150 /// Transfers the specified amount of tokens. Will check that142 <PalletCommon<T>>::ensure_correct_receiver(to)?;165 <PalletCommon<T>>::ensure_correct_receiver(to)?;143166144 if from != to && amount != 0 {167 if from != to && amount != 0 {168 let amount = amount169 .try_into()170 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?;145 <T as Config>::Currency::transfer(171 T::Mutate::transfer(from.as_sub(), to.as_sub(), amount, Preservation::Expendable)?;146 from.as_sub(),147 to.as_sub(),148 amount149 .try_into()150 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,151 ExistenceRequirement::AllowDeath,152 )?;153 };172 };154173runtime/common/config/pallets/mod.rsdiffbeforeafterboth92 pub Symbol: String = TOKEN_SYMBOL.to_string();92 pub Symbol: String = TOKEN_SYMBOL.to_string();93}93}94impl pallet_balances_adapter::Config for Runtime {94impl pallet_balances_adapter::Config for Runtime {95 type Currency = Balances;95 type Inspect = Balances;96 type Mutate = Balances;96 type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;97 type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;97 type Decimals = Decimals;98 type Decimals = Decimals;98 type Name = Name;99 type Name = Name;