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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use solana_program::{
account_info::AccountInfo, program_error::ProgramError, program_pack::IsInitialized,
pubkey::Pubkey,
};
use crate::{
error::GovernanceError,
state::enums::{GovernanceAccountType, MintMaxVoteWeightSource},
tools::account::{assert_is_valid_account, get_account_data, AccountMaxSize},
PROGRAM_AUTHORITY_SEED,
};
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct RealmConfigArgs {
pub use_council_mint: bool,
pub use_custodian: bool,
pub community_mint_max_vote_weight_source: MintMaxVoteWeightSource,
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct RealmConfig {
pub reserved: [u8; 8],
pub community_mint_max_vote_weight_source: MintMaxVoteWeightSource,
pub council_mint: Option<Pubkey>,
pub custodian: Option<Pubkey>,
}
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct Realm {
pub account_type: GovernanceAccountType,
pub community_mint: Pubkey,
pub config: RealmConfig,
pub reserved: [u8; 8],
pub authority: Option<Pubkey>,
pub name: String,
}
impl AccountMaxSize for Realm {
fn get_max_size(&self) -> Option<usize> {
Some(self.name.len() + 161)
}
}
impl IsInitialized for Realm {
fn is_initialized(&self) -> bool {
self.account_type == GovernanceAccountType::Realm
}
}
impl Realm {
pub fn assert_is_valid_governing_token_mint(
&self,
governing_token_mint: &Pubkey,
) -> Result<(), ProgramError> {
if self.community_mint == *governing_token_mint {
return Ok(());
}
if self.config.council_mint == Some(*governing_token_mint) {
return Ok(());
}
Err(GovernanceError::InvalidGoverningTokenMint.into())
}
pub fn assert_is_valid_governing_token_mint_and_holding(
&self,
program_id: &Pubkey,
realm: &Pubkey,
governing_token_mint: &Pubkey,
governing_token_holding: &Pubkey,
) -> Result<(), ProgramError> {
self.assert_is_valid_governing_token_mint(governing_token_mint)?;
let governing_token_holding_address =
get_governing_token_holding_address(program_id, realm, governing_token_mint);
if governing_token_holding_address != *governing_token_holding {
return Err(GovernanceError::InvalidGoverningTokenHoldingAccount.into());
}
Ok(())
}
}
pub fn assert_is_valid_realm(
program_id: &Pubkey,
realm_info: &AccountInfo,
) -> Result<(), ProgramError> {
assert_is_valid_account(realm_info, GovernanceAccountType::Realm, program_id)
}
pub fn get_realm_data(
program_id: &Pubkey,
realm_info: &AccountInfo,
) -> Result<Realm, ProgramError> {
get_account_data::<Realm>(realm_info, program_id)
}
pub fn get_realm_data_for_authority(
program_id: &Pubkey,
realm_info: &AccountInfo,
realm_authority: &Pubkey,
) -> Result<Realm, ProgramError> {
let realm_data = get_account_data::<Realm>(realm_info, program_id)?;
if realm_data.authority.is_none() {
return Err(GovernanceError::RealmHasNoAuthority.into());
}
if realm_data.authority.unwrap() != *realm_authority {
return Err(GovernanceError::InvalidAuthorityForRealm.into());
}
Ok(realm_data)
}
pub fn get_realm_data_for_governing_token_mint(
program_id: &Pubkey,
realm_info: &AccountInfo,
governing_token_mint: &Pubkey,
) -> Result<Realm, ProgramError> {
let realm_data = get_realm_data(program_id, realm_info)?;
realm_data.assert_is_valid_governing_token_mint(governing_token_mint)?;
Ok(realm_data)
}
pub fn get_realm_address_seeds(name: &str) -> [&[u8]; 2] {
[PROGRAM_AUTHORITY_SEED, name.as_bytes()]
}
pub fn get_realm_address(program_id: &Pubkey, name: &str) -> Pubkey {
Pubkey::find_program_address(&get_realm_address_seeds(name), program_id).0
}
pub fn get_governing_token_holding_address_seeds<'a>(
realm: &'a Pubkey,
governing_token_mint: &'a Pubkey,
) -> [&'a [u8]; 3] {
[
PROGRAM_AUTHORITY_SEED,
realm.as_ref(),
governing_token_mint.as_ref(),
]
}
pub fn get_governing_token_holding_address(
program_id: &Pubkey,
realm: &Pubkey,
governing_token_mint: &Pubkey,
) -> Pubkey {
Pubkey::find_program_address(
&get_governing_token_holding_address_seeds(realm, governing_token_mint),
program_id,
)
.0
}
pub fn assert_valid_realm_config_args(config_args: &RealmConfigArgs) -> Result<(), ProgramError> {
match config_args.community_mint_max_vote_weight_source {
MintMaxVoteWeightSource::SupplyFraction(fraction) => {
if !(1..=MintMaxVoteWeightSource::SUPPLY_FRACTION_BASE).contains(&fraction) {
return Err(GovernanceError::InvalidMaxVoteWeightSupplyFraction.into());
}
}
_ => return Err(GovernanceError::MintMaxVoteWeightSourceNotSupported.into()),
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_max_size() {
let realm = Realm {
account_type: GovernanceAccountType::Realm,
community_mint: Pubkey::new_unique(),
reserved: [0; 8],
authority: Some(Pubkey::new_unique()),
name: "test-realm".to_string(),
config: RealmConfig {
council_mint: Some(Pubkey::new_unique()),
reserved: [0; 8],
custodian: Some(Pubkey::new_unique()),
community_mint_max_vote_weight_source: MintMaxVoteWeightSource::Absolute(100),
},
};
let size = realm.try_to_vec().unwrap().len();
assert_eq!(realm.get_max_size(), Some(size));
}
}