revoke.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #![cfg(feature = "test-sbf")]
  2. mod setup;
  3. use setup::{account, mint};
  4. use solana_program_test::{tokio, ProgramTest};
  5. use solana_sdk::{
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. transaction::Transaction,
  10. };
  11. #[test_case::test_case(spl_token::ID ; "spl-token")]
  12. #[test_case::test_case(Pubkey::new_from_array(token_program::ID) ; "p-token")]
  13. #[tokio::test]
  14. async fn revoke(token_program: Pubkey) {
  15. let program_id = Pubkey::new_from_array(token_program::ID);
  16. let mut context = ProgramTest::new("token_program", program_id, None)
  17. .start_with_context()
  18. .await;
  19. // Given a mint account.
  20. let mint_authority = Keypair::new();
  21. let freeze_authority = Pubkey::new_unique();
  22. let mint = mint::initialize(
  23. &mut context,
  24. mint_authority.pubkey(),
  25. Some(freeze_authority),
  26. &token_program,
  27. )
  28. .await
  29. .unwrap();
  30. // And a token account with 100 tokens.
  31. let owner = Keypair::new();
  32. let account = account::initialize(&mut context, &mint, &owner.pubkey(), &token_program).await;
  33. mint::mint(
  34. &mut context,
  35. &mint,
  36. &account,
  37. &mint_authority,
  38. 100,
  39. &token_program,
  40. )
  41. .await
  42. .unwrap();
  43. // And 50 tokens delegated.
  44. let delegate = Pubkey::new_unique();
  45. account::approve(
  46. &mut context,
  47. &account,
  48. &delegate,
  49. &owner,
  50. 50,
  51. &token_program,
  52. )
  53. .await;
  54. // When we revoke the delegation.
  55. let mut revoke_ix =
  56. spl_token::instruction::revoke(&spl_token::ID, &account, &owner.pubkey(), &[]).unwrap();
  57. revoke_ix.program_id = token_program;
  58. let tx = Transaction::new_signed_with_payer(
  59. &[revoke_ix],
  60. Some(&context.payer.pubkey()),
  61. &[&context.payer, &owner],
  62. context.last_blockhash,
  63. );
  64. context.banks_client.process_transaction(tx).await.unwrap();
  65. // Then the account should not have a delegate nor delegated amount.
  66. let account = context.banks_client.get_account(account).await.unwrap();
  67. assert!(account.is_some());
  68. let account = account.unwrap();
  69. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  70. assert!(account.delegate.is_none());
  71. assert!(account.delegated_amount == 0);
  72. }