account.rs 2.5 KB

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