revoke.rs 2.1 KB

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