difftreelog
feat replace Currency with fungible traits
in: master
4 files changed
pallets/balances-adapter/src/common.rsdiffbeforeafterboth--- 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<T: Config>(PhantomData<T>);
@@ -250,8 +250,7 @@
fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}
fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {
- let balance = <T as Config>::Currency::total_balance(account.as_sub());
- let balance: u128 = balance.into();
+ let balance = <Pallet<T>>::total_balance(&account);
if balance != 0 {
vec![TokenId::default()]
} else {
@@ -302,24 +301,23 @@
1
}
- fn account_balance(&self, account: <T>::CrossAccountId) -> u32 {
- let balance: u128 = <T as Config>::Currency::free_balance(account.as_sub()).into();
+ fn account_balance(&self, account: T::CrossAccountId) -> u32 {
+ let balance = <Pallet<T>>::balance_of(&account);
(balance != 0).into()
}
- fn balance(&self, account: <T>::CrossAccountId, token: TokenId) -> u128 {
+ fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {
if token != TokenId::default() {
return 0;
}
- <T as Config>::Currency::free_balance(account.as_sub()).into()
+ <Pallet<T>>::balance_of(&account)
}
fn total_pieces(&self, token: TokenId) -> Option<u128> {
if token != TokenId::default() {
return None;
}
- let total = <T as Config>::Currency::total_issuance();
- Some(total.into())
+ Some(<Pallet<T>>::total_issuance())
}
fn allowance(
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth--- 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<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
- let balance = <T as Config>::Currency::free_balance(owner.as_sub());
+ let balance = <Pallet<T>>::balance_of(&owner);
Ok(balance.into())
}
@@ -50,8 +49,7 @@
fn total_supply(&self) -> Result<U256> {
self.consume_store_reads(1)?;
- let total = <T as Config>::Currency::total_issuance();
- Ok(total.into())
+ Ok(<Pallet<T>>::total_issuance().into())
}
#[weight(<SelfWeightOf<T>>::transfer_allow_death())]
@@ -98,7 +96,7 @@
fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = owner.into_sub_cross_account::<T>()?;
- let balance = <T as Config>::Currency::free_balance(owner.as_sub());
+ let balance = <Pallet<T>>::balance_of(&owner);
Ok(balance.into())
}
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23extern crate alloc;4use core::ops::Deref;56use frame_support::sp_runtime::DispatchResult;7use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};8pub use pallet::*;910pub mod common;11pub mod erc;1213pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1415/// Handle for native fungible collection16pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);17impl<T: Config> NativeFungibleHandle<T> {18 /// Creates a handle19 pub fn new() -> NativeFungibleHandle<T> {20 Self(SubstrateRecorder::new(u64::MAX))21 }2223 /// Creates a handle24 pub fn new_with_gas_limit(gas_limit: u64) -> NativeFungibleHandle<T> {25 Self(SubstrateRecorder::new(gas_limit))26 }2728 /// Check if the collection is internal29 pub fn check_is_internal(&self) -> DispatchResult {30 Ok(())31 }32}3334impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {35 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {36 &self.037 }38 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {39 self.040 }41}4243impl<T: Config> Deref for NativeFungibleHandle<T> {44 type Target = SubstrateRecorder<T>;4546 fn deref(&self) -> &Self::Target {47 &self.048 }49}50#[frame_support::pallet]51pub mod pallet {52 use super::*;53 use alloc::string::String;54 use frame_support::{55 dispatch::PostDispatchInfo,56 ensure,57 pallet_prelude::{DispatchResultWithPostInfo, Pays},58 traits::{Currency, ExistenceRequirement, Get},59 };60 use pallet_balances::WeightInfo;61 use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon};62 use pallet_structure::Pallet as PalletStructure;63 use sp_core::U256;64 use sp_runtime::DispatchError;65 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping};6667 #[pallet::config]68 pub trait Config:69 frame_system::Config70 + pallet_evm_coder_substrate::Config71 + pallet_common::Config72 + pallet_structure::Config73 {74 /// Currency from `pallet_balances`75 type Currency: frame_support::traits::Currency<76 Self::AccountId,77 Balance = Self::CurrencyBalance,78 >;79 /// Balance type of chain80 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;8182 /// Decimals of balance83 type Decimals: Get<u8>;84 /// Collection name85 type Name: Get<String>;86 /// Collection symbol87 type Symbol: Get<String>;8889 /// Weight information90 type WeightInfo: WeightInfo;91 }92 #[pallet::pallet]93 pub struct Pallet<T>(_);9495 impl<T: Config> Pallet<T> {96 /// 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.98 ///99 /// - `spender`: CrossAccountId who has the allowance rights.100 /// - `from`: The owner of the tokens who sets the allowance.101 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.102 fn check_allowed(103 spender: &T::CrossAccountId,104 from: &T::CrossAccountId,105 nesting_budget: &dyn Budget,106 ) -> Result<u128, DispatchError> {107 if let Some((collection_id, token_id)) =108 T::CrossTokenAddressMapping::address_to_token(from)109 {110 ensure!(111 <PalletStructure<T>>::check_indirectly_owned(112 spender.clone(),113 collection_id,114 token_id,115 None,116 nesting_budget117 )?,118 <CommonError<T>>::ApprovedValueTooLow,119 );120 } else if !spender.conv_eq(from) {121 return Ok(0);122 }123124 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())125 }126127 /// Transfers the specified amount of tokens. Will check that128 /// the transfer is allowed for the token.129 ///130 /// - `collection`: Collection that contains the token.131 /// - `from`: Owner of tokens to transfer.132 /// - `to`: Recepient of transfered tokens.133 /// - `amount`: Amount of tokens to transfer.134 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.135 pub fn transfer(136 _collection: &NativeFungibleHandle<T>,137 from: &T::CrossAccountId,138 to: &T::CrossAccountId,139 amount: u128,140 _nesting_budget: &dyn Budget,141 ) -> DispatchResultWithPostInfo {142 <PalletCommon<T>>::ensure_correct_receiver(to)?;143144 if from != to && amount != 0 {145 <T as Config>::Currency::transfer(146 from.as_sub(),147 to.as_sub(),148 amount149 .try_into()150 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,151 ExistenceRequirement::AllowDeath,152 )?;153 };154155 Ok(PostDispatchInfo {156 actual_weight: Some(<SelfWeightOf<T>>::transfer_allow_death()),157 pays_fee: Pays::Yes,158 })159 }160161 /// Transfer NFT token from one account to another.162 ///163 /// Same as the [`Self::transfer`] but spender doesn't needs to be the owner of the token.164 /// The owner should set allowance for the spender to transfer token.165 ///166 /// - `collection`: Collection that contains the token.167 /// - `spender`: Account that spend the money.168 /// - `from`: Owner of tokens to transfer.169 /// - `to`: Recepient of transfered tokens.170 /// - `amount`: Amount of tokens to transfer.171 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.172 pub fn transfer_from(173 collection: &NativeFungibleHandle<T>,174 spender: &T::CrossAccountId,175 from: &T::CrossAccountId,176 to: &T::CrossAccountId,177 amount: u128,178 nesting_budget: &dyn Budget,179 ) -> DispatchResultWithPostInfo {180 let allowance = Self::check_allowed(spender, from, nesting_budget)?;181 if allowance < amount {182 return Err(<CommonError<T>>::ApprovedValueTooLow.into());183 }184 Self::transfer(collection, from, to, amount, nesting_budget)185 }186 }187}runtime/common/config/pallets/mod.rsdiffbeforeafterboth--- 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 = <Balances as Currency<Self::AccountId>>::Balance;
type Decimals = Decimals;
type Name = Name;