@@ -53,11 +53,23 @@ fn create_token_account(
5353 }
5454}
5555
56+ /// Creates a Mollusk instance with the default feature set, excluding the
57+ /// `bpf_account_data_direct_mapping` feature.
58+ fn mollusk ( ) -> Mollusk {
59+ let mut mollusk = Mollusk :: default ( ) ;
60+ mollusk. add_program (
61+ & TOKEN_PROGRAM_ID ,
62+ "pinocchio_token_program" ,
63+ & bpf_loader_upgradeable:: id ( ) ,
64+ ) ;
65+ mollusk
66+ }
67+
5668fn unwrap_lamports_instruction (
5769 source : & Pubkey ,
5870 destination : & Pubkey ,
5971 authority : & Pubkey ,
60- amount : u64 ,
72+ amount : Option < u64 > ,
6173) -> Result < Instruction , ProgramError > {
6274 let accounts = vec ! [
6375 AccountMeta :: new( * source, false ) ,
@@ -67,7 +79,13 @@ fn unwrap_lamports_instruction(
6779
6880 // Start with the batch discriminator
6981 let mut data: Vec < u8 > = vec ! [ TokenInstruction :: UnwrapLamports as u8 ] ;
70- data. extend_from_slice ( & amount. to_le_bytes ( ) ) ;
82+
83+ if let Some ( amount) = amount {
84+ data. push ( 1 ) ;
85+ data. extend_from_slice ( & amount. to_le_bytes ( ) ) ;
86+ } else {
87+ data. push ( 0 ) ;
88+ }
7189
7290 Ok ( Instruction {
7391 program_id : spl_token:: ID ,
@@ -76,20 +94,63 @@ fn unwrap_lamports_instruction(
7694 } )
7795}
7896
79- /// Creates a Mollusk instance with the default feature set, excluding the
80- /// `bpf_account_data_direct_mapping` feature.
81- fn mollusk ( ) -> Mollusk {
82- let mut mollusk = Mollusk :: default ( ) ;
83- mollusk. add_program (
97+ #[ tokio:: test]
98+ async fn unwrap_lamports ( ) {
99+ let native_mint = Pubkey :: new_from_array ( native_mint:: ID ) ;
100+ let authority_key = Pubkey :: new_unique ( ) ;
101+ let destination_account_key = Pubkey :: new_unique ( ) ;
102+
103+ // native account:
104+ // - amount: 2_000_000_000
105+ let source_account_key = Pubkey :: new_unique ( ) ;
106+ let source_account = create_token_account (
107+ & native_mint,
108+ & authority_key,
109+ true ,
110+ 2_000_000_000 ,
84111 & TOKEN_PROGRAM_ID ,
85- "pinocchio_token_program" ,
86- & bpf_loader_upgradeable:: id ( ) ,
87112 ) ;
88- mollusk
113+
114+ let instruction = unwrap_lamports_instruction (
115+ & source_account_key,
116+ & destination_account_key,
117+ & authority_key,
118+ None ,
119+ )
120+ . unwrap ( ) ;
121+
122+ // It should succeed to unwrap 2_000_000_000 lamports.
123+
124+ let result = mollusk ( ) . process_and_validate_instruction (
125+ & instruction,
126+ & [
127+ ( source_account_key, source_account) ,
128+ ( destination_account_key, Account :: default ( ) ) ,
129+ ( authority_key, Account :: default ( ) ) ,
130+ ] ,
131+ & [
132+ Check :: success ( ) ,
133+ Check :: account ( & destination_account_key)
134+ . lamports ( 2_000_000_000 )
135+ . build ( ) ,
136+ Check :: account ( & source_account_key)
137+ . lamports ( Rent :: default ( ) . minimum_balance ( size_of :: < TokenAccount > ( ) ) )
138+ . build ( ) ,
139+ ] ,
140+ ) ;
141+
142+ // And the remaining amount must be 0.
143+
144+ result. resulting_accounts . iter ( ) . for_each ( |( key, account) | {
145+ if * key == source_account_key {
146+ let token_account = spl_token:: state:: Account :: unpack ( & account. data ) . unwrap ( ) ;
147+ assert_eq ! ( token_account. amount, 0 ) ;
148+ }
149+ } ) ;
89150}
90151
91152#[ tokio:: test]
92- async fn unwrap_lamports ( ) {
153+ async fn unwrap_lamports_with_amount ( ) {
93154 let native_mint = Pubkey :: new_from_array ( native_mint:: ID ) ;
94155 let authority_key = Pubkey :: new_unique ( ) ;
95156 let destination_account_key = Pubkey :: new_unique ( ) ;
@@ -109,7 +170,7 @@ async fn unwrap_lamports() {
109170 & source_account_key,
110171 & destination_account_key,
111172 & authority_key,
112- 2_000_000_000 ,
173+ Some ( 2_000_000_000 ) ,
113174 )
114175 . unwrap ( ) ;
115176
@@ -164,7 +225,7 @@ async fn fail_unwrap_lamports_with_insufficient_funds() {
164225 & source_account_key,
165226 & destination_account_key,
166227 & authority_key,
167- 2_000_000_000 ,
228+ Some ( 2_000_000_000 ) ,
168229 )
169230 . unwrap ( ) ;
170231
@@ -205,7 +266,7 @@ async fn unwrap_lamports_with_parial_amount() {
205266 & source_account_key,
206267 & destination_account_key,
207268 & authority_key,
208- 1_000_000_000 ,
269+ Some ( 1_000_000_000 ) ,
209270 )
210271 . unwrap ( ) ;
211272
@@ -263,7 +324,7 @@ async fn fail_unwrap_lamports_with_invalid_authority() {
263324 & source_account_key,
264325 & destination_account_key,
265326 & fake_authority_key, // <-- wrong authority
266- 2_000_000_000 ,
327+ Some ( 2_000_000_000 ) ,
267328 )
268329 . unwrap ( ) ;
269330
@@ -305,7 +366,7 @@ async fn fail_unwrap_lamports_with_non_native_account() {
305366 & source_account_key,
306367 & destination_account_key,
307368 & authority_key,
308- 1_000_000_000 ,
369+ Some ( 1_000_000_000 ) ,
309370 )
310371 . unwrap ( ) ;
311372
@@ -353,7 +414,7 @@ async fn unwrap_lamports_with_self_transfer() {
353414 & source_account_key,
354415 & source_account_key, // <-- destination same as source
355416 & authority_key,
356- 1_000_000_000 ,
417+ Some ( 1_000_000_000 ) ,
357418 )
358419 . unwrap ( ) ;
359420
@@ -407,7 +468,7 @@ async fn fail_unwrap_lamports_with_invalid_native_account() {
407468 & source_account_key,
408469 & destination_account_key,
409470 & authority_key,
410- 1_000_000_000 ,
471+ Some ( 1_000_000_000 ) ,
411472 )
412473 . unwrap ( ) ;
413474
0 commit comments