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