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 (#37)

* Make SetVotePercentage identical between FixedWeights and Vesting addins #36

* Fix set_vote_percentage function and command in vesting-cli #36

Co-authored-by: Semen Medvedev <[email protected]>
  • Loading branch information
s-medvedev and Semen Medvedev authored May 17, 2022
1 parent 5425078 commit 7bc097e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 49 deletions.
31 changes: 16 additions & 15 deletions addin-vesting/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,13 @@ fn command_set_vote_percentage_with_realm(
payer: Keypair,
vesting_authority: Keypair,
vesting_owner_pubkey: Pubkey,
vesting_token_pubkey: Pubkey,
mint_pubkey: Pubkey,
realm_pubkey: Pubkey,
percentage: u16,
) {

let instruction = set_vote_percentage_with_realm(
&vesting_addin_program_id,
&vesting_token_pubkey,
&vesting_owner_pubkey,
&vesting_authority.pubkey(),
&governance_program_id,
Expand Down Expand Up @@ -825,14 +823,25 @@ fn main() {
),
)
.arg(
Arg::with_name("vesting_address")
.long("vesting_address")
Arg::with_name("mint_address")
.long("mint_address")
.value_name("ADDRESS")
.required(true)
.validator(is_pubkey)
.takes_value(true)
.help(
"Specify the vesting token address (publickey).",
"Specify the address (publickey) of the mint for the token that should be used.",
),
)
.arg(
Arg::with_name("realm_address")
.long("realm_address")
.value_name("ADDRESS")
.required(true)
.validator(is_pubkey)
.takes_value(true)
.help(
"Specify the address (publickey) of the governance realm.",
),
)
.arg(
Expand Down Expand Up @@ -1094,30 +1103,22 @@ fn main() {
("set-vote-percentage", Some(arg_matches)) => {

let vesting_authority = keypair_of(arg_matches, "vesting_authority").unwrap();
let vesting_token_pubkey = pubkey_of(arg_matches, "vesting_address").unwrap();
let mint_pubkey = pubkey_of(arg_matches, "mint_address").unwrap();
let realm_pubkey = pubkey_of(arg_matches, "realm_address").unwrap();

let vesting_owner_pubkey = pubkey_of(arg_matches, "vesting_owner").unwrap();

let percentage: u16 = value_of(arg_matches, "percentage").unwrap();

let payer_keypair = keypair_of(arg_matches, "payer").unwrap_or( keypair_of(arg_matches, "vesting_authority").unwrap() );

let (vesting_pubkey,_) = Pubkey::find_program_address(&[&vesting_token_pubkey.as_ref()], &vesting_addin_program_id);

let vesting_record_account_data = rpc_client.get_account_data(&vesting_pubkey).unwrap();
let vesting_record: VestingRecord = try_from_slice_unchecked(&vesting_record_account_data).unwrap();

let mint_pubkey: Pubkey = vesting_record.mint;
let realm_pubkey: Pubkey = vesting_record.realm.unwrap();

command_set_vote_percentage_with_realm(
rpc_client,
governance_program_id,
vesting_addin_program_id,
payer_keypair,
vesting_authority,
vesting_owner_pubkey,
vesting_token_pubkey,
mint_pubkey,
realm_pubkey,
percentage,
Expand Down
40 changes: 19 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,17 @@ 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_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(*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
1 change: 0 additions & 1 deletion addin-vesting/program/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ async fn test_token_vesting_with_realm() {
let set_vote_percentage_instructions = [
set_vote_percentage_with_realm(
&program_id,
&vesting_token_account.pubkey(),
&destination_account.pubkey(),
&destination_delegate.pubkey(),
&governance_id,
Expand Down

0 comments on commit 7bc097e

Please sign in to comment.