Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 2d357e8

Browse files
authored
token-2022: Add transfer tests with extension too (#2868)
1 parent 43b9a37 commit 2d357e8

File tree

1 file changed

+91
-22
lines changed

1 file changed

+91
-22
lines changed

token/program-2022/tests/transfer.rs

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ use {
55
program_test::{TestContext, TokenContext},
66
solana_program_test::tokio,
77
solana_sdk::{
8-
instruction::InstructionError, signature::Signer, signer::keypair::Keypair,
8+
instruction::InstructionError, pubkey::Pubkey, signature::Signer, signer::keypair::Keypair,
99
transaction::TransactionError, transport::TransportError,
1010
},
1111
spl_token_2022::error::TokenError,
12-
spl_token_client::token::TokenError as TokenClientError,
12+
spl_token_client::token::{ExtensionInitializationParams, TokenError as TokenClientError},
1313
};
1414

15-
#[tokio::test]
16-
async fn basic() {
17-
let mut context = TestContext::new().await;
18-
context.init_token_with_mint(vec![]).await.unwrap();
15+
#[derive(PartialEq)]
16+
enum TestMode {
17+
All,
18+
CheckedOnly,
19+
}
20+
21+
async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
1922
let TokenContext {
2023
decimals,
2124
mint_authority,
@@ -43,11 +46,13 @@ async fn basic() {
4346
.await
4447
.unwrap();
4548

46-
// unchecked is ok
47-
token
48-
.transfer_unchecked(&alice_account, &bob_account, &alice, 1)
49-
.await
50-
.unwrap();
49+
if test_mode == TestMode::All {
50+
// unchecked is ok
51+
token
52+
.transfer_unchecked(&alice_account, &bob_account, &alice, 1)
53+
.await
54+
.unwrap();
55+
}
5156

5257
// checked is ok
5358
token
@@ -87,9 +92,28 @@ async fn basic() {
8792
}
8893

8994
#[tokio::test]
90-
async fn self_transfer() {
95+
async fn basic() {
9196
let mut context = TestContext::new().await;
9297
context.init_token_with_mint(vec![]).await.unwrap();
98+
run_basic_transfers(context, TestMode::All).await;
99+
}
100+
101+
#[tokio::test]
102+
async fn basic_with_extension() {
103+
let mut context = TestContext::new().await;
104+
context
105+
.init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig {
106+
transfer_fee_config_authority: Some(Pubkey::new_unique()),
107+
withdraw_withheld_authority: Some(Pubkey::new_unique()),
108+
transfer_fee_basis_points: 100u16,
109+
maximum_fee: 1_000_000u64,
110+
}])
111+
.await
112+
.unwrap();
113+
run_basic_transfers(context, TestMode::CheckedOnly).await;
114+
}
115+
116+
async fn run_self_transfers(context: TestContext, test_mode: TestMode) {
93117
let TokenContext {
94118
decimals,
95119
mint_authority,
@@ -116,10 +140,12 @@ async fn self_transfer() {
116140
.transfer_checked(&alice_account, &alice_account, &alice, 1, decimals)
117141
.await
118142
.unwrap();
119-
token
120-
.transfer_unchecked(&alice_account, &alice_account, &alice, 1)
121-
.await
122-
.unwrap();
143+
if test_mode == TestMode::All {
144+
token
145+
.transfer_unchecked(&alice_account, &alice_account, &alice, 1)
146+
.await
147+
.unwrap();
148+
}
123149

124150
// too much self transfer is not ok
125151
let error = token
@@ -138,9 +164,28 @@ async fn self_transfer() {
138164
}
139165

140166
#[tokio::test]
141-
async fn self_owned() {
167+
async fn self_transfer() {
142168
let mut context = TestContext::new().await;
143169
context.init_token_with_mint(vec![]).await.unwrap();
170+
run_self_transfers(context, TestMode::All).await;
171+
}
172+
173+
#[tokio::test]
174+
async fn self_transfer_with_extension() {
175+
let mut context = TestContext::new().await;
176+
context
177+
.init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig {
178+
transfer_fee_config_authority: Some(Pubkey::new_unique()),
179+
withdraw_withheld_authority: Some(Pubkey::new_unique()),
180+
transfer_fee_basis_points: 100u16,
181+
maximum_fee: 1_000_000u64,
182+
}])
183+
.await
184+
.unwrap();
185+
run_self_transfers(context, TestMode::CheckedOnly).await;
186+
}
187+
188+
async fn run_self_owned(context: TestContext, test_mode: TestMode) {
144189
let TokenContext {
145190
decimals,
146191
mint_authority,
@@ -167,11 +212,13 @@ async fn self_owned() {
167212
.await
168213
.unwrap();
169214

170-
// unchecked is ok
171-
token
172-
.transfer_unchecked(&alice_account, &bob_account, &alice, 1)
173-
.await
174-
.unwrap();
215+
if test_mode == TestMode::All {
216+
// unchecked is ok
217+
token
218+
.transfer_unchecked(&alice_account, &bob_account, &alice, 1)
219+
.await
220+
.unwrap();
221+
}
175222

176223
// checked is ok
177224
token
@@ -186,6 +233,28 @@ async fn self_owned() {
186233
.unwrap();
187234
}
188235

236+
#[tokio::test]
237+
async fn self_owned() {
238+
let mut context = TestContext::new().await;
239+
context.init_token_with_mint(vec![]).await.unwrap();
240+
run_self_owned(context, TestMode::All).await;
241+
}
242+
243+
#[tokio::test]
244+
async fn self_owned_with_extension() {
245+
let mut context = TestContext::new().await;
246+
context
247+
.init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig {
248+
transfer_fee_config_authority: Some(Pubkey::new_unique()),
249+
withdraw_withheld_authority: Some(Pubkey::new_unique()),
250+
transfer_fee_basis_points: 100u16,
251+
maximum_fee: 1_000_000u64,
252+
}])
253+
.await
254+
.unwrap();
255+
run_self_owned(context, TestMode::CheckedOnly).await;
256+
}
257+
189258
#[tokio::test]
190259
async fn transfer_with_fee_on_mint_without_fee_configured() {
191260
let mut context = TestContext::new().await;

0 commit comments

Comments
 (0)