setup.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use {
  2. solana_keypair::Keypair,
  3. solana_program_test::ProgramTestContext,
  4. solana_pubkey::Pubkey,
  5. solana_rent::Rent,
  6. solana_signer::Signer,
  7. solana_stake_interface::{
  8. instruction as stake_instruction,
  9. state::{Authorized, Lockup},
  10. },
  11. solana_system_interface::{instruction as system_instruction, program as system_program},
  12. solana_transaction::Transaction,
  13. solana_vote_program::{
  14. vote_instruction,
  15. vote_state::{self, VoteInit, VoteStateV4},
  16. },
  17. };
  18. pub async fn setup_stake(
  19. context: &mut ProgramTestContext,
  20. user: &Keypair,
  21. vote_address: &Pubkey,
  22. stake_lamports: u64,
  23. ) -> Pubkey {
  24. let stake_keypair = Keypair::new();
  25. let transaction = Transaction::new_signed_with_payer(
  26. &stake_instruction::create_account_and_delegate_stake(
  27. &context.payer.pubkey(),
  28. &stake_keypair.pubkey(),
  29. vote_address,
  30. &Authorized::auto(&user.pubkey()),
  31. &Lockup::default(),
  32. stake_lamports,
  33. ),
  34. Some(&context.payer.pubkey()),
  35. &vec![&context.payer, &stake_keypair, user],
  36. context.last_blockhash,
  37. );
  38. context
  39. .banks_client
  40. .process_transaction(transaction)
  41. .await
  42. .unwrap();
  43. stake_keypair.pubkey()
  44. }
  45. pub async fn setup_vote(context: &mut ProgramTestContext) -> Pubkey {
  46. let mut instructions = vec![];
  47. let validator_keypair = Keypair::new();
  48. instructions.push(system_instruction::create_account(
  49. &context.payer.pubkey(),
  50. &validator_keypair.pubkey(),
  51. Rent::default().minimum_balance(0),
  52. 0,
  53. &system_program::id(),
  54. ));
  55. let vote_lamports = Rent::default().minimum_balance(VoteStateV4::size_of());
  56. let vote_keypair = Keypair::new();
  57. let user_keypair = Keypair::new();
  58. instructions.append(&mut vote_instruction::create_account_with_config(
  59. &context.payer.pubkey(),
  60. &vote_keypair.pubkey(),
  61. &VoteInit {
  62. node_pubkey: validator_keypair.pubkey(),
  63. authorized_voter: user_keypair.pubkey(),
  64. ..VoteInit::default()
  65. },
  66. vote_lamports,
  67. vote_instruction::CreateVoteAccountConfig {
  68. space: vote_state::VoteStateV4::size_of() as u64,
  69. ..vote_instruction::CreateVoteAccountConfig::default()
  70. },
  71. ));
  72. let transaction = Transaction::new_signed_with_payer(
  73. &instructions,
  74. Some(&context.payer.pubkey()),
  75. &vec![&context.payer, &validator_keypair, &vote_keypair],
  76. context.last_blockhash,
  77. );
  78. context
  79. .banks_client
  80. .process_transaction(transaction)
  81. .await
  82. .unwrap();
  83. vote_keypair.pubkey()
  84. }