set_authority.rs 2.0 KB

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