@@ -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} ;
1216use borsh:: { BorshDeserialize , BorshSchema , BorshSerialize } ;
1317use 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+ }
0 commit comments