close_account.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. mod setup;
  2. use {
  3. setup::{account, mint, TOKEN_PROGRAM_ID},
  4. solana_program_test::{tokio, ProgramTest},
  5. solana_sdk::{
  6. pubkey::Pubkey,
  7. signature::{Keypair, Signer},
  8. transaction::Transaction,
  9. },
  10. };
  11. #[tokio::test]
  12. async fn close_account() {
  13. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  14. .start_with_context()
  15. .await;
  16. // Given a mint account.
  17. let mint_authority = Keypair::new();
  18. let freeze_authority = Pubkey::new_unique();
  19. let mint = mint::initialize(
  20. &mut context,
  21. mint_authority.pubkey(),
  22. Some(freeze_authority),
  23. &TOKEN_PROGRAM_ID,
  24. )
  25. .await
  26. .unwrap();
  27. // And a token account.
  28. let owner = Keypair::new();
  29. let account =
  30. account::initialize(&mut context, &mint, &owner.pubkey(), &TOKEN_PROGRAM_ID).await;
  31. let token_account = context.banks_client.get_account(account).await.unwrap();
  32. assert!(token_account.is_some());
  33. // When we close the account.
  34. let close_account_ix = spl_token::instruction::close_account(
  35. &spl_token::ID,
  36. &account,
  37. &owner.pubkey(),
  38. &owner.pubkey(),
  39. &[],
  40. )
  41. .unwrap();
  42. let tx = Transaction::new_signed_with_payer(
  43. &[close_account_ix],
  44. Some(&context.payer.pubkey()),
  45. &[&context.payer, &owner],
  46. context.last_blockhash,
  47. );
  48. context.banks_client.process_transaction(tx).await.unwrap();
  49. // Then an account must not exist.
  50. let token_account = context.banks_client.get_account(account).await.unwrap();
  51. assert!(token_account.is_none());
  52. }