Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b488aee

Browse files
Governance: Create governance accounts
Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
1 parent ed0389f commit b488aee

24 files changed

Lines changed: 1139 additions & 84 deletions

‎governance/program/src/error.rs‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pub enum GovernanceError {
1919
#[error("Realm with the given name and governing mints already exists")]
2020
RealmAlreadyExists,
2121

22+
/// Invalid Realm
23+
#[error("Invalid realm")]
24+
InvalidRealm,
25+
2226
/// Invalid Governing Token Mint
2327
#[error("Invalid Governing Token Mint")]
2428
InvalidGoverningTokenMint,
@@ -39,17 +43,47 @@ pub enum GovernanceError {
3943
#[error("Invalid Voter account address")]
4044
InvalidVoterAccountAddress,
4145

42-
/// ---- Account Tools Errors -----
46+
/// Invalid Governance config
47+
#[error("Invalid Governance config")]
48+
InvalidGovernanceConfig,
49+
50+
/// ---- Account Tools Errors ----
4351
4452
/// Invalid account owner
4553
#[error("Invalid account owner")]
4654
InvalidAccountOwner,
4755

48-
/// ---- Token Tools Errors -----
56+
/// Invalid Account type
57+
#[error("Invalid Account type")]
58+
InvalidAccountType,
59+
60+
/// ---- Token Tools Errors ----
4961
5062
/// Invalid Token account owner
5163
#[error("Invalid Token account owner")]
5264
InvalidTokenAccountOwner,
65+
66+
/// ---- Bpf Upgradable Loader Tools Errors ----
67+
68+
/// Invalid ProgramData account Address
69+
#[error("Invalid ProgramData account address")]
70+
InvalidProgramDataAccountAddress,
71+
72+
/// Invalid ProgramData account data
73+
#[error("Invalid ProgramData account Data")]
74+
InvalidProgramDataAccountData,
75+
76+
/// Provided upgrade authority doesn't match current program upgrade authority
77+
#[error("Provided upgrade authority doesn't match current program upgrade authority")]
78+
InvalidUpgradeAuthority,
79+
80+
/// Current program upgrade authority must sign transaction
81+
#[error("Current program upgrade authority must sign transaction")]
82+
UpgradeAuthorityMustSign,
83+
84+
/// Given program is not upgradable
85+
#[error("Given program is not upgradable")]
86+
ProgramNotUpgradable,
5387
}
5488

5589
impl PrintProgramError for GovernanceError {

‎governance/program/src/instruction.rs‎

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ use crate::{
44
id,
55
state::{
66
enums::GoverningTokenType,
7+
governance::{
8+
get_account_governance_address, get_program_governance_address, GovernanceConfig,
9+
},
710
realm::{get_governing_token_holding_address, get_realm_address},
811
single_signer_instruction::InstructionData,
912
voter_record::get_voter_record_address,
1013
},
14+
tools::bpf_loader_upgradeable::get_program_data_address,
1115
};
1216
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
1317
use solana_program::{
18+
bpf_loader_upgradeable,
1419
instruction::{AccountMeta, Instruction},
1520
pubkey::Pubkey,
1621
system_program, sysvar,
@@ -63,6 +68,7 @@ pub enum GovernanceInstruction {
6368
/// 6. `[signer]` Payer
6469
/// 7. `[]` System
6570
/// 8. `[]` SPL Token
71+
/// 9. `[]` Sysvar Rent
6672
DepositGoverningTokens {},
6773

6874
/// Withdraws governing tokens (Community or Council) from Governance Realm and downgrades your voter weight within the Realm
@@ -101,34 +107,37 @@ pub enum GovernanceInstruction {
101107
new_vote_authority: Option<Pubkey>,
102108
},
103109

104-
/// Creates Program Governance account which governs an upgradable program
110+
/// Creates Account Governance account which can be used to govern an arbitrary account
105111
///
106-
/// 0. `[writable]` Governance account. PDA seeds: ['governance', governed_program]
107-
/// 1. `[]` Account of the Program governed by this Governance account
108-
/// 2. `[writable]` Program Data account of the Program governed by this Governance account
109-
/// 3. `[signer]` Current Upgrade Authority account of the Program governed by this Governance account
110-
/// 4. `[]` Governance Realm the Program Governance belongs to
111-
/// 5. `[signer]` Payer
112-
/// 6. `[]` System account
113-
/// 7. `[]` Bpf_upgrade_loader account
114-
CreateProgramGovernance {
115-
#[allow(dead_code)]
116-
/// Voting threshold in % required to tip the vote
117-
/// It's the percentage of tokens out of the entire pool of governance tokens eligible to vote
118-
vote_threshold: u8,
119-
112+
/// 0. `[writable]` Account Governance account. PDA seeds: ['account-governance', realm, governed_account]
113+
/// 1. `[signer]` Payer
114+
/// 2. `[]` System program
115+
/// 3. `[]` Sysvar Rent
116+
CreateAccountGovernance {
117+
/// Governance config
120118
#[allow(dead_code)]
121-
/// Minimum waiting time in slots for an instruction to be executed after proposal is voted on
122-
min_instruction_hold_up_time: u64,
119+
config: GovernanceConfig,
120+
},
123121

122+
/// Creates Program Governance account which governs an upgradable program
123+
///
124+
/// 0. `[writable]` Program Governance account. PDA seeds: ['program-governance', realm, governed_program]
125+
/// 1. `[writable]` Program Data account of the Program governed by this Governance account
126+
/// 2. `[signer]` Current Upgrade Authority account of the Program governed by this Governance account
127+
/// 3. `[signer]` Payer
128+
/// 4. `[]` bpf_upgradeable_loader program
129+
/// 5. `[]` System program
130+
/// 6. `[]` Sysvar Rent
131+
CreateProgramGovernance {
132+
/// Governance config
124133
#[allow(dead_code)]
125-
/// Time limit in slots for proposal to be open for voting
126-
max_voting_time: u64,
134+
config: GovernanceConfig,
127135

128136
#[allow(dead_code)]
129-
/// Minimum % of tokens for a governance token owner to be able to create proposal
130-
/// It's the percentage of tokens out of the entire pool of governance tokens eligible to vote
131-
token_threshold_to_create_proposal: u8,
137+
/// Indicate whether Program's upgrade_authority should be transferred to the Governance PDA
138+
/// If it's set to false then it can be done at a later time
139+
/// However the instruction would validate the current upgrade_authority signed the transaction nonetheless
140+
transfer_upgrade_authority: bool,
132141
},
133142

134143
/// Create Proposal account for Instructions that will be executed at various slots in the future
@@ -418,3 +427,66 @@ pub fn set_vote_authority(
418427
data: instruction.try_to_vec().unwrap(),
419428
}
420429
}
430+
431+
/// Creates CreateAccountGovernance instruction
432+
pub fn create_account_governance(
433+
// Accounts
434+
payer: &Pubkey,
435+
// Args
436+
config: GovernanceConfig,
437+
) -> Instruction {
438+
let account_governance_address =
439+
get_account_governance_address(&config.realm, &config.governed_account);
440+
441+
let accounts = vec![
442+
AccountMeta::new_readonly(config.realm, false),
443+
AccountMeta::new(account_governance_address, false),
444+
AccountMeta::new_readonly(*payer, true),
445+
AccountMeta::new_readonly(system_program::id(), false),
446+
AccountMeta::new_readonly(sysvar::rent::id(), false),
447+
];
448+
449+
let instruction = GovernanceInstruction::CreateAccountGovernance { config };
450+
451+
Instruction {
452+
program_id: id(),
453+
accounts,
454+
data: instruction.try_to_vec().unwrap(),
455+
}
456+
}
457+
458+
/// Creates CreateProgramGovernance instruction
459+
pub fn create_program_governance(
460+
// Accounts
461+
governed_program_upgrade_authority: &Pubkey,
462+
payer: &Pubkey,
463+
// Args
464+
config: GovernanceConfig,
465+
transfer_upgrade_authority: bool,
466+
) -> Instruction {
467+
let program_governance_address =
468+
get_program_governance_address(&config.realm, &config.governed_account);
469+
let governed_program_data_address = get_program_data_address(&config.governed_account);
470+
471+
let accounts = vec![
472+
AccountMeta::new_readonly(config.realm, false),
473+
AccountMeta::new(program_governance_address, false),
474+
AccountMeta::new(governed_program_data_address, false),
475+
AccountMeta::new_readonly(*governed_program_upgrade_authority, true),
476+
AccountMeta::new_readonly(*payer, true),
477+
AccountMeta::new_readonly(bpf_loader_upgradeable::id(), false),
478+
AccountMeta::new_readonly(system_program::id(), false),
479+
AccountMeta::new_readonly(sysvar::rent::id(), false),
480+
];
481+
482+
let instruction = GovernanceInstruction::CreateProgramGovernance {
483+
config,
484+
transfer_upgrade_authority,
485+
};
486+
487+
Instruction {
488+
program_id: id(),
489+
accounts,
490+
data: instruction.try_to_vec().unwrap(),
491+
}
492+
}

‎governance/program/src/processor/mod.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Program processor
22
3+
mod process_create_account_governance;
4+
mod process_create_program_governance;
35
mod process_create_realm;
46
mod process_deposit_governing_tokens;
57
mod process_set_vote_authority;
@@ -8,6 +10,8 @@ mod process_withdraw_governing_tokens;
810
use crate::instruction::GovernanceInstruction;
911
use borsh::BorshDeserialize;
1012

13+
use process_create_account_governance::*;
14+
use process_create_program_governance::*;
1115
use process_create_realm::*;
1216
use process_deposit_governing_tokens::*;
1317
use process_set_vote_authority::*;
@@ -54,6 +58,18 @@ pub fn process_instruction(
5458
&governing_token_owner,
5559
&new_vote_authority,
5660
),
61+
GovernanceInstruction::CreateProgramGovernance {
62+
config,
63+
transfer_upgrade_authority,
64+
} => process_create_program_governance(
65+
program_id,
66+
accounts,
67+
config,
68+
transfer_upgrade_authority,
69+
),
70+
GovernanceInstruction::CreateAccountGovernance { config } => {
71+
process_create_account_governance(program_id, accounts, config)
72+
}
5773
_ => todo!("Instruction not implemented yet"),
5874
}
5975
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Program state processor
2+
3+
use crate::{
4+
state::{
5+
enums::GovernanceAccountType,
6+
governance::{
7+
assert_is_valid_governance_config, get_account_governance_address_seeds, Governance,
8+
GovernanceConfig,
9+
},
10+
},
11+
tools::account::create_and_serialize_account_signed,
12+
};
13+
use solana_program::{
14+
account_info::{next_account_info, AccountInfo},
15+
entrypoint::ProgramResult,
16+
pubkey::Pubkey,
17+
rent::Rent,
18+
sysvar::Sysvar,
19+
};
20+
21+
/// Processes CreateAccountGovernance instruction
22+
pub fn process_create_account_governance(
23+
program_id: &Pubkey,
24+
accounts: &[AccountInfo],
25+
config: GovernanceConfig,
26+
) -> ProgramResult {
27+
let account_info_iter = &mut accounts.iter();
28+
29+
let realm_info = next_account_info(account_info_iter)?; // 0
30+
let account_governance_info = next_account_info(account_info_iter)?; // 0
31+
let payer_info = next_account_info(account_info_iter)?; // 1
32+
let system_info = next_account_info(account_info_iter)?; // 2
33+
34+
let rent_sysvar_info = next_account_info(account_info_iter)?; // 3
35+
let rent = &Rent::from_account_info(rent_sysvar_info)?;
36+
37+
assert_is_valid_governance_config(&config, realm_info)?;
38+
39+
let account_governance_data = Governance {
40+
account_type: GovernanceAccountType::AccountGovernance,
41+
config: config.clone(),
42+
proposal_count: 0,
43+
};
44+
45+
create_and_serialize_account_signed::<Governance>(
46+
payer_info,
47+
&account_governance_info,
48+
&account_governance_data,
49+
&get_account_governance_address_seeds(&config.realm, &config.governed_account),
50+
program_id,
51+
system_info,
52+
rent,
53+
)?;
54+
55+
Ok(())
56+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//! Program state processor
2+
3+
use crate::{
4+
state::governance::Governance,
5+
state::{
6+
enums::GovernanceAccountType,
7+
governance::{
8+
assert_is_valid_governance_config, get_program_governance_address_seeds,
9+
GovernanceConfig,
10+
},
11+
},
12+
tools::{
13+
account::create_and_serialize_account_signed,
14+
bpf_loader_upgradeable::{
15+
assert_program_upgrade_authority_is_signer, set_program_upgrade_authority,
16+
},
17+
},
18+
};
19+
use solana_program::{
20+
account_info::{next_account_info, AccountInfo},
21+
entrypoint::ProgramResult,
22+
pubkey::Pubkey,
23+
rent::Rent,
24+
sysvar::Sysvar,
25+
};
26+
27+
/// Processes CreateProgramGovernance instruction
28+
pub fn process_create_program_governance(
29+
program_id: &Pubkey,
30+
accounts: &[AccountInfo],
31+
config: GovernanceConfig,
32+
transfer_upgrade_authority: bool,
33+
) -> ProgramResult {
34+
let account_info_iter = &mut accounts.iter();
35+
36+
let realm_info = next_account_info(account_info_iter)?; // 0
37+
let program_governance_info = next_account_info(account_info_iter)?; // 0
38+
39+
let governed_program_data_info = next_account_info(account_info_iter)?; // 1
40+
let governed_program_upgrade_authority_info = next_account_info(account_info_iter)?; // 2
41+
42+
let payer_info = next_account_info(account_info_iter)?; // 3
43+
let bpf_upgrade_loader_info = next_account_info(account_info_iter)?; // 4
44+
45+
let system_info = next_account_info(account_info_iter)?; // 5
46+
47+
let rent_sysvar_info = next_account_info(account_info_iter)?; // 6
48+
let rent = &Rent::from_account_info(rent_sysvar_info)?;
49+
50+
assert_is_valid_governance_config(&config, &realm_info)?;
51+
52+
let program_governance_data = Governance {
53+
account_type: GovernanceAccountType::ProgramGovernance,
54+
config: config.clone(),
55+
proposal_count: 0,
56+
};
57+
58+
create_and_serialize_account_signed::<Governance>(
59+
payer_info,
60+
&program_governance_info,
61+
&program_governance_data,
62+
&get_program_governance_address_seeds(&config.realm, &config.governed_account),
63+
program_id,
64+
system_info,
65+
rent,
66+
)?;
67+
68+
if transfer_upgrade_authority {
69+
set_program_upgrade_authority(
70+
&config.governed_account,
71+
governed_program_data_info,
72+
governed_program_upgrade_authority_info,
73+
program_governance_info,
74+
bpf_upgrade_loader_info,
75+
)?;
76+
} else {
77+
assert_program_upgrade_authority_is_signer(
78+
&config.governed_account,
79+
&governed_program_data_info,
80+
&governed_program_upgrade_authority_info,
81+
)?;
82+
}
83+
84+
Ok(())
85+
}

0 commit comments

Comments
 (0)