set_authority.rs 1.7 KB

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