set_authority.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #![cfg(feature = "test-sbf")]
  2. mod setup;
  3. use setup::{mint, TOKEN_PROGRAM_ID};
  4. use solana_program_test::{tokio, ProgramTest};
  5. use solana_sdk::{
  6. program_option::COption,
  7. program_pack::Pack,
  8. pubkey::Pubkey,
  9. signature::{Keypair, Signer},
  10. transaction::Transaction,
  11. };
  12. use spl_token::instruction::AuthorityType;
  13. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  14. #[tokio::test]
  15. async fn set_authority(token_program: Pubkey) {
  16. let mut context = ProgramTest::new("token_program", TOKEN_PROGRAM_ID, None)
  17. .start_with_context()
  18. .await;
  19. // Given a mint account.
  20. let mint_authority = Keypair::new();
  21. let freeze_authority = Keypair::new();
  22. let mint = mint::initialize(
  23. &mut context,
  24. mint_authority.pubkey(),
  25. Some(freeze_authority.pubkey()),
  26. &token_program,
  27. )
  28. .await
  29. .unwrap();
  30. // When we set a new freeze authority.
  31. let new_authority = Pubkey::new_unique();
  32. let mut set_authority_ix = spl_token::instruction::set_authority(
  33. &spl_token::ID,
  34. &mint,
  35. Some(&new_authority),
  36. AuthorityType::FreezeAccount,
  37. &freeze_authority.pubkey(),
  38. &[],
  39. )
  40. .unwrap();
  41. set_authority_ix.program_id = token_program;
  42. let tx = Transaction::new_signed_with_payer(
  43. &[set_authority_ix],
  44. Some(&context.payer.pubkey()),
  45. &[&context.payer, &freeze_authority],
  46. context.last_blockhash,
  47. );
  48. context.banks_client.process_transaction(tx).await.unwrap();
  49. // Then the account should have the delegate and delegated amount.
  50. let account = context.banks_client.get_account(mint).await.unwrap();
  51. assert!(account.is_some());
  52. let account = account.unwrap();
  53. let mint = spl_token::state::Mint::unpack(&account.data).unwrap();
  54. assert!(mint.freeze_authority == COption::Some(new_authority));
  55. }