revoke.rs 2.2 KB

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