1
0

transfer.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 transfer() {
  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 transfer the tokens.
  42. let destination = Pubkey::new_unique();
  43. let destination_account =
  44. account::initialize(&mut context, &mint, &destination, &TOKEN_PROGRAM_ID).await;
  45. let transfer_ix = spl_token_interface::instruction::transfer(
  46. &spl_token_interface::ID,
  47. &account,
  48. &destination_account,
  49. &owner.pubkey(),
  50. &[],
  51. 100,
  52. )
  53. .unwrap();
  54. let tx = Transaction::new_signed_with_payer(
  55. &[transfer_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 an account has the correct data.
  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_interface::state::Account::unpack(&account.data).unwrap();
  66. assert!(account.amount == 0);
  67. }