Skip to content

Commit

Permalink
modify frame_support trait and pallet-balances impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Sep 6, 2024
1 parent 8d81f1e commit 871cc1a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
6 changes: 6 additions & 0 deletions substrate/frame/balances/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ impl<T: Config<I>, I: 'static> fungible::Balanced<T::AccountId> for Pallet<T, I>

impl<T: Config<I>, I: 'static> fungible::BalancedHold<T::AccountId> for Pallet<T, I> {}

impl<T: Config<I>, I: 'static> fungible::hold::DoneSlash<T::RuntimeHoldReason, T::AccountId, T::Balance> for Pallet<T, I> {
fn done_slash(reason: &T::RuntimeHoldReason, who: &T::AccountId, amount: T::Balance) {
T::DoneSlashHandler::done_slash(reason, who, amount);
}
}

impl<T: Config<I>, I: 'static> AccountTouch<(), T::AccountId> for Pallet<T, I> {
type Balance = T::Balance;
fn deposit_required(_: ()) -> Self::Balance {
Expand Down
3 changes: 3 additions & 0 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub mod pallet {
type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;

type WeightInfo = ();
type DoneSlashHandler = ();
}
}

Expand Down Expand Up @@ -312,6 +313,8 @@ pub mod pallet {
/// The maximum number of individual freeze locks that can exist on an account at any time.
#[pallet::constant]
type MaxFreezes: Get<u32>;

type DoneSlashHandler: fungible::hold::DoneSlash<Self::RuntimeHoldReason, Self::AccountId, Self::Balance>;
}

/// The in-code storage version.
Expand Down
14 changes: 12 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungible/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub trait Mutate<AccountId>:
}

/// Trait for slashing a fungible asset which can be place on hold.
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> {
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> + DoneSlash<Self::Reason, AccountId, Self::Balance> {
/// Reduce the balance of some funds on hold in an account.
///
/// The resulting imbalance is the first item of the tuple returned.
Expand All @@ -449,6 +449,16 @@ pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId
Self::done_slash(reason, who, decrease);
(credit, amount.saturating_sub(decrease))
}
}

fn done_slash(_reason: &Self::Reason, _who: &AccountId, _amount: Self::Balance) {}
pub trait DoneSlash<Reason, AccountId, Balance> {
fn done_slash(_reason: &Reason, _who: &AccountId, _amount: Balance) {}
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
impl<Reason, AccountId, Balance: Copy> DoneSlash<Reason, AccountId, Balance> for Tuple {
fn done_slash(reason: &Reason, who: &AccountId, amount: Balance) {
for_tuples!( #( Tuple::done_slash(reason, who, amount); )* );
}
}

12 changes: 12 additions & 0 deletions substrate/frame/support/src/traits/tokens/fungible/item_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//!
//! See the [`crate::traits::fungible`] doc for more information about fungible traits.

use frame_support::traits::fungible::hold::DoneSlash;
use super::*;
use crate::traits::{
fungible::imbalance,
Expand Down Expand Up @@ -467,5 +468,16 @@ impl<
}
}

impl<
F: fungibles::BalancedHold<AccountId>,
A: Get<<F as fungibles::Inspect<AccountId>>::AssetId>,
AccountId,
> DoneSlash<F::Reason, AccountId, F::Balance> for ItemOf<F, A, AccountId>
{
fn done_slash(reason: &F::Reason, who: &AccountId, amount: F::Balance) {
<F as fungibles::hold::DoneSlash<F::AssetId, F::Reason, AccountId, F::Balance>>::done_slash(A::get(), reason, who, amount)
}
}

#[test]
fn test() {}
24 changes: 22 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungible/union_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ impl<
}

