Skip to content

Commit

Permalink
Make SetVotePercentage identical between FixedWeights and Vesting add…
Browse files Browse the repository at this point in the history
…ins #36
  • Loading branch information
Semen Medvedev committed May 17, 2022
1 parent 5425078 commit 22c254c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
41 changes: 20 additions & 21 deletions addin-vesting/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ pub enum VestingInstruction {
Withdraw,


/// Set Vote Percentage for calcalate voter_weight from total_amount of deposited tokens
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The Vesting Mint
/// 1. `[]` The Vesting Owner account
/// 2. `[signer]` The Vesting Authority account
/// 3. `[]` The Governance program account
/// 4. `[]` The Realm account
/// 5. `[]` Governing Owner Record. PDA seeds (governance program): ['governance', realm, token_mint, vesting_owner]
/// 6. `[writable]` The VoterWeight Record. PDA seeds: ['voter_weight', realm, token_mint, vesting_owner]
SetVotePercentage {
#[allow(dead_code)]
vote_percentage: u16,
},


/// Change the destination account of a given simple vesting contract (SVC)
/// - can only be invoked by the present destination address of the contract.
///
Expand Down Expand Up @@ -96,24 +114,6 @@ pub enum VestingInstruction {
/// 5. `[]` The Mint account
/// 6. `[writable]` The VoterWeightRecord. PDA seeds: ['voter_weight', realm, token_mint, token_owner]
CreateVoterWeightRecord,


/// Set Vote Percentage for calcalate voter_weight from total_amount of deposited tokens
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The Vesting account. PDA seeds: [vesting spl-token account]
/// 1. `[]` The Vesting Owner account
/// 2. `[signer]` The Vesting Authority account
/// 3. `[]` The Governance program account
/// 4. `[]` The Realm account
/// 5. `[]` Governing Owner Record. PDA seeds (governance program): ['governance', realm, token_mint, vesting_owner]
/// 6. `[writable]` The VoterWeight Record. PDA seeds: ['voter_weight', realm, token_mint, vesting_owner]
SetVotePercentage {
#[allow(dead_code)]
vote_percentage: u16,
},
}

/// Creates a `Deposit` instruction to create and initialize the vesting token account
Expand Down Expand Up @@ -351,19 +351,18 @@ pub fn create_voter_weight_record(
#[allow(clippy::too_many_arguments)]
pub fn set_vote_percentage_with_realm(
program_id: &Pubkey,
vesting_token_account: &Pubkey,
vesting_mint: &Pubkey,
vesting_owner: &Pubkey,
vesting_authority: &Pubkey,
governance_id: &Pubkey,
realm: &Pubkey,
mint: &Pubkey,
vote_percentage: u16,
) -> Result<Instruction, ProgramError> {
let (vesting_account, _) = Pubkey::find_program_address(&[vesting_token_account.as_ref()], program_id);
let token_owner_record_account = get_token_owner_record_address(governance_id, realm, mint, vesting_owner);
let voter_weight_record_account = get_voter_weight_record_address(program_id, realm, mint, vesting_owner);
let accounts = vec![
AccountMeta::new_readonly(vesting_account, false),
AccountMeta::new_readonly(*vesting_mint, false),
AccountMeta::new_readonly(*vesting_owner, false),
AccountMeta::new_readonly(*vesting_authority, true),
AccountMeta::new_readonly(*governance_id, false),
Expand Down
16 changes: 4 additions & 12 deletions addin-vesting/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,31 +440,23 @@ impl Processor {
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();

let vesting_account = next_account_info(accounts_iter)?;
let vesting_mint_account = next_account_info(accounts_iter)?;
let vesting_owner_account = next_account_info(accounts_iter)?;
let vesting_authority_account = next_account_info(accounts_iter)?;
let governance_account = next_account_info(accounts_iter)?;
let realm_account = next_account_info(accounts_iter)?;
let owner_record_account = next_account_info(accounts_iter)?;
let voter_weight_record_account = next_account_info(accounts_iter)?;

let vesting_record = get_account_data::<VestingRecord>(program_id, vesting_account)?;

let expected_realm_account = vesting_record.realm.ok_or(VestingError::VestingIsNotUnderRealm)?;

if *realm_account.key != expected_realm_account {
return Err(VestingError::InvalidRealmAccount.into())
};

let realm_data = get_realm_data(governance_account.key, realm_account)?;
realm_data.assert_is_valid_governing_token_mint(&vesting_record.mint)?;
realm_data.assert_is_valid_governing_token_mint(vesting_mint_account.key)?;

let owner_record_data = get_token_owner_record_data_for_seeds(
governance_account.key,
owner_record_account,
&get_token_owner_record_address_seeds(
realm_account.key,
&vesting_record.mint,
vesting_mint_account.key,
vesting_owner_account.key,
),
)?;
Expand All @@ -474,7 +466,7 @@ impl Processor {
program_id,
voter_weight_record_account,
realm_account.key,
&vesting_record.mint,
vesting_mint_account.key,
vesting_owner_account.key)?;

voter_weight_record.set_vote_percentage(vote_percentage)?;
Expand Down
2 changes: 1 addition & 1 deletion addin-vesting/program/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ async fn test_token_vesting_with_realm() {
let set_vote_percentage_instructions = [
set_vote_percentage_with_realm(
&program_id,
&vesting_token_account.pubkey(),
&mint.pubkey(),
&destination_account.pubkey(),
&destination_delegate.pubkey(),
&governance_id,
Expand Down

0 comments on commit 22c254c

Please sign in to comment.