close_account.rs 1.6 KB

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