Skip to content

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

substrate/frame/support/src/traits/tokens/imbalance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use sp_std::ops::Div;
2525
mod on_unbalanced;
2626
mod signed_imbalance;
2727
mod split_two_ways;
28-
pub use on_unbalanced::OnUnbalanced;
28+
pub use on_unbalanced::{OnUnbalanced, ResolveAssetTo, ResolveTo};
2929
pub use signed_imbalance::SignedImbalance;
3030
pub use split_two_ways::SplitTwoWays;
3131

substrate/frame/support/src/traits/tokens/imbalance/on_unbalanced.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
//! Trait for handling imbalances.
1919
20-
use crate::traits::misc::TryDrop;
20+
use frame_support::traits::{fungible, fungibles, misc::TryDrop};
21+
use sp_core::TypedGet;
22+
use sp_std::marker::PhantomData;
2123

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

5557
impl<Imbalance: TryDrop> OnUnbalanced<Imbalance> for () {}
58+
59+
/// Resolves received asset credit to account `A`, implementing [`OnUnbalanced`].
60+
///
61+
/// Credits that cannot be resolved to account `A` are dropped. This may occur if the account for
62+
/// address `A` does not exist and the existential deposit requirement is not met.
63+
pub struct ResolveTo<A, F>(PhantomData<(A, F)>);
64+
impl<A: TypedGet, F: fungible::Balanced<A::Type>> OnUnbalanced<fungible::Credit<A::Type, F>>
65+
for ResolveTo<A, F>
66+
{
67+
fn on_nonzero_unbalanced(credit: fungible::Credit<A::Type, F>) {
68+
let _ = F::resolve(&A::get(), credit).map_err(|c| drop(c));
69+
}
70+
}
71+
72+
/// Resolves received asset credit to account `A`, implementing [`OnUnbalanced`].
73+
///
74+
/// Credits that cannot be resolved to account `A` are dropped. This may occur if the account for
75+
/// address `A` does not exist and the existential deposit requirement is not met.
76+
pub struct ResolveAssetTo<A, F>(PhantomData<(A, F)>);
77+
impl<A: TypedGet, F: fungibles::Balanced<A::Type>> OnUnbalanced<fungibles::Credit<A::Type, F>>
78+
for ResolveAssetTo<A, F>
79+
{
80+
fn on_nonzero_unbalanced(credit: fungibles::Credit<A::Type, F>) {
81+
let _ = F::resolve(&A::get(), credit).map_err(|c| drop(c));
82+
}
83+
}

0 commit comments

Comments
 (0)