1
0

account.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use solana_program_test::ProgramTestContext;
  2. use solana_sdk::{
  3. pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction,
  4. transaction::Transaction,
  5. };
  6. pub async fn initialize(
  7. context: &mut ProgramTestContext,
  8. mint: &Pubkey,
  9. owner: &Pubkey,
  10. program_id: &Pubkey,
  11. ) -> Pubkey {
  12. let account = Keypair::new();
  13. let account_size = 165;
  14. let rent = context.banks_client.get_rent().await.unwrap();
  15. let mut initialize_ix =
  16. spl_token::instruction::initialize_account(&spl_token::ID, &account.pubkey(), mint, owner)
  17. .unwrap();
  18. initialize_ix.program_id = *program_id;
  19. let instructions = vec![
  20. system_instruction::create_account(
  21. &context.payer.pubkey(),
  22. &account.pubkey(),
  23. rent.minimum_balance(account_size),
  24. account_size as u64,
  25. program_id,
  26. ),
  27. initialize_ix,
  28. ];
  29. let tx = Transaction::new_signed_with_payer(
  30. &instructions,
  31. Some(&context.payer.pubkey()),
  32. &[&context.payer, &account],
  33. context.last_blockhash,
  34. );
  35. context.banks_client.process_transaction(tx).await.unwrap();
  36. account.pubkey()
  37. }
  38. pub async fn approve(
  39. context: &mut ProgramTestContext,
  40. account: &Pubkey,
  41. delegate: &Pubkey,
  42. owner: &Keypair,
  43. amount: u64,
  44. program_id: &Pubkey,
  45. ) {
  46. let mut approve_ix = spl_token::instruction::approve(
  47. &spl_token::ID,
  48. account,
  49. delegate,
  50. &owner.pubkey(),
  51. &[],
  52. amount,
  53. )
  54. .unwrap();
  55. approve_ix.program_id = *program_id;
  56. let tx = Transaction::new_signed_with_payer(
  57. &[approve_ix],
  58. Some(&context.payer.pubkey()),
  59. &[&context.payer, owner],
  60. context.last_blockhash,
  61. );
  62. context.banks_client.process_transaction(tx).await.unwrap();
  63. }
  64. pub async fn freeze(
  65. context: &mut ProgramTestContext,
  66. account: &Pubkey,
  67. mint: &Pubkey,
  68. freeze_authority: &Keypair,
  69. program_id: &Pubkey,
  70. ) {
  71. let mut freeze_account_ix = spl_token::instruction::freeze_account(
  72. &spl_token::ID,
  73. account,
  74. mint,
  75. &freeze_authority.pubkey(),
  76. &[],
  77. )
  78. .unwrap();
  79. freeze_account_ix.program_id = *program_id;
  80. let tx = Transaction::new_signed_with_payer(
  81. &[freeze_account_ix],
  82. Some(&context.payer.pubkey()),
  83. &[&context.payer, freeze_authority],
  84. context.last_blockhash,
  85. );
  86. context.banks_client.process_transaction(tx).await.unwrap();
  87. }