Governance: Use separate thresholds to create proposals for council and community tokens (#2124)
* feat: use separate thresholds for council and community tokens to crate proposals * chore: add tests for proposal creation * chore: cleanup code and bump version * chore: fix test name Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
This commit is contained in:
parent
95738e0a49
commit
d17679a161
|
@ -3730,7 +3730,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spl-governance"
|
name = "spl-governance"
|
||||||
version = "1.0.3"
|
version = "1.0.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayref",
|
"arrayref",
|
||||||
"assert_matches",
|
"assert_matches",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "spl-governance"
|
name = "spl-governance"
|
||||||
version = "1.0.3"
|
version = "1.0.4"
|
||||||
description = "Solana Program Library Governance Program"
|
description = "Solana Program Library Governance Program"
|
||||||
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||||
repository = "https://github.com/solana-labs/solana-program-library"
|
repository = "https://github.com/solana-labs/solana-program-library"
|
||||||
|
|
|
@ -279,6 +279,10 @@ pub enum GovernanceError {
|
||||||
/// Instruction already flagged with error
|
/// Instruction already flagged with error
|
||||||
#[error("Instruction already flagged with error")]
|
#[error("Instruction already flagged with error")]
|
||||||
InstructionAlreadyFlaggedWithError,
|
InstructionAlreadyFlaggedWithError,
|
||||||
|
|
||||||
|
/// Invalid Realm for Governance
|
||||||
|
#[error("Invalid Realm for Governance")]
|
||||||
|
InvalidRealmForGovernance,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrintProgramError for GovernanceError {
|
impl PrintProgramError for GovernanceError {
|
||||||
|
|
|
@ -137,14 +137,15 @@ pub enum GovernanceInstruction {
|
||||||
|
|
||||||
/// Creates Proposal account for Instructions that will be executed at some point in the future
|
/// Creates Proposal account for Instructions that will be executed at some point in the future
|
||||||
///
|
///
|
||||||
/// 0. `[writable]` Proposal account. PDA seeds ['governance',governance, governing_token_mint, proposal_index]
|
/// 0. `[]` Realm account the created Proposal belongs to
|
||||||
/// 1. `[writable]` Governance account
|
/// 1. `[writable]` Proposal account. PDA seeds ['governance',governance, governing_token_mint, proposal_index]
|
||||||
/// 2. `[]` TokenOwnerRecord account for Proposal owner
|
/// 2. `[writable]` Governance account
|
||||||
/// 3. `[signer]` Governance Authority (Token Owner or Governance Delegate)
|
/// 3. `[]` TokenOwnerRecord account for Proposal owner
|
||||||
/// 4. `[signer]` Payer
|
/// 4. `[signer]` Governance Authority (Token Owner or Governance Delegate)
|
||||||
/// 5. `[]` System program
|
/// 5. `[signer]` Payer
|
||||||
/// 6. `[]` Rent sysvar
|
/// 6. `[]` System program
|
||||||
/// 7. `[]` Clock sysvar
|
/// 7. `[]` Rent sysvar
|
||||||
|
/// 8. `[]` Clock sysvar
|
||||||
CreateProposal {
|
CreateProposal {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
/// UTF-8 encoded name of the proposal
|
/// UTF-8 encoded name of the proposal
|
||||||
|
@ -669,7 +670,7 @@ pub fn create_proposal(
|
||||||
program_id: &Pubkey,
|
program_id: &Pubkey,
|
||||||
// Accounts
|
// Accounts
|
||||||
governance: &Pubkey,
|
governance: &Pubkey,
|
||||||
governing_token_owner: &Pubkey,
|
governing_token_owner_record: &Pubkey,
|
||||||
governance_authority: &Pubkey,
|
governance_authority: &Pubkey,
|
||||||
payer: &Pubkey,
|
payer: &Pubkey,
|
||||||
// Args
|
// Args
|
||||||
|
@ -685,17 +686,12 @@ pub fn create_proposal(
|
||||||
governing_token_mint,
|
governing_token_mint,
|
||||||
&proposal_index.to_le_bytes(),
|
&proposal_index.to_le_bytes(),
|
||||||
);
|
);
|
||||||
let token_owner_record_address = get_token_owner_record_address(
|
|
||||||
program_id,
|
|
||||||
realm,
|
|
||||||
governing_token_mint,
|
|
||||||
governing_token_owner,
|
|
||||||
);
|
|
||||||
|
|
||||||
let accounts = vec![
|
let accounts = vec![
|
||||||
|
AccountMeta::new_readonly(*realm, false),
|
||||||
AccountMeta::new(proposal_address, false),
|
AccountMeta::new(proposal_address, false),
|
||||||
AccountMeta::new(*governance, false),
|
AccountMeta::new(*governance, false),
|
||||||
AccountMeta::new_readonly(token_owner_record_address, false),
|
AccountMeta::new_readonly(*governing_token_owner_record, false),
|
||||||
AccountMeta::new_readonly(*governance_authority, true),
|
AccountMeta::new_readonly(*governance_authority, true),
|
||||||
AccountMeta::new_readonly(*payer, true),
|
AccountMeta::new_readonly(*payer, true),
|
||||||
AccountMeta::new_readonly(system_program::id(), false),
|
AccountMeta::new_readonly(system_program::id(), false),
|
||||||
|
|
|
@ -14,9 +14,10 @@ use crate::{
|
||||||
error::GovernanceError,
|
error::GovernanceError,
|
||||||
state::{
|
state::{
|
||||||
enums::{GovernanceAccountType, InstructionExecutionFlags, ProposalState},
|
enums::{GovernanceAccountType, InstructionExecutionFlags, ProposalState},
|
||||||
governance::get_governance_data,
|
governance::get_governance_data_for_realm,
|
||||||
proposal::{get_proposal_address_seeds, Proposal},
|
proposal::{get_proposal_address_seeds, Proposal},
|
||||||
token_owner_record::get_token_owner_record_data_for_realm_and_governing_mint,
|
realm::get_realm_data_for_governing_token_mint,
|
||||||
|
token_owner_record::get_token_owner_record_data_for_realm,
|
||||||
},
|
},
|
||||||
tools::account::create_and_serialize_account_signed,
|
tools::account::create_and_serialize_account_signed,
|
||||||
};
|
};
|
||||||
|
@ -31,42 +32,40 @@ pub fn process_create_proposal(
|
||||||
) -> ProgramResult {
|
) -> ProgramResult {
|
||||||
let account_info_iter = &mut accounts.iter();
|
let account_info_iter = &mut accounts.iter();
|
||||||
|
|
||||||
let proposal_info = next_account_info(account_info_iter)?; // 0
|
let realm_info = next_account_info(account_info_iter)?; // 0
|
||||||
let governance_info = next_account_info(account_info_iter)?; // 1
|
let proposal_info = next_account_info(account_info_iter)?; // 1
|
||||||
|
let governance_info = next_account_info(account_info_iter)?; // 2
|
||||||
|
|
||||||
let token_owner_record_info = next_account_info(account_info_iter)?; // 2
|
let token_owner_record_info = next_account_info(account_info_iter)?; // 3
|
||||||
let governance_authority_info = next_account_info(account_info_iter)?; // 3
|
let governance_authority_info = next_account_info(account_info_iter)?; // 4
|
||||||
|
|
||||||
let payer_info = next_account_info(account_info_iter)?; // 4
|
let payer_info = next_account_info(account_info_iter)?; // 5
|
||||||
let system_info = next_account_info(account_info_iter)?; // 5
|
let system_info = next_account_info(account_info_iter)?; // 6
|
||||||
|
|
||||||
let rent_sysvar_info = next_account_info(account_info_iter)?; // 6
|
let rent_sysvar_info = next_account_info(account_info_iter)?; // 7
|
||||||
let rent = &Rent::from_account_info(rent_sysvar_info)?;
|
let rent = &Rent::from_account_info(rent_sysvar_info)?;
|
||||||
|
|
||||||
let clock_info = next_account_info(account_info_iter)?; // 7
|
let clock_info = next_account_info(account_info_iter)?; // 8
|
||||||
let clock = Clock::from_account_info(clock_info)?;
|
let clock = Clock::from_account_info(clock_info)?;
|
||||||
|
|
||||||
if !proposal_info.data_is_empty() {
|
if !proposal_info.data_is_empty() {
|
||||||
return Err(GovernanceError::ProposalAlreadyExists.into());
|
return Err(GovernanceError::ProposalAlreadyExists.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut governance_data = get_governance_data(program_id, governance_info)?;
|
let realm_data =
|
||||||
|
get_realm_data_for_governing_token_mint(program_id, realm_info, &governing_token_mint)?;
|
||||||
|
|
||||||
let token_owner_record_data = get_token_owner_record_data_for_realm_and_governing_mint(
|
let mut governance_data =
|
||||||
program_id,
|
get_governance_data_for_realm(program_id, governance_info, realm_info.key)?;
|
||||||
token_owner_record_info,
|
|
||||||
&governance_data.realm,
|
|
||||||
&governing_token_mint,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// proposal_owner must be either governing token owner or governance_delegate and must sign this transaction
|
let token_owner_record_data =
|
||||||
|
get_token_owner_record_data_for_realm(program_id, token_owner_record_info, realm_info.key)?;
|
||||||
|
|
||||||
|
// Proposal owner (TokenOwner) or its governance_delegate must sign this transaction
|
||||||
token_owner_record_data.assert_token_owner_or_delegate_is_signer(governance_authority_info)?;
|
token_owner_record_data.assert_token_owner_or_delegate_is_signer(governance_authority_info)?;
|
||||||
|
|
||||||
if token_owner_record_data.governing_token_deposit_amount
|
// Ensure proposal owner (TokenOwner) has enough tokens to create proposal
|
||||||
< governance_data.config.min_tokens_to_create_proposal as u64
|
token_owner_record_data.assert_can_create_proposal(&realm_data, &governance_data.config)?;
|
||||||
{
|
|
||||||
return Err(GovernanceError::NotEnoughTokensToCreateProposal.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
let proposal_data = Proposal {
|
let proposal_data = Proposal {
|
||||||
account_type: GovernanceAccountType::Proposal,
|
account_type: GovernanceAccountType::Proposal,
|
||||||
|
|
|
@ -22,8 +22,8 @@ pub struct GovernanceConfig {
|
||||||
/// Note: In the current version only YesVote threshold is supported
|
/// Note: In the current version only YesVote threshold is supported
|
||||||
pub vote_threshold_percentage: VoteThresholdPercentage,
|
pub vote_threshold_percentage: VoteThresholdPercentage,
|
||||||
|
|
||||||
/// Minimum number of tokens a governance token owner must possess to be able to create a proposal
|
/// Minimum number of community tokens a governance token owner must possess to be able to create a proposal
|
||||||
pub min_tokens_to_create_proposal: u64,
|
pub min_community_tokens_to_create_proposal: u64,
|
||||||
|
|
||||||
/// Minimum waiting time in seconds for an instruction to be executed after proposal is voted on
|
/// Minimum waiting time in seconds for an instruction to be executed after proposal is voted on
|
||||||
pub min_instruction_hold_up_time: u32,
|
pub min_instruction_hold_up_time: u32,
|
||||||
|
@ -39,6 +39,9 @@ pub struct GovernanceConfig {
|
||||||
/// Once cool off time expires Proposal can't be cancelled any longer and becomes a law
|
/// Once cool off time expires Proposal can't be cancelled any longer and becomes a law
|
||||||
/// Note: This field is not implemented in the current version
|
/// Note: This field is not implemented in the current version
|
||||||
pub proposal_cool_off_time: u32,
|
pub proposal_cool_off_time: u32,
|
||||||
|
|
||||||
|
/// Minimum number of council tokens a governance token owner must possess to be able to create a proposal
|
||||||
|
pub min_council_tokens_to_create_proposal: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Governance Account
|
/// Governance Account
|
||||||
|
@ -57,11 +60,11 @@ pub struct Governance {
|
||||||
/// Governance config
|
/// Governance config
|
||||||
pub config: GovernanceConfig,
|
pub config: GovernanceConfig,
|
||||||
|
|
||||||
/// Reserved space for future versions
|
|
||||||
pub reserved: [u8; 8],
|
|
||||||
|
|
||||||
/// Running count of proposals
|
/// Running count of proposals
|
||||||
pub proposals_count: u32,
|
pub proposals_count: u32,
|
||||||
|
|
||||||
|
/// Reserved space for future versions
|
||||||
|
pub reserved: [u8; 8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AccountMaxSize for Governance {}
|
impl AccountMaxSize for Governance {}
|
||||||
|
@ -98,7 +101,7 @@ impl Governance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes account and checks owner program
|
/// Deserializes Governance account and checks owner program
|
||||||
pub fn get_governance_data(
|
pub fn get_governance_data(
|
||||||
program_id: &Pubkey,
|
program_id: &Pubkey,
|
||||||
governance_info: &AccountInfo,
|
governance_info: &AccountInfo,
|
||||||
|
@ -106,6 +109,21 @@ pub fn get_governance_data(
|
||||||
get_account_data::<Governance>(governance_info, program_id)
|
get_account_data::<Governance>(governance_info, program_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deserializes Governance account, checks owner program and asserts governance belongs to the given ream
|
||||||
|
pub fn get_governance_data_for_realm(
|
||||||
|
program_id: &Pubkey,
|
||||||
|
governance_info: &AccountInfo,
|
||||||
|
realm: &Pubkey,
|
||||||
|
) -> Result<Governance, ProgramError> {
|
||||||
|
let governance_data = get_governance_data(program_id, governance_info)?;
|
||||||
|
|
||||||
|
if governance_data.realm != *realm {
|
||||||
|
return Err(GovernanceError::InvalidRealmForGovernance.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(governance_data)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns ProgramGovernance PDA seeds
|
/// Returns ProgramGovernance PDA seeds
|
||||||
pub fn get_program_governance_address_seeds<'a>(
|
pub fn get_program_governance_address_seeds<'a>(
|
||||||
realm: &'a Pubkey,
|
realm: &'a Pubkey,
|
||||||
|
|
|
@ -484,7 +484,8 @@ mod test {
|
||||||
|
|
||||||
fn create_test_governance_config() -> GovernanceConfig {
|
fn create_test_governance_config() -> GovernanceConfig {
|
||||||
GovernanceConfig {
|
GovernanceConfig {
|
||||||
min_tokens_to_create_proposal: 5,
|
min_community_tokens_to_create_proposal: 5,
|
||||||
|
min_council_tokens_to_create_proposal: 1,
|
||||||
min_instruction_hold_up_time: 10,
|
min_instruction_hold_up_time: 10,
|
||||||
max_voting_time: 5,
|
max_voting_time: 5,
|
||||||
vote_threshold_percentage: VoteThresholdPercentage::YesVote(60),
|
vote_threshold_percentage: VoteThresholdPercentage::YesVote(60),
|
||||||
|
|
|
@ -77,6 +77,19 @@ pub fn get_realm_data(
|
||||||
get_account_data::<Realm>(realm_info, program_id)
|
get_account_data::<Realm>(realm_info, program_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deserializes Ream account and asserts the given governing_token_mint is either Community or Council mint of the Realm
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns Realm PDA seeds
|
/// Returns Realm PDA seeds
|
||||||
pub fn get_realm_address_seeds(name: &str) -> [&[u8]; 2] {
|
pub fn get_realm_address_seeds(name: &str) -> [&[u8]; 2] {
|
||||||
[PROGRAM_AUTHORITY_SEED, name.as_bytes()]
|
[PROGRAM_AUTHORITY_SEED, name.as_bytes()]
|
||||||
|
|
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::GovernanceError,
|
error::GovernanceError,
|
||||||
|
state::{enums::GovernanceAccountType, governance::GovernanceConfig, realm::Realm},
|
||||||
tools::account::{get_account_data, AccountMaxSize},
|
tools::account::{get_account_data, AccountMaxSize},
|
||||||
PROGRAM_AUTHORITY_SEED,
|
PROGRAM_AUTHORITY_SEED,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::state::enums::GovernanceAccountType;
|
|
||||||
|
|
||||||
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
|
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
|
||||||
use solana_program::{
|
use solana_program::{
|
||||||
account_info::AccountInfo, program_error::ProgramError, program_pack::IsInitialized,
|
account_info::AccountInfo, program_error::ProgramError, program_pack::IsInitialized,
|
||||||
|
@ -84,6 +83,28 @@ impl TokenOwnerRecord {
|
||||||
|
|
||||||
Err(GovernanceError::GoverningTokenOwnerOrDelegateMustSign.into())
|
Err(GovernanceError::GoverningTokenOwnerOrDelegateMustSign.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asserts TokenOwner has enough tokens to be allowed to create proposal
|
||||||
|
pub fn assert_can_create_proposal(
|
||||||
|
&self,
|
||||||
|
realm_data: &Realm,
|
||||||
|
config: &GovernanceConfig,
|
||||||
|
) -> Result<(), ProgramError> {
|
||||||
|
let min_tokens_to_create_proposal =
|
||||||
|
if self.governing_token_mint == realm_data.community_mint {
|
||||||
|
config.min_community_tokens_to_create_proposal
|
||||||
|
} else if Some(self.governing_token_mint) == realm_data.council_mint {
|
||||||
|
config.min_council_tokens_to_create_proposal
|
||||||
|
} else {
|
||||||
|
return Err(GovernanceError::InvalidGoverningTokenMint.into());
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.governing_token_deposit_amount < min_tokens_to_create_proposal {
|
||||||
|
return Err(GovernanceError::NotEnoughTokensToCreateProposal.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns TokenOwnerRecord PDA address
|
/// Returns TokenOwnerRecord PDA address
|
||||||
|
@ -138,23 +159,35 @@ pub fn get_token_owner_record_data_for_seeds(
|
||||||
get_token_owner_record_data(program_id, token_owner_record_info)
|
get_token_owner_record_data(program_id, token_owner_record_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes TokenOwnerRecord account and checks that its PDA matches the given realm and governing mint
|
/// Deserializes TokenOwnerRecord account and asserts it belongs to the given realm
|
||||||
|
pub fn get_token_owner_record_data_for_realm(
|
||||||
|
program_id: &Pubkey,
|
||||||
|
token_owner_record_info: &AccountInfo,
|
||||||
|
realm: &Pubkey,
|
||||||
|
) -> Result<TokenOwnerRecord, ProgramError> {
|
||||||
|
let token_owner_record_data = get_token_owner_record_data(program_id, token_owner_record_info)?;
|
||||||
|
|
||||||
|
if token_owner_record_data.realm != *realm {
|
||||||
|
return Err(GovernanceError::InvalidRealmForTokenOwnerRecord.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(token_owner_record_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserializes TokenOwnerRecord account and asserts it belongs to the given realm and is for the given governing mint
|
||||||
pub fn get_token_owner_record_data_for_realm_and_governing_mint(
|
pub fn get_token_owner_record_data_for_realm_and_governing_mint(
|
||||||
program_id: &Pubkey,
|
program_id: &Pubkey,
|
||||||
token_owner_record_info: &AccountInfo,
|
token_owner_record_info: &AccountInfo,
|
||||||
realm: &Pubkey,
|
realm: &Pubkey,
|
||||||
governing_token_mint: &Pubkey,
|
governing_token_mint: &Pubkey,
|
||||||
) -> Result<TokenOwnerRecord, ProgramError> {
|
) -> Result<TokenOwnerRecord, ProgramError> {
|
||||||
let token_owner_record_data = get_token_owner_record_data(program_id, token_owner_record_info)?;
|
let token_owner_record_data =
|
||||||
|
get_token_owner_record_data_for_realm(program_id, token_owner_record_info, realm)?;
|
||||||
|
|
||||||
if token_owner_record_data.governing_token_mint != *governing_token_mint {
|
if token_owner_record_data.governing_token_mint != *governing_token_mint {
|
||||||
return Err(GovernanceError::InvalidGoverningMintForTokenOwnerRecord.into());
|
return Err(GovernanceError::InvalidGoverningMintForTokenOwnerRecord.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
if token_owner_record_data.realm != *realm {
|
|
||||||
return Err(GovernanceError::InvalidRealmForTokenOwnerRecord.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(token_owner_record_data)
|
Ok(token_owner_record_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#![cfg(feature = "test-bpf")]
|
#![cfg(feature = "test-bpf")]
|
||||||
|
|
||||||
use solana_program::instruction::AccountMeta;
|
use solana_program::{instruction::AccountMeta, pubkey::Pubkey};
|
||||||
use solana_program_test::*;
|
use solana_program_test::*;
|
||||||
|
|
||||||
mod program_test;
|
mod program_test;
|
||||||
|
@ -10,7 +10,7 @@ use solana_sdk::signature::Keypair;
|
||||||
use spl_governance::error::GovernanceError;
|
use spl_governance::error::GovernanceError;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_community_proposal_created() {
|
async fn test_create_community_proposal() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let mut governance_test = GovernanceProgramTest::start_new().await;
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ async fn test_community_proposal_created() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_multiple_proposals_created() {
|
async fn test_create_multiple_proposals() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let mut governance_test = GovernanceProgramTest::start_new().await;
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ async fn test_create_proposal_with_governance_delegate_signer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_proposal_with_not_enough_tokens_error() {
|
async fn test_create_proposal_with_not_enough_community_tokens_error() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let mut governance_test = GovernanceProgramTest::start_new().await;
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
@ -190,10 +190,11 @@ async fn test_create_proposal_with_not_enough_tokens_error() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// Set token deposit amount below the required threshold
|
||||||
let token_amount = account_governance_cookie
|
let token_amount = account_governance_cookie
|
||||||
.account
|
.account
|
||||||
.config
|
.config
|
||||||
.min_tokens_to_create_proposal as u64
|
.min_community_tokens_to_create_proposal as u64
|
||||||
- 1;
|
- 1;
|
||||||
|
|
||||||
let token_owner_record_cookie = governance_test
|
let token_owner_record_cookie = governance_test
|
||||||
|
@ -212,7 +213,42 @@ async fn test_create_proposal_with_not_enough_tokens_error() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_proposal_with_invalid_token_owner_record_error() {
|
async fn test_create_proposal_with_not_enough_council_tokens_error() {
|
||||||
|
// Arrange
|
||||||
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
let realm_cookie = governance_test.with_realm().await;
|
||||||
|
let governed_account_cookie = governance_test.with_governed_account().await;
|
||||||
|
|
||||||
|
let mut account_governance_cookie = governance_test
|
||||||
|
.with_account_governance(&realm_cookie, &governed_account_cookie)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Set token deposit amount below the required threshold
|
||||||
|
let token_amount = account_governance_cookie
|
||||||
|
.account
|
||||||
|
.config
|
||||||
|
.min_council_tokens_to_create_proposal as u64
|
||||||
|
- 1;
|
||||||
|
|
||||||
|
let token_owner_record_cookie = governance_test
|
||||||
|
.with_council_token_deposit_amount(&realm_cookie, token_amount)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let err = governance_test
|
||||||
|
.with_proposal(&token_owner_record_cookie, &mut account_governance_cookie)
|
||||||
|
.await
|
||||||
|
.err()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert_eq!(err, GovernanceError::NotEnoughTokensToCreateProposal.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create_proposal_with_owner_or_delegate_must_sign_error() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let mut governance_test = GovernanceProgramTest::start_new().await;
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
@ -239,7 +275,7 @@ async fn test_create_proposal_with_invalid_token_owner_record_error() {
|
||||||
&mut account_governance_cookie,
|
&mut account_governance_cookie,
|
||||||
|i| {
|
|i| {
|
||||||
// Set token_owner_record_address for different (Council) mint
|
// Set token_owner_record_address for different (Council) mint
|
||||||
i.accounts[2] =
|
i.accounts[3] =
|
||||||
AccountMeta::new_readonly(council_token_owner_record_cookie.address, false);
|
AccountMeta::new_readonly(council_token_owner_record_cookie.address, false);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -250,6 +286,137 @@ async fn test_create_proposal_with_invalid_token_owner_record_error() {
|
||||||
// Assert
|
// Assert
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
err,
|
err,
|
||||||
GovernanceError::InvalidGoverningMintForTokenOwnerRecord.into()
|
GovernanceError::GoverningTokenOwnerOrDelegateMustSign.into()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create_proposal_with_invalid_governing_token_mint_error() {
|
||||||
|
// Arrange
|
||||||
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
let realm_cookie = governance_test.with_realm().await;
|
||||||
|
let governed_account_cookie = governance_test.with_governed_account().await;
|
||||||
|
|
||||||
|
let mut account_governance_cookie = governance_test
|
||||||
|
.with_account_governance(&realm_cookie, &governed_account_cookie)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut token_owner_record_cookie = governance_test
|
||||||
|
.with_council_token_deposit(&realm_cookie)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Try to use mint which doesn't belong to the Realm
|
||||||
|
token_owner_record_cookie.account.governing_token_mint = Pubkey::new_unique();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let err = governance_test
|
||||||
|
.with_proposal(&token_owner_record_cookie, &mut account_governance_cookie)
|
||||||
|
.await
|
||||||
|
.err()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert_eq!(err, GovernanceError::InvalidGoverningTokenMint.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create_community_proposal_using_council_tokens() {
|
||||||
|
// Arrange
|
||||||
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
let realm_cookie = governance_test.with_realm().await;
|
||||||
|
let governed_account_cookie = governance_test.with_governed_account().await;
|
||||||
|
|
||||||
|
let mut account_governance_cookie = governance_test
|
||||||
|
.with_account_governance(&realm_cookie, &governed_account_cookie)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut community_token_owner_record_cookie = governance_test
|
||||||
|
.with_community_token_deposit(&realm_cookie)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let council_token_owner_record_cookie = governance_test
|
||||||
|
.with_council_token_deposit(&realm_cookie)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Change the proposal owner to council token owner
|
||||||
|
community_token_owner_record_cookie.address = council_token_owner_record_cookie.address;
|
||||||
|
community_token_owner_record_cookie.token_owner = council_token_owner_record_cookie.token_owner;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let proposal_cookie = governance_test
|
||||||
|
.with_proposal(
|
||||||
|
&community_token_owner_record_cookie,
|
||||||
|
&mut account_governance_cookie,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
let proposal_account = governance_test
|
||||||
|
.get_proposal_account(&proposal_cookie.address)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
realm_cookie.account.community_mint,
|
||||||
|
proposal_account.governing_token_mint
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
council_token_owner_record_cookie.address,
|
||||||
|
proposal_account.token_owner_record
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create_council_proposal_using_community_tokens() {
|
||||||
|
// Arrange
|
||||||
|
let mut governance_test = GovernanceProgramTest::start_new().await;
|
||||||
|
|
||||||
|
let realm_cookie = governance_test.with_realm().await;
|
||||||
|
let governed_account_cookie = governance_test.with_governed_account().await;
|
||||||
|
|
||||||
|
let mut account_governance_cookie = governance_test
|
||||||
|
.with_account_governance(&realm_cookie, &governed_account_cookie)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut council_token_owner_record_cookie = governance_test
|
||||||
|
.with_council_token_deposit(&realm_cookie)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let community_token_owner_record_cookie = governance_test
|
||||||
|
.with_community_token_deposit(&realm_cookie)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Change the proposal owner to community token owner
|
||||||
|
council_token_owner_record_cookie.address = community_token_owner_record_cookie.address;
|
||||||
|
council_token_owner_record_cookie.token_owner = community_token_owner_record_cookie.token_owner;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let proposal_cookie = governance_test
|
||||||
|
.with_proposal(
|
||||||
|
&council_token_owner_record_cookie,
|
||||||
|
&mut account_governance_cookie,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
let proposal_account = governance_test
|
||||||
|
.get_proposal_account(&proposal_cookie.address)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
realm_cookie.account.council_mint.unwrap(),
|
||||||
|
proposal_account.governing_token_mint
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
community_token_owner_record_cookie.address,
|
||||||
|
proposal_account.token_owner_record
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -313,6 +313,21 @@ impl GovernanceProgramTest {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn with_council_token_deposit_amount(
|
||||||
|
&mut self,
|
||||||
|
realm_cookie: &RealmCookie,
|
||||||
|
amount: u64,
|
||||||
|
) -> TokeOwnerRecordCookie {
|
||||||
|
self.with_initial_governing_token_deposit(
|
||||||
|
&realm_cookie.address,
|
||||||
|
&realm_cookie.account.council_mint.unwrap(),
|
||||||
|
&realm_cookie.council_mint_authority.as_ref().unwrap(),
|
||||||
|
amount,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub async fn with_council_token_deposit(
|
pub async fn with_council_token_deposit(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
@ -642,7 +657,8 @@ impl GovernanceProgramTest {
|
||||||
|
|
||||||
pub fn get_default_governance_config(&mut self) -> GovernanceConfig {
|
pub fn get_default_governance_config(&mut self) -> GovernanceConfig {
|
||||||
GovernanceConfig {
|
GovernanceConfig {
|
||||||
min_tokens_to_create_proposal: 5,
|
min_community_tokens_to_create_proposal: 5,
|
||||||
|
min_council_tokens_to_create_proposal: 2,
|
||||||
min_instruction_hold_up_time: 10,
|
min_instruction_hold_up_time: 10,
|
||||||
max_voting_time: 10,
|
max_voting_time: 10,
|
||||||
vote_threshold_percentage: VoteThresholdPercentage::YesVote(60),
|
vote_threshold_percentage: VoteThresholdPercentage::YesVote(60),
|
||||||
|
@ -1021,7 +1037,7 @@ impl GovernanceProgramTest {
|
||||||
let mut create_proposal_instruction = create_proposal(
|
let mut create_proposal_instruction = create_proposal(
|
||||||
&self.program_id,
|
&self.program_id,
|
||||||
&governance_cookie.address,
|
&governance_cookie.address,
|
||||||
&token_owner_record_cookie.token_owner.pubkey(),
|
&token_owner_record_cookie.address,
|
||||||
&governance_authority.pubkey(),
|
&governance_authority.pubkey(),
|
||||||
&self.context.payer.pubkey(),
|
&self.context.payer.pubkey(),
|
||||||
&governance_cookie.account.realm,
|
&governance_cookie.account.realm,
|
||||||
|
|
Loading…
Reference in New Issue