process_create_associated_token_account.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use {
  2. mollusk_svm::result::Check,
  3. solana_instruction::AccountMeta,
  4. solana_program_error::ProgramError,
  5. solana_pubkey::Pubkey,
  6. solana_sysvar as sysvar,
  7. spl_associated_token_account_interface::address::get_associated_token_address_with_program_id,
  8. spl_associated_token_account_mollusk_harness::{
  9. build_create_ata_instruction, token_2022_immutable_owner_rent_exempt_balance,
  10. AtaTestHarness, CreateAtaInstructionType,
  11. },
  12. };
  13. #[test]
  14. fn test_associated_token_address() {
  15. let mut harness =
  16. AtaTestHarness::new(&spl_token_2022_interface::id()).with_wallet_and_mint(1_000_000, 6);
  17. harness.create_ata(CreateAtaInstructionType::default());
  18. }
  19. #[test]
  20. fn test_create_with_fewer_lamports() {
  21. let harness =
  22. AtaTestHarness::new(&spl_token_2022_interface::id()).with_wallet_and_mint(1_000_000, 6);
  23. let wallet = harness.wallet.unwrap();
  24. let mint = harness.mint.unwrap();
  25. let ata_address = get_associated_token_address_with_program_id(
  26. &wallet,
  27. &mint,
  28. &spl_token_2022_interface::id(),
  29. );
  30. let insufficient_lamports = 890880;
  31. harness.ensure_account_exists_with_lamports(ata_address, insufficient_lamports);
  32. let instruction = build_create_ata_instruction(
  33. spl_associated_token_account_interface::program::id(),
  34. harness.payer,
  35. ata_address,
  36. wallet,
  37. mint,
  38. spl_token_2022_interface::id(),
  39. CreateAtaInstructionType::default(),
  40. );
  41. harness.ctx.process_and_validate_instruction(
  42. &instruction,
  43. &[
  44. Check::success(),
  45. Check::account(&ata_address)
  46. .lamports(token_2022_immutable_owner_rent_exempt_balance())
  47. .owner(&spl_token_2022_interface::id())
  48. .build(),
  49. ],
  50. );
  51. }
  52. #[test]
  53. fn test_create_with_excess_lamports() {
  54. let harness =
  55. AtaTestHarness::new(&spl_token_2022_interface::id()).with_wallet_and_mint(1_000_000, 6);
  56. let wallet = harness.wallet.unwrap();
  57. let mint = harness.mint.unwrap();
  58. let ata_address = get_associated_token_address_with_program_id(
  59. &wallet,
  60. &mint,
  61. &spl_token_2022_interface::id(),
  62. );
  63. let excess_lamports = token_2022_immutable_owner_rent_exempt_balance() + 1;
  64. harness.ensure_account_exists_with_lamports(ata_address, excess_lamports);
  65. let instruction = build_create_ata_instruction(
  66. spl_associated_token_account_interface::program::id(),
  67. harness.payer,
  68. ata_address,
  69. wallet,
  70. mint,
  71. spl_token_2022_interface::id(),
  72. CreateAtaInstructionType::default(),
  73. );
  74. harness.ctx.process_and_validate_instruction(
  75. &instruction,
  76. &[
  77. Check::success(),
  78. Check::account(&ata_address)
  79. .lamports(excess_lamports)
  80. .owner(&spl_token_2022_interface::id())
  81. .build(),
  82. ],
  83. );
  84. }
  85. #[test]
  86. fn test_create_account_mismatch() {
  87. let harness =
  88. AtaTestHarness::new(&spl_token_2022_interface::id()).with_wallet_and_mint(1_000_000, 6);
  89. let wallet = harness.wallet.unwrap();
  90. let mint = harness.mint.unwrap();
  91. let ata_address = get_associated_token_address_with_program_id(
  92. &wallet,
  93. &mint,
  94. &spl_token_2022_interface::id(),
  95. );
  96. for account_idx in [1, 2, 3] {
  97. let mut instruction = build_create_ata_instruction(
  98. spl_associated_token_account_interface::program::id(),
  99. harness.payer,
  100. ata_address,
  101. wallet,
  102. mint,
  103. spl_token_2022_interface::id(),
  104. CreateAtaInstructionType::default(),
  105. );
  106. instruction.accounts[account_idx] = if account_idx == 1 {
  107. AccountMeta::new(Pubkey::default(), false)
  108. } else {
  109. AccountMeta::new_readonly(Pubkey::default(), false)
  110. };
  111. harness.ctx.process_and_validate_instruction(
  112. &instruction,
  113. &[Check::err(ProgramError::InvalidSeeds)],
  114. );
  115. }
  116. }
  117. #[test]
  118. fn test_create_associated_token_account_using_legacy_implicit_instruction() {
  119. let mut harness =
  120. AtaTestHarness::new(&spl_token_2022_interface::id()).with_wallet_and_mint(1_000_000, 6);
  121. harness.create_and_check_ata_with_custom_instruction(
  122. CreateAtaInstructionType::default(),
  123. |instruction| {
  124. instruction.data = vec![];
  125. instruction
  126. .accounts
  127. .push(AccountMeta::new_readonly(sysvar::rent::id(), false));
  128. },
  129. );
  130. }