impls.rs 2.48 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Auxillary struct/enums for polkadot runtime.

19
20
21
use sp_runtime::traits::Convert;
use frame_support::traits::{OnUnbalanced, Imbalance, Currency};
use crate::NegativeImbalance;
22
23

/// Logic for the author to get a portion of fees.
24
pub struct ToAuthor<R>(sp_std::marker::PhantomData<R>);
25
26
27

impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
where
28
29
30
31
32
33
34
	R: pallet_balances::Trait + pallet_authorship::Trait,
	<R as frame_system::Trait>::AccountId: From<primitives::v0::AccountId>,
	<R as frame_system::Trait>::AccountId: Into<primitives::v0::AccountId>,
	<R as frame_system::Trait>::Event: From<pallet_balances::RawEvent<
		<R as frame_system::Trait>::AccountId,
		<R as pallet_balances::Trait>::Balance,
		pallet_balances::DefaultInstance>
35
36
37
	>,
{
	fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
38
		let numeric_amount = amount.peek();
39
40
41
		let author = <pallet_authorship::Module<R>>::author();
		<pallet_balances::Module<R>>::resolve_creating(&<pallet_authorship::Module<R>>::author(), amount);
		<frame_system::Module<R>>::deposit_event(pallet_balances::RawEvent::Deposit(author, numeric_amount));
42
43
44
45
	}
}

/// Converter for currencies to votes.
46
pub struct CurrencyToVoteHandler<R>(sp_std::marker::PhantomData<R>);
47
48
49

impl<R> CurrencyToVoteHandler<R>
where
50
	R: pallet_balances::Trait,
51
52
53
	R::Balance: Into<u128>,
{
	fn factor() -> u128 {
54
		let issuance: u128 = <pallet_balances::Module<R>>::total_issuance().into();
55
56
		(issuance / u64::max_value() as u128).max(1)
	}
57
58
}

59
60
impl<R> Convert<u128, u64> for CurrencyToVoteHandler<R>
where
61
	R: pallet_balances::Trait,
62
63
	R::Balance: Into<u128>,
{
64
65
66
	fn convert(x: u128) -> u64 { (x / Self::factor()) as u64 }
}

67
68
impl<R> Convert<u128, u128> for CurrencyToVoteHandler<R>
where
69
	R: pallet_balances::Trait,
70
71
	R::Balance: Into<u128>,
{
72
73
	fn convert(x: u128) -> u128 { x * Self::factor() }
}