1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2024 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge 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 (see http://www.gnu.org/licenses).
// Centrifuge 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.

//! OpenGov Tracks parametrization
//!
//! Based on [Centrifuge Chain parametrization](https://docs.google.com/document/d/1aPH1sTfAttD3K0f4AoOF2iDb3nH1GHlx5TfIP4Xi4SM)
//! whereas `DAYS` and `HOURS` were replaced with `MINUTES` for Development
//! runtime.

use cfg_primitives::{
	Balance, BlockNumber, CFG, MINUTES, TRACK_INDEX_POOL_ADMIN, TRACK_INDEX_REF_CANCELLER,
	TRACK_INDEX_REF_KILLER, TRACK_INDEX_ROOT, TRACK_INDEX_TREASURER,
	TRACK_INDEX_WHITELISTED_CALLER,
};
use cfg_utils::math::{to_percent, to_ppm};
use pallet_referenda::Curve;

use crate::RuntimeOrigin;

const SUP_ROOT: Curve =
	Curve::make_reciprocal(1, 14, to_percent(10), to_ppm(8800), to_percent(100));
const APP_ROOT: Curve = Curve::make_linear(14, 14, to_percent(50), to_percent(100));

const SUP_WHITELISTED: Curve =
	Curve::make_reciprocal(1, 7, to_ppm(800), to_ppm(100), to_percent(50));
const APP_WHITELISTED: Curve = Curve::make_linear(7, 7, to_percent(50), to_percent(100));

const SUP_POOL_ADMIN: Curve =
	Curve::make_reciprocal(1, 7, to_ppm(38500), to_ppm(5900), to_percent(50));
const APP_POOL_ADMIN: Curve = Curve::make_linear(7, 7, to_percent(70), to_percent(100));

const SUP_TREASURER: Curve = Curve::make_linear(25, 28, to_percent(1), to_percent(50));
const APP_TREASURER: Curve = Curve::make_linear(14, 14, to_percent(70), to_percent(100));

const SUP_REF_CANCELLER: Curve =
	Curve::make_reciprocal(1, 7, to_ppm(38500), to_ppm(5900), to_percent(50));
const APP_REF_CANCELLER: Curve = Curve::make_linear(7, 7, to_percent(70), to_percent(100));

const SUP_REF_KILLER: Curve =
	Curve::make_reciprocal(1, 7, to_ppm(38500), to_ppm(5900), to_percent(50));
const APP_REF_KILLER: Curve = Curve::make_linear(7, 7, to_percent(70), to_percent(100));

const TRACKS_DATA: [(u16, pallet_referenda::TrackInfo<Balance, BlockNumber>); 6] = [
	(
		TRACK_INDEX_ROOT,
		pallet_referenda::TrackInfo {
			name: "root",
			max_deciding: 2,
			decision_deposit: 300_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_ROOT,
			min_support: SUP_ROOT,
		},
	),
	(
		TRACK_INDEX_WHITELISTED_CALLER,
		pallet_referenda::TrackInfo {
			name: "whitelisted_caller",
			max_deciding: 20,
			decision_deposit: 1_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_WHITELISTED,
			min_support: SUP_WHITELISTED,
		},
	),
	(
		TRACK_INDEX_POOL_ADMIN,
		pallet_referenda::TrackInfo {
			name: "pool_admin",
			max_deciding: 5,
			decision_deposit: 1_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_POOL_ADMIN,
			min_support: SUP_POOL_ADMIN,
		},
	),
	(
		TRACK_INDEX_TREASURER,
		pallet_referenda::TrackInfo {
			name: "treasurer",
			max_deciding: 2,
			decision_deposit: 10_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_TREASURER,
			min_support: SUP_TREASURER,
		},
	),
	(
		TRACK_INDEX_REF_CANCELLER,
		pallet_referenda::TrackInfo {
			name: "referendum_canceller",
			max_deciding: 20,
			decision_deposit: 50_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_REF_CANCELLER,
			min_support: SUP_REF_CANCELLER,
		},
	),
	(
		TRACK_INDEX_REF_KILLER,
		pallet_referenda::TrackInfo {
			name: "referendum_killer",
			max_deciding: 20,
			decision_deposit: 75_000 * CFG,
			prepare_period: 1 * MINUTES,
			decision_period: 1 * MINUTES,
			confirm_period: 1 * MINUTES,
			min_enactment_period: 1 * MINUTES,
			min_approval: APP_REF_KILLER,
			min_support: SUP_REF_KILLER,
		},
	),
];

pub struct TracksInfo;
impl pallet_referenda::TracksInfo<Balance, BlockNumber> for TracksInfo {
	type Id = u16;
	type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;

	fn tracks() -> &'static [(Self::Id, pallet_referenda::TrackInfo<Balance, BlockNumber>)] {
		&TRACKS_DATA[..]
	}

	fn track_for(id: &Self::RuntimeOrigin) -> Result<Self::Id, ()> {
		if let Ok(system_origin) = frame_system::RawOrigin::try_from(id.clone()) {
			match system_origin {
				frame_system::RawOrigin::Root => Ok(TRACK_INDEX_ROOT),
				_ => Err(()),
			}
		} else if let Ok(custom_origin) = runtime_common::origins::gov::Origin::try_from(id.clone())
		{
			match custom_origin {
				runtime_common::origins::gov::Origin::WhitelistedCaller => {
					Ok(TRACK_INDEX_WHITELISTED_CALLER)
				}
				// General admin
				runtime_common::origins::gov::Origin::PoolAdmin => Ok(TRACK_INDEX_POOL_ADMIN),
				runtime_common::origins::gov::Origin::Treasurer => Ok(TRACK_INDEX_TREASURER),
				// Referendum admins
				runtime_common::origins::gov::Origin::ReferendumCanceller => {
					Ok(TRACK_INDEX_REF_CANCELLER)
				}
				runtime_common::origins::gov::Origin::ReferendumKiller => {
					Ok(TRACK_INDEX_REF_KILLER)
				}
			}
		} else {
			Err(())
		}
	}
}
pallet_referenda::impl_tracksinfo_get!(TracksInfo, Balance, BlockNumber);