|
| 1 | +use crate::common::{assertions::RpcErrorAssertions, *}; |
| 2 | +use jsonrpsee::rpc_params; |
| 3 | +use solana_sdk::{ |
| 4 | + program_pack::Pack, signature::Keypair, signer::Signer, transaction::Transaction, |
| 5 | +}; |
| 6 | +use solana_system_interface::instruction::create_account; |
| 7 | +use spl_associated_token_account::get_associated_token_address; |
| 8 | +use spl_token::instruction as token_instruction; |
| 9 | + |
| 10 | +#[tokio::test] |
| 11 | +async fn test_frozen_token_account_as_fee_payment() { |
| 12 | + let ctx = TestContext::new().await.expect("Failed to create test context"); |
| 13 | + let setup = TestAccountSetup::new().await; |
| 14 | + |
| 15 | + let frozen_token_account_keypair = Keypair::new(); |
| 16 | + |
| 17 | + let rent = setup |
| 18 | + .rpc_client |
| 19 | + .get_minimum_balance_for_rent_exemption(spl_token::state::Account::LEN) |
| 20 | + .await |
| 21 | + .expect("Failed to get rent exemption"); |
| 22 | + |
| 23 | + let create_account_ix = create_account( |
| 24 | + &setup.sender_keypair.pubkey(), |
| 25 | + &frozen_token_account_keypair.pubkey(), |
| 26 | + rent, |
| 27 | + spl_token::state::Account::LEN as u64, |
| 28 | + &spl_token::id(), |
| 29 | + ); |
| 30 | + |
| 31 | + let create_frozen_token_account_ix = spl_token::instruction::initialize_account( |
| 32 | + &spl_token::id(), |
| 33 | + &frozen_token_account_keypair.pubkey(), |
| 34 | + &setup.usdc_mint.pubkey(), |
| 35 | + &setup.sender_keypair.pubkey(), |
| 36 | + ) |
| 37 | + .expect("Failed to create initialize account instruction"); |
| 38 | + |
| 39 | + let mint_tokens_ix = token_instruction::mint_to( |
| 40 | + &spl_token::id(), |
| 41 | + &setup.usdc_mint.pubkey(), |
| 42 | + &frozen_token_account_keypair.pubkey(), |
| 43 | + &setup.sender_keypair.pubkey(), |
| 44 | + &[&setup.sender_keypair.pubkey()], |
| 45 | + 100_000, |
| 46 | + ) |
| 47 | + .expect("Failed to create mint instruction"); |
| 48 | + |
| 49 | + let freeze_instruction = token_instruction::freeze_account( |
| 50 | + &spl_token::id(), |
| 51 | + &frozen_token_account_keypair.pubkey(), |
| 52 | + &setup.usdc_mint.pubkey(), |
| 53 | + &setup.sender_keypair.pubkey(), |
| 54 | + &[&setup.sender_keypair.pubkey()], |
| 55 | + ) |
| 56 | + .expect("Failed to create freeze instruction"); |
| 57 | + |
| 58 | + let recent_blockhash = setup.rpc_client.get_latest_blockhash().await.unwrap(); |
| 59 | + let setup_tx = Transaction::new_signed_with_payer( |
| 60 | + &[create_account_ix, create_frozen_token_account_ix, mint_tokens_ix, freeze_instruction], |
| 61 | + Some(&setup.sender_keypair.pubkey()), |
| 62 | + &[&setup.sender_keypair, &frozen_token_account_keypair], |
| 63 | + recent_blockhash, |
| 64 | + ); |
| 65 | + |
| 66 | + setup |
| 67 | + .rpc_client |
| 68 | + .send_and_confirm_transaction(&setup_tx) |
| 69 | + .await |
| 70 | + .expect("Failed to setup and freeze token account"); |
| 71 | + |
| 72 | + let malicious_tx = ctx |
| 73 | + .transaction_builder() |
| 74 | + .with_fee_payer(FeePayerTestHelper::get_fee_payer_pubkey()) |
| 75 | + .with_spl_payment_with_accounts( |
| 76 | + &frozen_token_account_keypair.pubkey(), |
| 77 | + &get_associated_token_address( |
| 78 | + &FeePayerTestHelper::get_fee_payer_pubkey(), |
| 79 | + &setup.usdc_mint.pubkey(), |
| 80 | + ), |
| 81 | + &setup.sender_keypair.pubkey(), |
| 82 | + 50_000, |
| 83 | + ) |
| 84 | + .build() |
| 85 | + .await |
| 86 | + .expect("Failed to create transaction with frozen fee payer token account"); |
| 87 | + |
| 88 | + let result = |
| 89 | + ctx.rpc_call::<serde_json::Value, _>("signTransaction", rpc_params![malicious_tx]).await; |
| 90 | + |
| 91 | + match result { |
| 92 | + // 0x11: Frozen token account |
| 93 | + Err(error) => { |
| 94 | + error.assert_contains_message("custom program error: 0x11"); |
| 95 | + } |
| 96 | + Ok(_) => panic!("Expected error for transaction with frozen fee payment account"), |
| 97 | + } |
| 98 | +} |
0 commit comments