Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Credit to Account impls of OnUnbalanced trait #1876

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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));
}
}