@@ -53,11 +53,23 @@ fn create_token_account(
53
53
}
54
54
}
55
55
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
+
56
68
fn unwrap_lamports_instruction (
57
69
source : & Pubkey ,
58
70
destination : & Pubkey ,
59
71
authority : & Pubkey ,
60
- amount : u64 ,
72
+ amount : Option < u64 > ,
61
73
) -> Result < Instruction , ProgramError > {
62
74
let accounts = vec ! [
63
75
AccountMeta :: new( * source, false ) ,
@@ -67,7 +79,13 @@ fn unwrap_lamports_instruction(
67
79
68
80
// Start with the batch discriminator
69
81
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
+ }
71
89
72
90
Ok ( Instruction {
73
91
program_id : spl_token:: ID ,
@@ -76,20 +94,63 @@ fn unwrap_lamports_instruction(
76
94
} )
77
95
}
78
96
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 ,
84
111
& TOKEN_PROGRAM_ID ,
85
- "pinocchio_token_program" ,
86
- & bpf_loader_upgradeable:: id ( ) ,
87
112
) ;
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
+ } ) ;
89
150
}
90
151
91
152
#[ tokio:: test]
92
- async fn unwrap_lamports ( ) {
153
+ async fn unwrap_lamports_with_amount ( ) {
93
154
let native_mint = Pubkey :: new_from_array ( native_mint:: ID ) ;
94
155
let authority_key = Pubkey :: new_unique ( ) ;
95
156
let destination_account_key = Pubkey :: new_unique ( ) ;
@@ -109,7 +170,7 @@ async fn unwrap_lamports() {
109
170
& source_account_key,
110
171
& destination_account_key,
111
172
& authority_key,
112
- 2_000_000_000 ,
173
+ Some ( 2_000_000_000 ) ,
113
174
)
114
175
. unwrap ( ) ;
115
176
@@ -164,7 +225,7 @@ async fn fail_unwrap_lamports_with_insufficient_funds() {
164
225
& source_account_key,
165
226
& destination_account_key,
166
227
& authority_key,
167
- 2_000_000_000 ,
228
+ Some ( 2_000_000_000 ) ,
168
229
)
169
230
. unwrap ( ) ;
170
231
@@ -205,7 +266,7 @@ async fn unwrap_lamports_with_parial_amount() {
205
266
& source_account_key,
206
267
& destination_account_key,
207
268
& authority_key,
208
- 1_000_000_000 ,
269
+ Some ( 1_000_000_000 ) ,
209
270
)
210
271
. unwrap ( ) ;
211
272
@@ -263,7 +324,7 @@ async fn fail_unwrap_lamports_with_invalid_authority() {
263
324
& source_account_key,
264
325
& destination_account_key,
265
326
& fake_authority_key, // <-- wrong authority
266
- 2_000_000_000 ,
327
+ Some ( 2_000_000_000 ) ,
267
328
)
268
329
. unwrap ( ) ;
269
330
@@ -305,7 +366,7 @@ async fn fail_unwrap_lamports_with_non_native_account() {
305
366
& source_account_key,
306
367
& destination_account_key,
307
368
& authority_key,
308
- 1_000_000_000 ,
369
+ Some ( 1_000_000_000 ) ,
309
370
)
310
371
. unwrap ( ) ;
311
372
@@ -353,7 +414,7 @@ async fn unwrap_lamports_with_self_transfer() {
353
414
& source_account_key,
354
415
& source_account_key, // <-- destination same as source
355
416
& authority_key,
356
- 1_000_000_000 ,
417
+ Some ( 1_000_000_000 ) ,
357
418
)
358
419
. unwrap ( ) ;
359
420
@@ -407,7 +468,7 @@ async fn fail_unwrap_lamports_with_invalid_native_account() {
407
468
& source_account_key,
408
469
& destination_account_key,
409
470
& authority_key,
410
- 1_000_000_000 ,
471
+ Some ( 1_000_000_000 ) ,
411
472
)
412
473
. unwrap ( ) ;
413
474
0 commit comments