Governance: Capture vote_threshold_percentage on voted on proposal (#2152)

* feat: capture vote_threshold_percentage on voted on proposal

* chore: fix test
This commit is contained in:
Sebastian Bor 2021-07-27 16:07:01 +01:00 committed by GitHub
parent 4b9139981c
commit 1eaef02a21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

View File

@ -13,7 +13,10 @@ use solana_program::{
use crate::{ use crate::{
error::GovernanceError, error::GovernanceError,
state::{ state::{
enums::{GovernanceAccountType, InstructionExecutionFlags, ProposalState}, enums::{
GovernanceAccountType, InstructionExecutionFlags, ProposalState,
VoteThresholdPercentage,
},
governance::get_governance_data_for_realm, governance::get_governance_data_for_realm,
proposal::{get_proposal_address_seeds, Proposal}, proposal::{get_proposal_address_seeds, Proposal},
realm::get_realm_data_for_governing_token_mint, realm::get_realm_data_for_governing_token_mint,
@ -97,6 +100,7 @@ pub fn process_create_proposal(
yes_votes_count: 0, yes_votes_count: 0,
no_votes_count: 0, no_votes_count: 0,
governing_token_mint_vote_supply: None, governing_token_mint_vote_supply: None,
vote_threshold_percentage: VoteThresholdPercentage::None,
}; };
create_and_serialize_account_signed::<Proposal>( create_and_serialize_account_signed::<Proposal>(

View File

@ -102,6 +102,9 @@ impl Default for ProposalState {
#[repr(C)] #[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)] #[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum VoteThresholdPercentage { pub enum VoteThresholdPercentage {
/// Vote threshold not set
None,
/// Voting threshold of Yes votes in % required to tip the vote /// Voting threshold of Yes votes in % required to tip the vote
/// It's the percentage of tokens out of the entire pool of governance tokens eligible to vote /// It's the percentage of tokens out of the entire pool of governance tokens eligible to vote
/// Note: If the threshold is below or equal to 50% then an even split of votes ex: 50:50 or 40:40 is always resolved as Defeated /// Note: If the threshold is below or equal to 50% then an even split of votes ex: 50:50 or 40:40 is always resolved as Defeated

View File

@ -93,6 +93,11 @@ pub struct Proposal {
/// after vote was completed. /// after vote was completed.
pub governing_token_mint_vote_supply: Option<u64>, pub governing_token_mint_vote_supply: Option<u64>,
/// The vote threshold percentage at the time Proposal was decided
/// It's used to show correct vote results for historical proposals in cases when the threshold
/// was changed for governance config after vote was completed.
pub vote_threshold_percentage: VoteThresholdPercentage,
/// Proposal name /// Proposal name
pub name: String, pub name: String,
@ -102,7 +107,7 @@ pub struct Proposal {
impl AccountMaxSize for Proposal { impl AccountMaxSize for Proposal {
fn get_max_size(&self) -> Option<usize> { fn get_max_size(&self) -> Option<usize> {
Some(self.name.len() + self.description_link.len() + 202) Some(self.name.len() + self.description_link.len() + 204)
} }
} }
@ -209,7 +214,10 @@ impl Proposal {
self.state = self.get_final_vote_state(governing_token_mint_supply, config); self.state = self.get_final_vote_state(governing_token_mint_supply, config);
self.voting_completed_at = Some(current_unix_timestamp); self.voting_completed_at = Some(current_unix_timestamp);
// Capture vote params to correctly display historical results
self.governing_token_mint_vote_supply = Some(governing_token_mint_supply); self.governing_token_mint_vote_supply = Some(governing_token_mint_supply);
self.vote_threshold_percentage = config.vote_threshold_percentage.clone();
Ok(()) Ok(())
} }
@ -248,7 +256,10 @@ impl Proposal {
{ {
self.state = tipped_state; self.state = tipped_state;
self.voting_completed_at = Some(current_unix_timestamp); self.voting_completed_at = Some(current_unix_timestamp);
// Capture vote params to correctly display historical results
self.governing_token_mint_vote_supply = Some(governing_token_mint_supply); self.governing_token_mint_vote_supply = Some(governing_token_mint_supply);
self.vote_threshold_percentage = config.vote_threshold_percentage.clone();
} }
} }
@ -489,6 +500,7 @@ mod test {
instructions_executed_count: 10, instructions_executed_count: 10,
instructions_count: 10, instructions_count: 10,
instructions_next_index: 10, instructions_next_index: 10,
vote_threshold_percentage: VoteThresholdPercentage::YesVote(100),
} }
} }

View File

@ -66,6 +66,13 @@ async fn test_cast_vote() {
); );
assert_eq!(Some(100), proposal_account.governing_token_mint_vote_supply); assert_eq!(Some(100), proposal_account.governing_token_mint_vote_supply);
assert_eq!(
account_governance_cookie
.account
.config
.vote_threshold_percentage,
proposal_account.vote_threshold_percentage
);
let token_owner_record = governance_test let token_owner_record = governance_test
.get_token_owner_record_account(&token_owner_record_cookie.address) .get_token_owner_record_account(&token_owner_record_cookie.address)

View File

@ -89,6 +89,14 @@ async fn test_finalize_vote_to_succeeded() {
); );
assert_eq!(Some(210), proposal_account.governing_token_mint_vote_supply); assert_eq!(Some(210), proposal_account.governing_token_mint_vote_supply);
assert_eq!(
account_governance_cookie
.account
.config
.vote_threshold_percentage,
proposal_account.vote_threshold_percentage
);
} }
#[tokio::test] #[tokio::test]

View File

@ -1133,6 +1133,7 @@ impl GovernanceProgramTest {
execution_flags: InstructionExecutionFlags::None, execution_flags: InstructionExecutionFlags::None,
governing_token_mint_vote_supply: None, governing_token_mint_vote_supply: None,
vote_threshold_percentage: VoteThresholdPercentage::None,
}; };
let proposal_address = get_proposal_address( let proposal_address = get_proposal_address(