Add FixedPointOperand blanket implementation (#14634)

* bound `Balance` to `FixedPointOperand`

* ".git/.scripts/commands/fmt/fmt.sh"

* clean up code

* Apply suggestions from code review

Co-authored-by: Gavin Wood <gavin@parity.io>

* wip

* add blanket `FixedPointOperand` impl

* update nis CurrencyBalance

* remove CheckedNeg bound

---------

Co-authored-by: command-bot <>
Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Juan
2023-08-02 09:54:11 +02:00
committed by GitHub
parent 77321288c3
commit 85f9931e4f
11 changed files with 35 additions and 63 deletions
-1
View File
@@ -1141,7 +1141,6 @@ impl pallet_asset_rate::Config for Runtime {
type CreateOrigin = EnsureRoot<AccountId>;
type RemoveOrigin = EnsureRoot<AccountId>;
type UpdateOrigin = EnsureRoot<AccountId>;
type Balance = Balance;
type Currency = Balances;
type AssetKind = u32;
type RuntimeEvent = RuntimeEvent;
+3 -10
View File
@@ -59,11 +59,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::traits::{
fungible::Inspect,
tokens::{Balance, ConversionFromAssetBalance},
};
use sp_runtime::{traits::Zero, FixedPointNumber, FixedPointOperand, FixedU128};
use frame_support::traits::{fungible::Inspect, tokens::ConversionFromAssetBalance};
use sp_runtime::{traits::Zero, FixedPointNumber, FixedU128};
pub use pallet::*;
pub use weights::WeightInfo;
@@ -111,11 +108,8 @@ pub mod pallet {
/// The origin permissioned to update an existiing conversion rate for an asset.
type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// The units in which we record balances.
type Balance: Balance + FixedPointOperand;
/// The currency mechanism for this pallet.
type Currency: Inspect<Self::AccountId, Balance = Self::Balance>;
type Currency: Inspect<Self::AccountId>;
/// The type for asset kinds for which the conversion rate to native balance is set.
type AssetKind: Parameter + MaxEncodedLen;
@@ -230,7 +224,6 @@ pub mod pallet {
impl<T> ConversionFromAssetBalance<BalanceOf<T>, AssetKindOf<T>, BalanceOf<T>> for Pallet<T>
where
T: Config,
BalanceOf<T>: FixedPointOperand + Zero,
{
type Error = pallet::Error<T>;
-1
View File
@@ -84,7 +84,6 @@ impl pallet_asset_rate::Config for Test {
type CreateOrigin = frame_system::EnsureRoot<u64>;
type RemoveOrigin = frame_system::EnsureRoot<u64>;
type UpdateOrigin = frame_system::EnsureRoot<u64>;
type Balance = u64;
type Currency = Balances;
type AssetKind = u32;
#[cfg(feature = "runtime-benchmarks")]
+1 -3
View File
@@ -22,7 +22,7 @@ use frame_support::{
pallet_prelude::*,
traits::{fungible, tokens::ConversionToAssetBalance},
};
use sp_runtime::{traits::Convert, FixedPointNumber, FixedPointOperand, FixedU128};
use sp_runtime::{traits::Convert, FixedPointNumber, FixedU128};
pub(super) type DepositBalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
@@ -293,8 +293,6 @@ where
T: Config<I>,
I: 'static,
CON: Convert<BalanceOf<F, T>, AssetBalanceOf<T, I>>,
BalanceOf<F, T>: FixedPointOperand + Zero,
AssetBalanceOf<T, I>: FixedPointOperand + Zero,
{
type Error = ConversionError;
+4 -11
View File
@@ -165,6 +165,7 @@ pub mod pallet {
fungible::{self, hold::Mutate as FunHoldMutate, Balanced as FunBalanced},
nonfungible::{Inspect as NftInspect, Transfer as NftTransfer},
tokens::{
Balance,
Fortitude::Polite,
Precision::{BestEffort, Exact},
Preservation::Expendable,
@@ -214,17 +215,9 @@ pub mod pallet {
/// Overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;
/// Just the `Currency::Balance` type; we have this item to allow us to constrain it to
/// `From<u64>`.
type CurrencyBalance: sp_runtime::traits::AtLeast32BitUnsigned
+ codec::FullCodec
+ Copy
+ MaybeSerializeDeserialize
+ sp_std::fmt::Debug
+ Default
+ From<u64>
+ TypeInfo
+ MaxEncodedLen;
/// Just the [`Balance`] type; we have this item to allow us to constrain it to
/// [`From<u64>`].
type CurrencyBalance: Balance + From<u64>;
/// Origin required for auto-funding the deficit.
type FundOrigin: EnsureOrigin<Self::RuntimeOrigin>;
@@ -25,9 +25,7 @@ use crate::{
dispatch::{DispatchError, DispatchResult},
traits::Get,
};
use codec::MaxEncodedLen;
use sp_runtime::{traits::MaybeSerializeDeserialize, FixedPointOperand};
use sp_std::fmt::Debug;
use sp_runtime::traits::MaybeSerializeDeserialize;
mod reservable;
pub use reservable::{NamedReservableCurrency, ReservableCurrency};
@@ -37,7 +35,7 @@ pub use lockable::{LockIdentifier, LockableCurrency, VestingSchedule};
/// Abstraction over a fungible assets system.
pub trait Currency<AccountId> {
/// The balance of an account.
type Balance: Balance + MaybeSerializeDeserialize + Debug + MaxEncodedLen + FixedPointOperand;
type Balance: Balance + MaybeSerializeDeserialize;
/// The opaque token type for an imbalance. This is returned by unbalanced operations
/// and must be dealt with. It may be dropped but cannot be cloned.
@@ -60,7 +60,6 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointOperand,
};
#[cfg(test)]
@@ -165,12 +164,8 @@ pub struct ChargeAssetTxPayment<T: Config> {
impl<T: Config> ChargeAssetTxPayment<T>
where
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send
+ Sync
+ FixedPointOperand
+ Into<ChargeAssetBalanceOf<T>>
+ From<ChargeAssetLiquidityOf<T>>,
AssetBalanceOf<T>: Send + Sync,
BalanceOf<T>: Send + Sync + Into<ChargeAssetBalanceOf<T>> + From<ChargeAssetLiquidityOf<T>>,
ChargeAssetIdOf<T>: Send + Sync,
{
/// Utility constructor. Used only in client/factory code.
@@ -234,11 +229,10 @@ impl<T: Config> sp_std::fmt::Debug for ChargeAssetTxPayment<T> {
impl<T: Config> SignedExtension for ChargeAssetTxPayment<T>
where
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
AssetBalanceOf<T>: Send + Sync,
BalanceOf<T>: Send
+ Sync
+ From<u64>
+ FixedPointOperand
+ Into<ChargeAssetBalanceOf<T>>
+ Into<ChargeAssetLiquidityOf<T>>
+ From<ChargeAssetLiquidityOf<T>>,
@@ -56,7 +56,6 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointOperand,
};
#[cfg(test)]
@@ -156,8 +155,8 @@ pub struct ChargeAssetTxPayment<T: Config> {
impl<T: Config> ChargeAssetTxPayment<T>
where
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send + Sync + FixedPointOperand + IsType<ChargeAssetBalanceOf<T>>,
AssetBalanceOf<T>: Send + Sync,
BalanceOf<T>: Send + Sync + IsType<ChargeAssetBalanceOf<T>>,
ChargeAssetIdOf<T>: Send + Sync,
Credit<T::AccountId, T::Fungibles>: IsType<ChargeAssetLiquidityOf<T>>,
{
@@ -213,8 +212,8 @@ impl<T: Config> sp_std::fmt::Debug for ChargeAssetTxPayment<T> {
impl<T: Config> SignedExtension for ChargeAssetTxPayment<T>
where
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand + IsType<ChargeAssetBalanceOf<T>>,
AssetBalanceOf<T>: Send + Sync,
BalanceOf<T>: Send + Sync + From<u64> + IsType<ChargeAssetBalanceOf<T>>,
ChargeAssetIdOf<T>: Send + Sync,
Credit<T::AccountId, T::Fungibles>: IsType<ChargeAssetLiquidityOf<T>>,
{
@@ -67,7 +67,7 @@ use sp_runtime::{
transaction_validity::{
TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointNumber, FixedPointOperand, FixedU128, Perbill, Perquintill, RuntimeDebug,
FixedPointNumber, FixedU128, Perbill, Perquintill, RuntimeDebug,
};
use sp_std::prelude::*;
pub use types::{FeeDetails, InclusionFee, RuntimeDispatchInfo};
@@ -461,10 +461,7 @@ pub mod pallet {
}
}
impl<T: Config> Pallet<T>
where
BalanceOf<T>: FixedPointOperand,
{
impl<T: Config> Pallet<T> {
/// Query the data that we know about the fee of a given `call`.
///
/// This pallet is not and cannot be aware of the internals of a signed extension, for example
@@ -649,7 +646,6 @@ where
impl<T> Convert<Weight, BalanceOf<T>> for Pallet<T>
where
T: Config,
BalanceOf<T>: FixedPointOperand,
{
/// Compute the fee for the specified weight.
///
@@ -678,7 +674,7 @@ pub struct ChargeTransactionPayment<T: Config>(#[codec(compact)] BalanceOf<T>);
impl<T: Config> ChargeTransactionPayment<T>
where
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
BalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send + Sync,
{
/// utility constructor. Used only in client/factory code.
pub fn from(fee: BalanceOf<T>) -> Self {
@@ -800,7 +796,7 @@ impl<T: Config> sp_std::fmt::Debug for ChargeTransactionPayment<T> {
impl<T: Config> SignedExtension for ChargeTransactionPayment<T>
where
BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
BalanceOf<T>: Send + Sync + From<u64>,
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
const IDENTIFIER: &'static str = "ChargeTransactionPayment";
@@ -866,7 +862,6 @@ where
impl<T: Config, AnyCall: GetDispatchInfo + Encode> EstimateCallFee<AnyCall, BalanceOf<T>>
for Pallet<T>
where
BalanceOf<T>: FixedPointOperand,
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
fn estimate_call_fee(call: &AnyCall, post_info: PostDispatchInfo) -> BalanceOf<T> {
@@ -52,16 +52,18 @@ pub trait FixedPointOperand:
{
}
impl FixedPointOperand for i128 {}
impl FixedPointOperand for u128 {}
impl FixedPointOperand for i64 {}
impl FixedPointOperand for u64 {}
impl FixedPointOperand for i32 {}
impl FixedPointOperand for u32 {}
impl FixedPointOperand for i16 {}
impl FixedPointOperand for u16 {}
impl FixedPointOperand for i8 {}
impl FixedPointOperand for u8 {}
impl<T> FixedPointOperand for T where
T: Copy
+ Clone
+ Bounded
+ Zero
+ Saturating
+ PartialOrd
+ UniqueSaturatedInto<u128>
+ TryFrom<u128>
+ CheckedNeg
{
}
/// Something that implements a decimal fixed point number.
///
@@ -59,6 +59,7 @@ pub trait BaseArithmetic:
+ CheckedMul
+ CheckedDiv
+ CheckedRem
+ CheckedNeg
+ Ensure
+ Saturating
+ PartialOrd<Self>
@@ -116,6 +117,7 @@ impl<
+ CheckedMul
+ CheckedDiv
+ CheckedRem
+ CheckedNeg
+ Ensure
+ Saturating
+ PartialOrd<Self>