set_authority.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. mod setup;
  2. use setup::{mint, TOKEN_PROGRAM_ID};
  3. use solana_program_test::{tokio, ProgramTest};
  4. use solana_sdk::{
  5. program_option::COption,
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. transaction::Transaction,
  10. };
  11. use spl_token::instruction::AuthorityType;
  12. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  13. #[tokio::test]
  14. async fn set_authority(token_program: Pubkey) {
  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,
  26. )
  27. .await
  28. .unwrap();
  29. // When we set a new freeze authority.
  30. let new_authority = Pubkey::new_unique();
  31. let mut 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. set_authority_ix.program_id = token_program;
  41. let tx = Transaction::new_signed_with_payer(
  42. &[set_authority_ix],
  43. Some(&context.payer.pubkey()),
  44. &[&context.payer, &freeze_authority],
  45. context.last_blockhash,
  46. );
  47. context.banks_client.process_transaction(tx).await.unwrap();
  48. // Then the account should have the delegate and delegated amount.
  49. let account = context.banks_client.get_account(mint).await.unwrap();
  50. assert!(account.is_some());
  51. let account = account.unwrap();
  52. let mint = spl_token::state::Mint::unpack(&account.data).unwrap();
  53. assert!(mint.freeze_authority == COption::Some(new_authority));
  54. }