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
Changes from 2 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
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_unbalanced(credit: fungible::Credit<A::Type, F>) {
muharem marked this conversation as resolved.
Show resolved Hide resolved
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_unbalanced(credit: fungibles::Credit<A::Type, F>) {
let _ = F::resolve(&A::get(), credit).map_err(|c| drop(c));
}
}
Loading