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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use borsh::maybestd::io::Write;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use solana_program::account_info::AccountInfo;
use solana_program::borsh::try_from_slice_unchecked;
use solana_program::program_error::ProgramError;
use solana_program::{program_pack::IsInitialized, pubkey::Pubkey};
use spl_governance_tools::account::{get_account_data, AccountMaxSize};
use crate::error::GovernanceError;
use crate::PROGRAM_AUTHORITY_SEED;
use crate::state::{
enums::GovernanceAccountType,
legacy::{VoteRecordV1, VoteWeightV1},
};
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct VoteChoice {
pub rank: u8,
pub weight_percentage: u8,
}
impl VoteChoice {
pub fn get_choice_weight(&self, voter_weight: u64) -> Result<u64, ProgramError> {
Ok(match self.weight_percentage {
100 => voter_weight,
0 => 0,
_ => return Err(GovernanceError::InvalidVoteChoiceWeightPercentage.into()),
})
}
}
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum Vote {
Approve(Vec<VoteChoice>),
Deny,
Abstain,
Veto,
}
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct VoteRecordV2 {
pub account_type: GovernanceAccountType,
pub proposal: Pubkey,
pub governing_token_owner: Pubkey,
pub is_relinquished: bool,
pub voter_weight: u64,
pub vote: Vote,
pub reserved_v2: [u8; 8],
}
impl AccountMaxSize for VoteRecordV2 {}
impl IsInitialized for VoteRecordV2 {
fn is_initialized(&self) -> bool {
self.account_type == GovernanceAccountType::VoteRecordV2
}
}
impl VoteRecordV2 {
pub fn assert_can_relinquish_vote(&self) -> Result<(), ProgramError> {
if self.is_relinquished {
return Err(GovernanceError::VoteAlreadyRelinquished.into());
}
Ok(())
}
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::VoteRecordV2 {
BorshSerialize::serialize(&self, writer)?
} else if self.account_type == GovernanceAccountType::VoteRecordV1 {
if self.reserved_v2 != [0; 8] {
panic!("Extended data not supported by VoteRecordV1")
}
let vote_weight = match &self.vote {
Vote::Approve(_options) => VoteWeightV1::Yes(self.voter_weight),
Vote::Deny => VoteWeightV1::No(self.voter_weight),
Vote::Abstain | Vote::Veto => {
panic!("Vote type: {:?} not supported by VoteRecordV1", &self.vote)
}
};
let vote_record_data_v1 = VoteRecordV1 {
account_type: self.account_type,
proposal: self.proposal,
governing_token_owner: self.governing_token_owner,
is_relinquished: self.is_relinquished,
vote_weight,
};
BorshSerialize::serialize(&vote_record_data_v1, writer)?;
}
Ok(())
}
}
pub fn get_vote_record_data(
program_id: &Pubkey,
vote_record_info: &AccountInfo,
) -> Result<VoteRecordV2, ProgramError> {
let account_type: GovernanceAccountType =
try_from_slice_unchecked(&vote_record_info.data.borrow())?;
if account_type == GovernanceAccountType::VoteRecordV1 {
let vote_record_data_v1 = get_account_data::<VoteRecordV1>(program_id, vote_record_info)?;
let (vote, voter_weight) = match vote_record_data_v1.vote_weight {
VoteWeightV1::Yes(weight) => (
Vote::Approve(vec![VoteChoice {
rank: 0,
weight_percentage: 100,
}]),
weight,
),
VoteWeightV1::No(weight) => (Vote::Deny, weight),
};
return Ok(VoteRecordV2 {
account_type,
proposal: vote_record_data_v1.proposal,
governing_token_owner: vote_record_data_v1.governing_token_owner,
is_relinquished: vote_record_data_v1.is_relinquished,
voter_weight,
vote,
reserved_v2: [0; 8],
});
}
get_account_data::<VoteRecordV2>(program_id, vote_record_info)
}
pub fn get_vote_record_data_for_proposal_and_token_owner(
program_id: &Pubkey,
vote_record_info: &AccountInfo,
proposal: &Pubkey,
governing_token_owner: &Pubkey,
) -> Result<VoteRecordV2, ProgramError> {
let vote_record_data = get_vote_record_data(program_id, vote_record_info)?;
if vote_record_data.proposal != *proposal {
return Err(GovernanceError::InvalidProposalForVoterRecord.into());
}
if vote_record_data.governing_token_owner != *governing_token_owner {
return Err(GovernanceError::InvalidGoverningTokenOwnerForVoteRecord.into());
}
Ok(vote_record_data)
}
pub fn get_vote_record_address_seeds<'a>(
proposal: &'a Pubkey,
token_owner_record: &'a Pubkey,
) -> [&'a [u8]; 3] {
[
PROGRAM_AUTHORITY_SEED,
proposal.as_ref(),
token_owner_record.as_ref(),
]
}
pub fn get_vote_record_address<'a>(
program_id: &Pubkey,
proposal: &'a Pubkey,
token_owner_record: &'a Pubkey,
) -> Pubkey {
Pubkey::find_program_address(
&get_vote_record_address_seeds(proposal, token_owner_record),
program_id,
)
.0
}
#[cfg(test)]
mod test {
use borsh::BorshSerialize;
use solana_program::clock::Epoch;
use super::*;
#[test]
fn test_vote_record_v1_to_v2_serialisation_roundtrip() {
let vote_record_v1_source = VoteRecordV1 {
account_type: GovernanceAccountType::VoteRecordV1,
proposal: Pubkey::new_unique(),
governing_token_owner: Pubkey::new_unique(),
is_relinquished: true,
vote_weight: VoteWeightV1::Yes(120),
};
let mut account_data = vec![];
vote_record_v1_source.serialize(&mut account_data).unwrap();
let program_id = Pubkey::new_unique();
let info_key = Pubkey::new_unique();
let mut lamports = 10u64;
let account_info = AccountInfo::new(
&info_key,
false,
false,
&mut lamports,
&mut account_data[..],
&program_id,
false,
Epoch::default(),
);
let vote_record_v2 = get_vote_record_data(&program_id, &account_info).unwrap();
vote_record_v2
.serialize(&mut &mut **account_info.data.borrow_mut())
.unwrap();
let vote_record_v1_target =
get_account_data::<VoteRecordV1>(&program_id, &account_info).unwrap();
assert_eq!(vote_record_v1_source, vote_record_v1_target)
}
}