Skip to content

Commit

Permalink
Resolve Credit to Account impls of OnUnbalanced trait (paritytech#1876
Browse files Browse the repository at this point in the history
  • Loading branch information
muharem authored Oct 23, 2023
1 parent ca7f91f commit efc3044
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion substrate/frame/support/src/traits/tokens/imbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_std::ops::Div;
mod on_unbalanced;
mod signed_imbalance;
mod split_two_ways;
pub use on_unbalanced::OnUnbalanced;
pub use on_unbalanced::{OnUnbalanced, ResolveAssetTo, ResolveTo};
pub use signed_imbalance::SignedImbalance;
pub use split_two_ways::SplitTwoWays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

//! Trait for handling imbalances.

use crate::traits::misc::TryDrop;
use frame_support::traits::{fungible, fungibles, misc::TryDrop};
use sp_core::TypedGet;
use sp_std::marker::PhantomData;

/// Handler for when some currency "account" decreased in balance for
/// some reason.
Expand Down Expand Up @@ -53,3 +55,29 @@ pub trait OnUnbalanced<Imbalance: TryDrop> {
}

impl<Imbalance: TryDrop> OnUnbalanced<Imbalance> for () {}

/// Resolves received asset credit to account `A`, implementing [`OnUnbalanced`].
///
/// Credits that cannot be resolved to account `A` are dropped. This may occur if the account for
/// address `A` does not exist and the existential deposit requirement is not met.
pub struct ResolveTo<A, F>(PhantomData<(A, F)>);
impl<A: TypedGet, F: fungible::Balanced<A::Type>> OnUnbalanced<fungible::Credit<A::Type, F>>
for ResolveTo<A, F>
{
fn on_nonzero_unbalanced(credit: fungible::Credit<A::Type, F>) {
let _ = F::resolve(&A::get(), credit).map_err(|c| drop(c));
}
}

/// Resolves received asset credit to account `A`, implementing [`OnUnbalanced`].
///
/// Credits that cannot be resolved to account `A` are dropped. This may occur if the account for
/// address `A` does not exist and the existential deposit requirement is not met.
pub struct ResolveAssetTo<A, F>(PhantomData<(A, F)>);
impl<A: TypedGet, F: fungibles::Balanced<A::Type>> OnUnbalanced<fungibles::Credit<A::Type, F>>
for ResolveAssetTo<A, F>
{
fn on_nonzero_unbalanced(credit: fungibles::Credit<A::Type, F>) {
let _ = F::resolve(&A::get(), credit).map_err(|c| drop(c));
}
}

0 comments on commit efc3044

Please sign in to comment.