mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 20:57:59 +00:00
Resolve Credit to Account impls of OnUnbalanced trait (#1876)
Implements the `OnUnbalanced` trait to resolve received credits to the specified account. Credits that fail to resolve are dropped. ### Motivation Throughout the codebase, several types implement the trait with the same behavior. While some currently utilize older currency trait, a migration to the new fungible/s is anticipated for all. Examples: [1](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/cumulus/parachains/common/src/impls.rs#L37), [2](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/polkadot/runtime/common/src/impls.rs#L36), [3](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/substrate/bin/node/runtime/src/impls.rs#L40), [4](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/substrate/bin/node/runtime/src/lib.rs#L1969), [5](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/substrate/frame/broker/src/mock.rs#L198), [6](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/substrate/frame/society/src/lib.rs#L2031), [7](https://github.com/paritytech/polkadot-sdk/blob/1b34571c0c423115813034783ddf524aac257bf5/substrate/frame/treasury/src/lib.rs#L1118)
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user