approve_checked.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 approve_checked(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. // When we approve a delegate.
  41. let delegate = Pubkey::new_unique();
  42. let mut approve_ix = spl_token::instruction::approve_checked(
  43. &spl_token::ID,
  44. &account,
  45. &mint,
  46. &delegate,
  47. &owner.pubkey(),
  48. &[],
  49. 50,
  50. 4,
  51. )
  52. .unwrap();
  53. approve_ix.program_id = token_program;
  54. let tx = Transaction::new_signed_with_payer(
  55. &[approve_ix],
  56. Some(&context.payer.pubkey()),
  57. &[&context.payer, &owner],
  58. context.last_blockhash,
  59. );
  60. context.banks_client.process_transaction(tx).await.unwrap();
  61. // Then the account should have the delegate and delegated amount.
  62. let account = context.banks_client.get_account(account).await.unwrap();
  63. assert!(account.is_some());
  64. let account = account.unwrap();
  65. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  66. assert!(account.delegate.is_some());
  67. assert!(account.delegate.unwrap() == delegate);
  68. assert!(account.delegated_amount == 50);
  69. }