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

Commit

Permalink
Add FixedPointOperand blanket implementation (#14634)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
juangirini and gavofyork committed Aug 2, 2023
1 parent b05a9d3 commit 4d9d911
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 63 deletions.
1 change: 0 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 @@ -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;
Expand Down Expand Up @@ -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>;

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 AssetKind = u32;
#[cfg(feature = "runtime-benchmarks")]
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
15 changes: 4 additions & 11 deletions frame/nis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>;
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
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
2 changes: 2 additions & 0 deletions primitives/arithmetic/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait BaseArithmetic:
+ CheckedMul
+ CheckedDiv
+ CheckedRem
+ CheckedNeg
+ Ensure
+ Saturating
+ PartialOrd<Self>
Expand Down Expand Up @@ -116,6 +117,7 @@ impl<
+ CheckedMul
+ CheckedDiv
+ CheckedRem
+ CheckedNeg
+ Ensure
+ Saturating
+ PartialOrd<Self>
Expand Down

0 comments on commit 4d9d911

Please sign in to comment.