5
5
program_test:: { TestContext , TokenContext } ,
6
6
solana_program_test:: tokio,
7
7
solana_sdk:: {
8
- instruction:: InstructionError , signature:: Signer , signer:: keypair:: Keypair ,
8
+ instruction:: InstructionError , pubkey :: Pubkey , signature:: Signer , signer:: keypair:: Keypair ,
9
9
transaction:: TransactionError , transport:: TransportError ,
10
10
} ,
11
11
spl_token_2022:: error:: TokenError ,
12
- spl_token_client:: token:: TokenError as TokenClientError ,
12
+ spl_token_client:: token:: { ExtensionInitializationParams , TokenError as TokenClientError } ,
13
13
} ;
14
14
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 ) {
19
22
let TokenContext {
20
23
decimals,
21
24
mint_authority,
@@ -43,11 +46,13 @@ async fn basic() {
43
46
. await
44
47
. unwrap ( ) ;
45
48
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
+ }
51
56
52
57
// checked is ok
53
58
token
@@ -87,9 +92,28 @@ async fn basic() {
87
92
}
88
93
89
94
#[ tokio:: test]
90
- async fn self_transfer ( ) {
95
+ async fn basic ( ) {
91
96
let mut context = TestContext :: new ( ) . await ;
92
97
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 ) {
93
117
let TokenContext {
94
118
decimals,
95
119
mint_authority,
@@ -116,10 +140,12 @@ async fn self_transfer() {
116
140
. transfer_checked ( & alice_account, & alice_account, & alice, 1 , decimals)
117
141
. await
118
142
. 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
+ }
123
149
124
150
// too much self transfer is not ok
125
151
let error = token
@@ -138,9 +164,28 @@ async fn self_transfer() {
138
164
}
139
165
140
166
#[ tokio:: test]
141
- async fn self_owned ( ) {
167
+ async fn self_transfer ( ) {
142
168
let mut context = TestContext :: new ( ) . await ;
143
169
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 ) {
144
189
let TokenContext {
145
190
decimals,
146
191
mint_authority,
@@ -167,11 +212,13 @@ async fn self_owned() {
167
212
. await
168
213
. unwrap ( ) ;
169
214
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
+ }
175
222
176
223
// checked is ok
177
224
token
@@ -186,6 +233,28 @@ async fn self_owned() {
186
233
. unwrap ( ) ;
187
234
}
188
235
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
+
189
258
#[ tokio:: test]
190
259
async fn transfer_with_fee_on_mint_without_fee_configured ( ) {
191
260
let mut context = TestContext :: new ( ) . await ;
0 commit comments