burn.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. mod setup;
  2. use {
  3. setup::{account, mint, TOKEN_PROGRAM_ID},
  4. solana_program_test::{tokio, ProgramTest},
  5. solana_sdk::{
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. transaction::Transaction,
  10. },
  11. };
  12. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  13. #[tokio::test]
  14. async fn burn(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 = Pubkey::new_unique();
  21. let mint = mint::initialize(
  22. &mut context,
  23. mint_authority.pubkey(),
  24. Some(freeze_authority),
  25. &token_program,
  26. )
  27. .await
  28. .unwrap();
  29. // And a token account with 100 tokens.
  30. let owner = Keypair::new();
  31. let account = account::initialize(&mut context, &mint, &owner.pubkey(), &token_program).await;
  32. mint::mint(
  33. &mut context,
  34. &mint,
  35. &account,
  36. &mint_authority,
  37. 100,
  38. &token_program,
  39. )
  40. .await
  41. .unwrap();
  42. // When we burn 50 tokens.
  43. let mut burn_ix =
  44. spl_token::instruction::burn(&spl_token::ID, &account, &mint, &owner.pubkey(), &[], 50)
  45. .unwrap();
  46. burn_ix.program_id = token_program;
  47. let tx = Transaction::new_signed_with_payer(
  48. &[burn_ix],
  49. Some(&context.payer.pubkey()),
  50. &[&context.payer, &owner],
  51. context.last_blockhash,
  52. );
  53. context.banks_client.process_transaction(tx).await.unwrap();
  54. // Then the account should have 50 tokens remaining.
  55. let account = context.banks_client.get_account(account).await.unwrap();
  56. assert!(account.is_some());
  57. let account = account.unwrap();
  58. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  59. assert!(account.amount == 50);
  60. }