@@ -15,14 +15,13 @@ use solana_sdk::{
15
15
transaction:: Transaction ,
16
16
} ;
17
17
use spl_associated_token_account:: get_associated_token_address_with_program_id;
18
- use spl_token:: instruction:: AuthorityType ;
19
18
use wormhole_anchor_sdk:: wormhole:: { BridgeData , FeeCollector } ;
20
19
21
20
use crate :: sdk:: {
22
21
accounts:: { Governance , Wormhole , NTT } ,
23
22
instructions:: {
24
23
admin:: { register_transceiver, set_peer, RegisterTransceiver , SetPeer } ,
25
- initialize:: { initialize , Initialize } ,
24
+ initialize:: { initialize_with_token_program_id , Initialize } ,
26
25
} ,
27
26
transceivers:: wormhole:: instructions:: admin:: { set_transceiver_peer, SetTransceiverPeer } ,
28
27
} ;
@@ -73,10 +72,33 @@ pub async fn setup_with_extra_accounts(
73
72
( ctx, test_data)
74
73
}
75
74
75
+ pub async fn setup_with_extra_accounts_with_transfer_fee (
76
+ mode : Mode ,
77
+ accounts : & [ ( Pubkey , Account ) ] ,
78
+ ) -> ( ProgramTestContext , TestData ) {
79
+ let program_owner = Keypair :: new ( ) ;
80
+ let mut program_test = setup_programs ( program_owner. pubkey ( ) ) . await . unwrap ( ) ;
81
+
82
+ for ( pubkey, account) in accounts {
83
+ program_test. add_account ( * pubkey, account. clone ( ) ) ;
84
+ }
85
+
86
+ let mut ctx = program_test. start_with_context ( ) . await ;
87
+
88
+ let test_data = setup_accounts_with_transfer_fee ( & mut ctx, program_owner) . await ;
89
+ setup_ntt_with_token_program_id ( & mut ctx, & test_data, mode, & spl_token_2022:: id ( ) ) . await ;
90
+
91
+ ( ctx, test_data)
92
+ }
93
+
76
94
pub async fn setup ( mode : Mode ) -> ( ProgramTestContext , TestData ) {
77
95
setup_with_extra_accounts ( mode, & [ ] ) . await
78
96
}
79
97
98
+ pub async fn setup_with_transfer_fee ( mode : Mode ) -> ( ProgramTestContext , TestData ) {
99
+ setup_with_extra_accounts_with_transfer_fee ( mode, & [ ] ) . await
100
+ }
101
+
80
102
fn prefer_bpf ( ) -> bool {
81
103
std:: env:: var ( "BPF_OUT_DIR" ) . is_ok ( ) || std:: env:: var ( "SBF_OUT_DIR" ) . is_ok ( )
82
104
}
@@ -126,13 +148,22 @@ pub async fn setup_programs(program_owner: Pubkey) -> Result<ProgramTest, Error>
126
148
/// Set up test accounts, and mint MINT_AMOUNT to the user's token account
127
149
/// Set up the program for locking mode, and registers a peer
128
150
pub async fn setup_ntt ( ctx : & mut ProgramTestContext , test_data : & TestData , mode : Mode ) {
151
+ setup_ntt_with_token_program_id ( ctx, test_data, mode, & Token :: id ( ) ) . await ;
152
+ }
153
+
154
+ pub async fn setup_ntt_with_token_program_id (
155
+ ctx : & mut ProgramTestContext ,
156
+ test_data : & TestData ,
157
+ mode : Mode ,
158
+ token_program_id : & Pubkey ,
159
+ ) {
129
160
if mode == Mode :: Burning {
130
161
// we set the mint authority to the ntt contract in burn/mint mode
131
- spl_token :: instruction:: set_authority (
132
- & spl_token :: ID ,
162
+ spl_token_2022 :: instruction:: set_authority (
163
+ token_program_id ,
133
164
& test_data. mint ,
134
165
Some ( & test_data. ntt . token_authority ( ) ) ,
135
- AuthorityType :: MintTokens ,
166
+ spl_token_2022 :: instruction :: AuthorityType :: MintTokens ,
136
167
& test_data. mint_authority . pubkey ( ) ,
137
168
& [ ] ,
138
169
)
@@ -142,7 +173,7 @@ pub async fn setup_ntt(ctx: &mut ProgramTestContext, test_data: &TestData, mode:
142
173
. unwrap ( ) ;
143
174
}
144
175
145
- initialize (
176
+ initialize_with_token_program_id (
146
177
& test_data. ntt ,
147
178
Initialize {
148
179
payer : ctx. payer . pubkey ( ) ,
@@ -155,6 +186,7 @@ pub async fn setup_ntt(ctx: &mut ProgramTestContext, test_data: &TestData, mode:
155
186
limit : OUTBOUND_LIMIT ,
156
187
mode,
157
188
} ,
189
+ token_program_id,
158
190
)
159
191
. submit_with_signers ( & [ & test_data. program_owner ] , ctx)
160
192
. await
@@ -265,6 +297,71 @@ pub async fn setup_accounts(ctx: &mut ProgramTestContext, program_owner: Keypair
265
297
}
266
298
}
267
299
300
+ pub async fn setup_accounts_with_transfer_fee (
301
+ ctx : & mut ProgramTestContext ,
302
+ program_owner : Keypair ,
303
+ ) -> TestData {
304
+ // create mint
305
+ let mint = Keypair :: new ( ) ;
306
+ let mint_authority = Keypair :: new ( ) ;
307
+
308
+ let user = Keypair :: new ( ) ;
309
+ let payer = ctx. payer . pubkey ( ) ;
310
+
311
+ create_mint_with_transfer_fee ( ctx, & mint, & mint_authority. pubkey ( ) , 9 , 500 , 5000 )
312
+ . await
313
+ . submit ( ctx)
314
+ . await
315
+ . unwrap ( ) ;
316
+
317
+ // create associated token account for user
318
+ let user_token_account = get_associated_token_address_with_program_id (
319
+ & user. pubkey ( ) ,
320
+ & mint. pubkey ( ) ,
321
+ & spl_token_2022:: id ( ) ,
322
+ ) ;
323
+
324
+ spl_associated_token_account:: instruction:: create_associated_token_account (
325
+ & payer,
326
+ & user. pubkey ( ) ,
327
+ & mint. pubkey ( ) ,
328
+ & spl_token_2022:: id ( ) ,
329
+ )
330
+ . submit ( ctx)
331
+ . await
332
+ . unwrap ( ) ;
333
+
334
+ spl_token_2022:: instruction:: mint_to (
335
+ & spl_token_2022:: id ( ) ,
336
+ & mint. pubkey ( ) ,
337
+ & user_token_account,
338
+ & mint_authority. pubkey ( ) ,
339
+ & [ ] ,
340
+ MINT_AMOUNT ,
341
+ )
342
+ . unwrap ( )
343
+ . submit_with_signers ( & [ & mint_authority] , ctx)
344
+ . await
345
+ . unwrap ( ) ;
346
+
347
+ TestData {
348
+ ntt : NTT {
349
+ program : example_native_token_transfers:: ID ,
350
+ wormhole : Wormhole {
351
+ program : wormhole_anchor_sdk:: wormhole:: program:: ID ,
352
+ } ,
353
+ } ,
354
+ governance : Governance {
355
+ program : wormhole_governance:: ID ,
356
+ } ,
357
+ program_owner,
358
+ mint_authority,
359
+ mint : mint. pubkey ( ) ,
360
+ user,
361
+ user_token_account,
362
+ }
363
+ }
364
+
268
365
pub async fn create_mint (
269
366
ctx : & mut ProgramTestContext ,
270
367
mint : & Keypair ,
@@ -300,6 +397,57 @@ pub async fn create_mint(
300
397
)
301
398
}
302
399
400
+ pub async fn create_mint_with_transfer_fee (
401
+ ctx : & mut ProgramTestContext ,
402
+ mint : & Keypair ,
403
+ mint_authority : & Pubkey ,
404
+ decimals : u8 ,
405
+ transfer_fee_basis_points : u16 ,
406
+ maximum_fee : u64 ,
407
+ ) -> Transaction {
408
+ let rent = ctx. banks_client . get_rent ( ) . await . unwrap ( ) ;
409
+ let extension_types = vec ! [ spl_token_2022:: extension:: ExtensionType :: TransferFeeConfig ] ;
410
+ let space = spl_token_2022:: extension:: ExtensionType :: try_calculate_account_len :: <
411
+ spl_token_2022:: state:: Mint ,
412
+ > ( & extension_types)
413
+ . unwrap ( ) ;
414
+ let mint_rent = rent. minimum_balance ( space) ;
415
+
416
+ let blockhash = ctx. banks_client . get_latest_blockhash ( ) . await . unwrap ( ) ;
417
+
418
+ Transaction :: new_signed_with_payer (
419
+ & [
420
+ system_instruction:: create_account (
421
+ & ctx. payer . pubkey ( ) ,
422
+ & mint. pubkey ( ) ,
423
+ mint_rent,
424
+ space as u64 ,
425
+ & spl_token_2022:: id ( ) ,
426
+ ) ,
427
+ spl_token_2022:: extension:: transfer_fee:: instruction:: initialize_transfer_fee_config (
428
+ & spl_token_2022:: id ( ) ,
429
+ & mint. pubkey ( ) ,
430
+ None ,
431
+ None ,
432
+ transfer_fee_basis_points,
433
+ maximum_fee,
434
+ )
435
+ . unwrap ( ) ,
436
+ spl_token_2022:: instruction:: initialize_mint2 (
437
+ & spl_token_2022:: id ( ) ,
438
+ & mint. pubkey ( ) ,
439
+ mint_authority,
440
+ None ,
441
+ decimals,
442
+ )
443
+ . unwrap ( ) ,
444
+ ] ,
445
+ Some ( & ctx. payer . pubkey ( ) ) ,
446
+ & [ & ctx. payer , & mint] ,
447
+ blockhash,
448
+ )
449
+ }
450
+
303
451
// TODO: upstream this to solana-program-test
304
452
305
453
/// Add a SBF program to the test environment. (copied from solana_program_test
0 commit comments