set_authority.rs 1.9 KB

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