account.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use {
  2. solana_keypair::Keypair, solana_program_test::ProgramTestContext, solana_pubkey::Pubkey,
  3. solana_signer::Signer, solana_system_interface::instruction::create_account,
  4. solana_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 = spl_token_interface::instruction::initialize_account(
  16. &spl_token_interface::ID,
  17. &account.pubkey(),
  18. mint,
  19. owner,
  20. )
  21. .unwrap();
  22. initialize_ix.program_id = *program_id;
  23. let instructions = vec![
  24. create_account(
  25. &context.payer.pubkey(),
  26. &account.pubkey(),
  27. rent.minimum_balance(account_size),
  28. account_size as u64,
  29. program_id,
  30. ),
  31. initialize_ix,
  32. ];
  33. let tx = Transaction::new_signed_with_payer(
  34. &instructions,
  35. Some(&context.payer.pubkey()),
  36. &[&context.payer, &account],
  37. context.last_blockhash,
  38. );
  39. context.banks_client.process_transaction(tx).await.unwrap();
  40. account.pubkey()
  41. }
  42. pub async fn approve(
  43. context: &mut ProgramTestContext,
  44. account: &Pubkey,
  45. delegate: &Pubkey,
  46. owner: &Keypair,
  47. amount: u64,
  48. program_id: &Pubkey,
  49. ) {
  50. let mut approve_ix = spl_token_interface::instruction::approve(
  51. &spl_token_interface::ID,
  52. account,
  53. delegate,
  54. &owner.pubkey(),
  55. &[],
  56. amount,
  57. )
  58. .unwrap();
  59. approve_ix.program_id = *program_id;
  60. let tx = Transaction::new_signed_with_payer(
  61. &[approve_ix],
  62. Some(&context.payer.pubkey()),
  63. &[&context.payer, owner],
  64. context.last_blockhash,
  65. );
  66. context.banks_client.process_transaction(tx).await.unwrap();
  67. }
  68. pub async fn freeze(
  69. context: &mut ProgramTestContext,
  70. account: &Pubkey,
  71. mint: &Pubkey,
  72. freeze_authority: &Keypair,
  73. program_id: &Pubkey,
  74. ) {
  75. let mut freeze_account_ix = spl_token_interface::instruction::freeze_account(
  76. &spl_token_interface::ID,
  77. account,
  78. mint,
  79. &freeze_authority.pubkey(),
  80. &[],
  81. )
  82. .unwrap();
  83. freeze_account_ix.program_id = *program_id;
  84. let tx = Transaction::new_signed_with_payer(
  85. &[freeze_account_ix],
  86. Some(&context.payer.pubkey()),
  87. &[&context.payer, freeze_authority],
  88. context.last_blockhash,
  89. );
  90. context.banks_client.process_transaction(tx).await.unwrap();
  91. }