1
0

burn.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 burn() {
  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. // When we burn 50 tokens.
  42. let burn_ix =
  43. spl_token::instruction::burn(&spl_token::ID, &account, &mint, &owner.pubkey(), &[], 50)
  44. .unwrap();
  45. let tx = Transaction::new_signed_with_payer(
  46. &[burn_ix],
  47. Some(&context.payer.pubkey()),
  48. &[&context.payer, &owner],
  49. context.last_blockhash,
  50. );
  51. context.banks_client.process_transaction(tx).await.unwrap();
  52. // Then the account should have 50 tokens remaining.
  53. let account = context.banks_client.get_account(account).await.unwrap();
  54. assert!(account.is_some());
  55. let account = account.unwrap();
  56. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  57. assert!(account.amount == 50);
  58. }