|
| 1 | +use { |
| 2 | + anchor_lang::error::ErrorCode, |
| 3 | + integration_tests::{ |
| 4 | + assert_anchor_program_error, |
| 5 | + setup::{ |
| 6 | + setup, |
| 7 | + SetupProps, |
| 8 | + SetupResult, |
| 9 | + }, |
| 10 | + solana::utils::{ |
| 11 | + fetch_account_data, |
| 12 | + fetch_positions_account, |
| 13 | + }, |
| 14 | + staking::{ |
| 15 | + helper_functions::initialize_new_stake_account, |
| 16 | + instructions::{ |
| 17 | + create_position, |
| 18 | + create_voter_record, |
| 19 | + transfer_account, |
| 20 | + }, |
| 21 | + pda::{ |
| 22 | + get_stake_account_metadata_address, |
| 23 | + get_voter_record_address, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + solana_sdk::{ |
| 28 | + native_token::LAMPORTS_PER_SOL, |
| 29 | + signature::Keypair, |
| 30 | + signer::Signer, |
| 31 | + }, |
| 32 | + staking::{ |
| 33 | + error::ErrorCode as StakingError, |
| 34 | + state::{ |
| 35 | + positions::TargetWithParameters, |
| 36 | + stake_account::StakeAccountMetadataV2, |
| 37 | + voter_weight_record::VoterWeightRecord, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn test_transfer_account() { |
| 44 | + let SetupResult { |
| 45 | + mut svm, |
| 46 | + payer: governance_authority, |
| 47 | + pyth_token_mint, |
| 48 | + publisher_keypair: _, |
| 49 | + pool_data_pubkey: _, |
| 50 | + reward_program_authority: _, |
| 51 | + maybe_publisher_index: _, |
| 52 | + } = setup(SetupProps { |
| 53 | + init_config: true, |
| 54 | + init_target: true, |
| 55 | + init_mint: true, |
| 56 | + init_pool_data: true, |
| 57 | + init_publishers: true, |
| 58 | + reward_amount_override: None, |
| 59 | + }); |
| 60 | + |
| 61 | + let owner = Keypair::new(); |
| 62 | + let new_owner = Keypair::new(); |
| 63 | + |
| 64 | + svm.airdrop(&owner.pubkey(), LAMPORTS_PER_SOL).unwrap(); |
| 65 | + svm.airdrop(&new_owner.pubkey(), LAMPORTS_PER_SOL).unwrap(); |
| 66 | + |
| 67 | + let stake_account_positions = |
| 68 | + initialize_new_stake_account(&mut svm, &owner, &pyth_token_mint, true, true); |
| 69 | + // make sure voter record can be created permissionlessly if it doesn't exist |
| 70 | + create_voter_record(&mut svm, &new_owner, stake_account_positions).unwrap(); |
| 71 | + |
| 72 | + assert_anchor_program_error!( |
| 73 | + transfer_account( |
| 74 | + &mut svm, |
| 75 | + &owner, // governance_authority has to sign |
| 76 | + stake_account_positions, |
| 77 | + new_owner.pubkey() |
| 78 | + ), |
| 79 | + ErrorCode::ConstraintHasOne, |
| 80 | + 0 |
| 81 | + ); |
| 82 | + |
| 83 | + transfer_account( |
| 84 | + &mut svm, |
| 85 | + &governance_authority, |
| 86 | + stake_account_positions, |
| 87 | + new_owner.pubkey(), |
| 88 | + ) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + let mut positions_account = fetch_positions_account(&mut svm, &stake_account_positions); |
| 92 | + let positions = positions_account.to_dynamic_position_array(); |
| 93 | + assert_eq!(positions.owner().unwrap(), new_owner.pubkey()); |
| 94 | + |
| 95 | + let stake_account_metadata: StakeAccountMetadataV2 = fetch_account_data( |
| 96 | + &mut svm, |
| 97 | + &get_stake_account_metadata_address(stake_account_positions), |
| 98 | + ); |
| 99 | + assert_eq!(stake_account_metadata.owner, new_owner.pubkey()); |
| 100 | + |
| 101 | + let voter_record: VoterWeightRecord = |
| 102 | + fetch_account_data(&mut svm, &get_voter_record_address(stake_account_positions)); |
| 103 | + assert_eq!(voter_record.governing_token_owner, new_owner.pubkey()); |
| 104 | + |
| 105 | + // new_owner creates a new position |
| 106 | + create_position( |
| 107 | + &mut svm, |
| 108 | + &new_owner, |
| 109 | + stake_account_positions, |
| 110 | + TargetWithParameters::Voting, |
| 111 | + None, |
| 112 | + 100, |
| 113 | + ) |
| 114 | + .unwrap(); |
| 115 | + |
| 116 | + svm.expire_blockhash(); |
| 117 | + // now the account can't be recovered |
| 118 | + assert_anchor_program_error!( |
| 119 | + transfer_account( |
| 120 | + &mut svm, |
| 121 | + &governance_authority, |
| 122 | + stake_account_positions, |
| 123 | + new_owner.pubkey() |
| 124 | + ), |
| 125 | + StakingError::RecoverWithStake, |
| 126 | + 0 |
| 127 | + ); |
| 128 | +} |
0 commit comments