burn_checked.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #[tokio::test]
  13. async fn burn_checked() {
  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_ID,
  25. )
  26. .await
  27. .unwrap();
  28. // And a token account with 100 tokens.
  29. let owner = Keypair::new();
  30. let account =
  31. account::initialize(&mut context, &mint, &owner.pubkey(), &TOKEN_PROGRAM_ID).await;
  32. mint::mint(
  33. &mut context,
  34. &mint,
  35. &account,
  36. &mint_authority,
  37. 100,
  38. &TOKEN_PROGRAM_ID,
  39. )
  40. .await
  41. .unwrap();
  42. // When we burn 50 tokens.
  43. let burn_ix = spl_token::instruction::burn_checked(
  44. &spl_token::ID,
  45. &account,
  46. &mint,
  47. &owner.pubkey(),
  48. &[],
  49. 50,
  50. 4,
  51. )
  52. .unwrap();
  53. let tx = Transaction::new_signed_with_payer(
  54. &[burn_ix],
  55. Some(&context.payer.pubkey()),
  56. &[&context.payer, &owner],
  57. context.last_blockhash,
  58. );
  59. context.banks_client.process_transaction(tx).await.unwrap();
  60. // Then the account should have 50 tokens remaining.
  61. let account = context.banks_client.get_account(account).await.unwrap();
  62. assert!(account.is_some());
  63. let account = account.unwrap();
  64. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  65. assert!(account.amount == 50);
  66. }