referrerpolicy=no-referrer-when-downgrade

frame_support/traits/tokens/imbalance/
split_two_ways.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Means for splitting an imbalance into two and handling them differently.
19
20use super::super::imbalance::{Imbalance, OnUnbalanced};
21use core::{marker::PhantomData, ops::Div};
22use sp_runtime::traits::Saturating;
23
24/// Split an unbalanced amount two ways between a common divisor.
25pub struct SplitTwoWays<Balance, Imbalance, Target1, Target2, const PART1: u32, const PART2: u32>(
26	PhantomData<(Balance, Imbalance, Target1, Target2)>,
27);
28
29impl<
30		Balance: From<u32> + Saturating + Div<Output = Balance>,
31		I: Imbalance<Balance>,
32		Target1: OnUnbalanced<I>,
33		Target2: OnUnbalanced<I>,
34		const PART1: u32,
35		const PART2: u32,
36	> OnUnbalanced<I> for SplitTwoWays<Balance, I, Target1, Target2, PART1, PART2>
37{
38	fn on_nonzero_unbalanced(amount: I) {
39		let total: u32 = PART1 + PART2;
40		let amount1 = amount.peek().saturating_mul(PART1.into()) / total.into();
41		let (imb1, imb2) = amount.split(amount1);
42		Target1::on_unbalanced(imb1);
43		Target2::on_unbalanced(imb2);
44	}
45}