Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add FixedPointOperand blanket implementation #14634

Merged
merged 8 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,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 AssetId = u32;
type RuntimeEvent = RuntimeEvent;
Expand Down
13 changes: 3 additions & 10 deletions frame/asset-rate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,11 +106,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 identifier for the class of asset.
type AssetId: frame_support::traits::tokens::AssetId;
Expand Down Expand Up @@ -224,7 +218,6 @@ pub mod pallet {
impl<T> ConversionFromAssetBalance<BalanceOf<T>, AssetIdOf<T>, BalanceOf<T>> for Pallet<T>
where
T: Config,
BalanceOf<T>: FixedPointOperand + Zero,
{
type Error = pallet::Error<T>;

Expand Down
1 change: 0 additions & 1 deletion frame/asset-rate/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 AssetId = u32;
}
Expand Down
7 changes: 5 additions & 2 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ pub use types::*;

use scale_info::TypeInfo;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, Saturating, StaticLookup, Zero},
traits::{
AtLeast32BitUnsigned, CheckedAdd, CheckedNeg, CheckedSub, Saturating, StaticLookup, Zero,
},
ArithmeticError, TokenError,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -241,7 +243,8 @@ pub mod pallet {
+ Copy
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ TypeInfo;
+ TypeInfo
+ CheckedNeg;
Copy link
Member

@gavofyork gavofyork Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid adding here - just add it into BaseArithmetic trait/blanket impl along with all the other Checked* bounds. With that in place you should be able to remove all the other CheckedNeg stuff in this PR and simplify a bit.


/// Max number of items to destroy per `destroy_accounts` and `destroy_approvals` call.
///
Expand Down
4 changes: 1 addition & 3 deletions frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions frame/nis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use sp_arithmetic::{PerThing, Perquintill};
use sp_runtime::{
traits::{AccountIdConversion, Bounded, Convert, ConvertBack, Saturating, Zero},
traits::{
AccountIdConversion, Bounded, CheckedNeg, Convert, ConvertBack, Saturating, Zero,
},
Rounding, TokenError,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -224,7 +226,8 @@ pub mod pallet {
+ Default
+ From<u64>
+ TypeInfo
+ MaxEncodedLen;
+ MaxEncodedLen
+ CheckedNeg;
juangirini marked this conversation as resolved.
Show resolved Hide resolved

/// Origin required for auto-funding the deficit.
type FundOrigin: EnsureOrigin<Self::RuntimeOrigin>;
Expand Down
6 changes: 2 additions & 4 deletions frame/support/src/traits/tokens/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions frame/support/src/traits/tokens/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Miscellaneous types.

use codec::{Decode, Encode, FullCodec, MaxEncodedLen};
use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero};
use sp_arithmetic::traits::{AtLeast32BitUnsigned, CheckedNeg, Zero};
use sp_core::RuntimeDebug;
use sp_runtime::{traits::Convert, ArithmeticError, DispatchError, TokenError};
use sp_std::fmt::Debug;
Expand Down Expand Up @@ -231,7 +231,14 @@ impl<T: FullCodec + Clone + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxE

/// Simple amalgamation trait to collect together properties for a Balance under one roof.
pub trait Balance:
AtLeast32BitUnsigned + FullCodec + Copy + Default + Debug + scale_info::TypeInfo + MaxEncodedLen
AtLeast32BitUnsigned
+ FullCodec
+ Copy
+ Default
+ Debug
+ scale_info::TypeInfo
+ MaxEncodedLen
+ CheckedNeg
{
}
impl<
Expand All @@ -241,7 +248,8 @@ impl<
+ Default
+ Debug
+ scale_info::TypeInfo
+ MaxEncodedLen,
+ MaxEncodedLen
+ CheckedNeg,
> Balance for T
{
}
Expand Down
12 changes: 3 additions & 9 deletions frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointOperand,
};

#[cfg(test)]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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>>,
Expand Down
9 changes: 4 additions & 5 deletions frame/transaction-payment/asset-tx-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointOperand,
};

#[cfg(test)]
Expand Down Expand Up @@ -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>>,
{
Expand Down Expand Up @@ -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>>,
{
Expand Down
13 changes: 4 additions & 9 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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> {
Expand Down
22 changes: 12 additions & 10 deletions primitives/arithmetic/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
6 changes: 3 additions & 3 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp_application_crypto::AppCrypto;
pub use sp_arithmetic::traits::{
checked_pow, ensure_pow, AtLeast32Bit, AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedDiv,
CheckedMul, CheckedShl, CheckedShr, CheckedSub, Ensure, EnsureAdd, EnsureAddAssign, EnsureDiv,
EnsureDivAssign, EnsureFixedPointNumber, EnsureFrom, EnsureInto, EnsureMul, EnsureMulAssign,
EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, IntegerSquareRoot, One,
CheckedMul, CheckedNeg, CheckedShl, CheckedShr, CheckedSub, Ensure, EnsureAdd, EnsureAddAssign,
EnsureDiv, EnsureDivAssign, EnsureFixedPointNumber, EnsureFrom, EnsureInto, EnsureMul,
EnsureMulAssign, EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, IntegerSquareRoot, One,
SaturatedConversion, Saturating, UniqueSaturatedFrom, UniqueSaturatedInto, Zero,
};
use sp_core::{self, storage::StateVersion, Hasher, RuntimeDebug, TypeId};
Expand Down