revoke.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. mod setup;
  2. use {
  3. setup::{account, mint, TOKEN_PROGRAM_ID},
  4. solana_keypair::Keypair,
  5. solana_program_pack::Pack,
  6. solana_program_test::{tokio, ProgramTest},
  7. solana_pubkey::Pubkey,
  8. solana_signer::Signer,
  9. solana_transaction::Transaction,
  10. };
  11. #[tokio::test]
  12. async fn revoke() {
  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_ID,
  24. )
  25. .await
  26. .unwrap();
  27. // And a token account with 100 tokens.
  28. let owner = Keypair::new();
  29. let account =
  30. account::initialize(&mut context, &mint, &owner.pubkey(), &TOKEN_PROGRAM_ID).await;
  31. mint::mint(
  32. &mut context,
  33. &mint,
  34. &account,
  35. &mint_authority,
  36. 100,
  37. &TOKEN_PROGRAM_ID,
  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_ID,
  50. )
  51. .await;
  52. // When we revoke the delegation.
  53. let revoke_ix = spl_token_interface::instruction::revoke(
  54. &spl_token_interface::ID,
  55. &account,
  56. &owner.pubkey(),
  57. &[],
  58. )
  59. .unwrap();
  60. let tx = Transaction::new_signed_with_payer(
  61. &[revoke_ix],
  62. Some(&context.payer.pubkey()),
  63. &[&context.payer, &owner],
  64. context.last_blockhash,
  65. );
  66. context.banks_client.process_transaction(tx).await.unwrap();
  67. // Then the account should not have a delegate nor delegated amount.
  68. let account = context.banks_client.get_account(account).await.unwrap();
  69. assert!(account.is_some());
  70. let account = account.unwrap();
  71. let account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
  72. assert!(account.delegate.is_none());
  73. assert!(account.delegated_amount == 0);
  74. }