From 11719c8f16e9b3304ae655b6c5be3cac0002e988 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Wed, 24 May 2023 08:47:39 +0000 Subject: [PATCH] feat: replace Currency with fungible traits --- --- a/pallets/balances-adapter/src/common.rs +++ b/pallets/balances-adapter/src/common.rs @@ -1,9 +1,9 @@ use alloc::{vec, vec::Vec}; use core::marker::PhantomData; use crate::{Config, NativeFungibleHandle, Pallet}; -use frame_support::{fail, traits::Currency, weights::Weight}; +use frame_support::{fail, weights::Weight}; use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo}; -use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo}; +use pallet_common::{CommonCollectionOperations, CommonWeightInfo}; use up_data_structs::TokenId; pub struct CommonWeights(PhantomData); @@ -250,8 +250,7 @@ fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {} fn account_tokens(&self, account: ::CrossAccountId) -> Vec { - let balance = ::Currency::total_balance(account.as_sub()); - let balance: u128 = balance.into(); + let balance = >::total_balance(&account); if balance != 0 { vec![TokenId::default()] } else { @@ -302,24 +301,23 @@ 1 } - fn account_balance(&self, account: ::CrossAccountId) -> u32 { - let balance: u128 = ::Currency::free_balance(account.as_sub()).into(); + fn account_balance(&self, account: T::CrossAccountId) -> u32 { + let balance = >::balance_of(&account); (balance != 0).into() } - fn balance(&self, account: ::CrossAccountId, token: TokenId) -> u128 { + fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 { if token != TokenId::default() { return 0; } - ::Currency::free_balance(account.as_sub()).into() + >::balance_of(&account) } fn total_pieces(&self, token: TokenId) -> Option { if token != TokenId::default() { return None; } - let total = ::Currency::total_issuance(); - Some(total.into()) + Some(>::total_issuance()) } fn allowance( --- a/pallets/balances-adapter/src/erc.rs +++ b/pallets/balances-adapter/src/erc.rs @@ -1,6 +1,5 @@ use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf}; use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*}; -use frame_support::traits::{Currency}; use pallet_balances::WeightInfo; use pallet_common::{ erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult}, @@ -32,7 +31,7 @@ fn balance_of(&self, owner: Address) -> Result { self.consume_store_reads(1)?; let owner = T::CrossAccountId::from_eth(owner); - let balance = ::Currency::free_balance(owner.as_sub()); + let balance = >::balance_of(&owner); Ok(balance.into()) } @@ -50,8 +49,7 @@ fn total_supply(&self) -> Result { self.consume_store_reads(1)?; - let total = ::Currency::total_issuance(); - Ok(total.into()) + Ok(>::total_issuance().into()) } #[weight(>::transfer_allow_death())] @@ -98,7 +96,7 @@ fn balance_of_cross(&self, owner: CrossAddress) -> Result { self.consume_store_reads(1)?; let owner = owner.into_sub_cross_account::()?; - let balance = ::Currency::free_balance(owner.as_sub()); + let balance = >::balance_of(&owner); Ok(balance.into()) } --- a/pallets/balances-adapter/src/lib.rs +++ b/pallets/balances-adapter/src/lib.rs @@ -55,7 +55,11 @@ dispatch::PostDispatchInfo, ensure, pallet_prelude::{DispatchResultWithPostInfo, Pays}, - traits::{Currency, ExistenceRequirement, Get}, + traits::{ + Get, + fungible::{Inspect, Mutate}, + tokens::Preservation, + }, }; use pallet_balances::WeightInfo; use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon}; @@ -71,11 +75,18 @@ + pallet_common::Config + pallet_structure::Config { - /// Currency from `pallet_balances` - type Currency: frame_support::traits::Currency< + /// Inspect from `pallet_balances` + type Inspect: frame_support::traits::tokens::fungible::Inspect< + Self::AccountId, + Balance = Self::CurrencyBalance, + >; + + /// Mutate from `pallet_balances` + type Mutate: frame_support::traits::tokens::fungible::Mutate< Self::AccountId, Balance = Self::CurrencyBalance, >; + /// Balance type of chain type CurrencyBalance: Into + TryFrom + TryFrom + Into; @@ -93,6 +104,18 @@ pub struct Pallet(_); impl Pallet { + pub fn balance_of(account: &T::CrossAccountId) -> u128 { + T::Inspect::balance(account.as_sub()).into() + } + + pub fn total_balance(account: &T::CrossAccountId) -> u128 { + T::Inspect::total_balance(account.as_sub()).into() + } + + pub fn total_issuance() -> u128 { + T::Inspect::total_issuance().into() + } + /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens. /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds. /// @@ -121,7 +144,7 @@ return Ok(0); } - Ok(::Currency::free_balance(from.as_sub()).into()) + Ok(Self::balance_of(from)) } /// Transfers the specified amount of tokens. Will check that @@ -142,14 +165,10 @@ >::ensure_correct_receiver(to)?; if from != to && amount != 0 { - ::Currency::transfer( - from.as_sub(), - to.as_sub(), - amount - .try_into() - .map_err(|_| sp_runtime::ArithmeticError::Overflow)?, - ExistenceRequirement::AllowDeath, - )?; + let amount = amount + .try_into() + .map_err(|_| sp_runtime::ArithmeticError::Overflow)?; + T::Mutate::transfer(from.as_sub(), to.as_sub(), amount, Preservation::Expendable)?; }; Ok(PostDispatchInfo { --- a/runtime/common/config/pallets/mod.rs +++ b/runtime/common/config/pallets/mod.rs @@ -92,7 +92,8 @@ pub Symbol: String = TOKEN_SYMBOL.to_string(); } impl pallet_balances_adapter::Config for Runtime { - type Currency = Balances; + type Inspect = Balances; + type Mutate = Balances; type CurrencyBalance = >::Balance; type Decimals = Decimals; type Name = Name; -- gitstuff