impl<
Left: fungible::BalancedHold<AccountId>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason>,
Left: fungible::BalancedHold<AccountId> + fungible::hold::DoneSlash<Self::Reason, AccountId, Self::Balance>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason> + fungibles::hold::DoneSlash<AssetKind, Left::Reason, AccountId, Left::Balance>,
Criterion: Convert<AssetKind, Either<(), Right::AssetId>>,
AssetKind: AssetId,
AccountId,
Expand All @@ -871,6 +871,26 @@ impl<
}
}
}
impl<
Reason,
Balance,
Left: fungible::hold::DoneSlash<Reason, AccountId, Balance>,
Right: fungibles::hold::DoneSlash<Right::AssetId, Reason, AccountId, Balance> + fungibles::Inspect<AccountId>,
Criterion: Convert<AssetKind, Either<(), Right::AssetId>>,
AssetKind: AssetId,
AccountId,
> fungibles::hold::DoneSlash<AssetKind, Reason, AccountId, Balance> for UnionOf<Left, Right, Criterion, AssetKind, AccountId> {
fn done_slash(asset: AssetKind, reason: &Reason, who: &AccountId, amount: Balance) {
match Criterion::convert(asset.clone()) {
Left(()) => {
Left::done_slash(reason, who, amount);
},
Right(a) => {
Right::done_slash(a, reason, who, amount);
},
}
}
}

impl<
Left: fungible::Inspect<AccountId>,
Expand Down
17 changes: 10 additions & 7 deletions substrate/frame/support/src/traits/tokens/fungibles/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
}

/// Trait for slashing a fungible asset which can be place on hold.
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> {
pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId> + DoneSlash<Self::AssetId, Self::Reason, AccountId, Self::Balance> {
/// Reduce the balance of some funds on hold in an account.
///
/// The resulting imbalance is the first item of the tuple returned.
Expand All @@ -238,13 +238,16 @@ pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId
Self::done_slash(asset, reason, who, decrease);
(credit, amount.saturating_sub(decrease))
}
}

fn done_slash(
_asset: Self::AssetId,
_reason: &Self::Reason,
_who: &AccountId,
_amount: Self::Balance,
) {
pub trait DoneSlash<AssetId, Reason, AccountId, Balance> {
fn done_slash(_asset: AssetId, _reason: &Reason, _who: &AccountId, _amount: Balance) {}
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
impl<AssetId: Copy, Reason, AccountId, Balance: Copy> DoneSlash<AssetId, Reason, AccountId, Balance> for Tuple {
fn done_slash(asset_id: AssetId, reason: &Reason, who: &AccountId, amount: Balance) {
for_tuples!( #( Tuple::done_slash(asset_id, reason, who, amount); )* );
}
}

Expand Down
25 changes: 23 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungibles/union_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ impl<
}

impl<
Left: fungibles::BalancedHold<AccountId>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason>,
Left: fungibles::BalancedHold<AccountId> + fungibles::hold::DoneSlash<Self::AssetId, Self::Reason, AccountId, Self::Balance>,
Right: fungibles::BalancedHold<AccountId, Balance = Left::Balance, Reason = Left::Reason> + fungibles::hold::DoneSlash<Self::AssetId, Self::Reason, AccountId, Self::Balance>,
Criterion: Convert<AssetKind, Either<Left::AssetId, Right::AssetId>>,
AssetKind: AssetId,
AccountId,
Expand All @@ -853,6 +853,27 @@ impl<
}
}

impl<
Reason,
Balance,
Left: fungibles::Inspect<AccountId> + fungibles::hold::DoneSlash<Left::AssetId, Reason, AccountId, Balance>,
Right: fungibles::Inspect<AccountId> + fungibles::hold::DoneSlash<Right::AssetId, Reason, AccountId, Balance>,
Criterion: Convert<AssetKind, Either<Left::AssetId, Right::AssetId>>,
AssetKind: AssetId,
AccountId,
> fungibles::hold::DoneSlash<AssetKind, Reason, AccountId, Balance> for UnionOf<Left, Right, Criterion, AssetKind, AccountId> {
fn done_slash(asset: AssetKind, reason: &Reason, who: &AccountId, amount: Balance) {
match Criterion::convert(asset.clone()) {
Left(a) => {
Left::done_slash(a, reason, who, amount);
},
Right(a) => {
Right::done_slash(a, reason, who, amount);
},
}
}
}

impl<
Left: fungibles::Inspect<AccountId> + fungibles::Create<AccountId>,
Right: fungibles::Inspect<AccountId, Balance = Left::Balance> + fungibles::Create<AccountId>,
Expand Down

0 comments on commit 871cc1a

Please sign in to comment.