@@ -3,7 +3,10 @@ use anchor_lang::solana_program::log::sol_log_compute_units;
3
3
use light_compressed_account:: instruction_data:: cpi_context:: CompressedCpiContext ;
4
4
use light_compressed_token_sdk:: {
5
5
account:: CTokenAccount ,
6
- instructions:: transfer:: instruction:: { TransferConfig , TransferInputs } ,
6
+ instructions:: transfer:: {
7
+ account_infos:: { filter_packed_accounts, TransferAccountInfos , MAX_ACCOUNT_INFOS } ,
8
+ instruction:: { TransferConfig , TransferInputs } ,
9
+ } ,
7
10
TokenAccountMeta ,
8
11
} ;
9
12
use light_sdk:: {
@@ -90,9 +93,9 @@ pub fn deposit_tokens<'info>(
90
93
91
94
// We need to be careful what accounts we pass.
92
95
// Big accounts cost many CU.
93
- // TODO: add a filter tree accounts function.
96
+ // TODO: replace
94
97
let tree_account_infos =
95
- filter_tree_accounts ( & [ & sender_account] , cpi_accounts. tree_accounts ( ) . unwrap ( ) ) ;
98
+ filter_packed_accounts ( & [ & sender_account] , cpi_accounts. tree_accounts ( ) . unwrap ( ) ) ;
96
99
let tree_pubkeys = tree_account_infos
97
100
. iter ( )
98
101
. map ( |x| x. pubkey ( ) )
@@ -129,20 +132,17 @@ pub fn deposit_tokens<'info>(
129
132
130
133
msg ! ( "create_account_infos" ) ;
131
134
sol_log_compute_units ( ) ;
132
- let account_infos = TransferAccountInfos {
135
+ // TODO: initialize from CpiAccounts, use with_compressed_pda() offchain.
136
+ let account_infos: TransferAccountInfos < ' _ , ' info , MAX_ACCOUNT_INFOS > = TransferAccountInfos {
133
137
fee_payer : cpi_accounts. fee_payer ( ) ,
134
138
authority : cpi_accounts. fee_payer ( ) ,
135
- tree_accounts : tree_account_infos. as_slice ( ) ,
139
+ packed_accounts : tree_account_infos. as_slice ( ) ,
136
140
ctoken_accounts : token_account_infos,
137
141
cpi_context : Some ( cpi_context) ,
138
142
} ;
139
143
let account_infos = account_infos. into_account_infos ( ) ;
140
- // 1390968
141
- // let account_infos = create_account_infos(
142
- // &instruction,
143
- // token_account_infos,
144
- // &[cpi_accounts.fee_payer()],
145
- // );
144
+ // into_account_infos_checked() can be used for debugging but doubles CU cost to 1.5k CU
145
+
146
146
sol_log_compute_units ( ) ;
147
147
148
148
sol_log_compute_units ( ) ;
@@ -153,119 +153,3 @@ pub fn deposit_tokens<'info>(
153
153
154
154
Ok ( ( ) )
155
155
}
156
-
157
- // 7479
158
- fn create_account_infos < ' info > (
159
- instruction : & Instruction ,
160
- account_infos : & [ AccountInfo < ' info > ] ,
161
- additionals : & [ & AccountInfo < ' info > ] ,
162
- ) -> Vec < AccountInfo < ' info > > {
163
- let mut res_account_infos = Vec :: with_capacity ( instruction. accounts . len ( ) ) ;
164
-
165
- for ( i, account_meta) in instruction. accounts . iter ( ) . enumerate ( ) {
166
- if let Some ( account_info) = account_infos[ i..]
167
- . iter ( )
168
- . find ( |x| * x. key == account_meta. pubkey )
169
- {
170
- res_account_infos. push ( account_info. clone ( ) ) ;
171
- } else {
172
- if let Some ( account_info) = additionals. iter ( ) . find ( |x| * x. key == account_meta. pubkey ) {
173
- res_account_infos. push ( ( * account_info) . clone ( ) ) ;
174
- } else {
175
- if let Some ( account_info) = account_infos[ ..i]
176
- . iter ( )
177
- . find ( |x| * x. key == account_meta. pubkey )
178
- {
179
- res_account_infos. push ( account_info. clone ( ) ) ;
180
- } else {
181
- panic ! ( "account not found" ) ;
182
- }
183
- }
184
- }
185
- }
186
- res_account_infos
187
- }
188
-
189
- // For pinocchio we will need to build the accounts in oder
190
- // The easiest is probably just pass the accounts multiple times since deserialization is zero copy.
191
- pub struct TransferAccountInfos < ' a , ' info > {
192
- fee_payer : & ' a AccountInfo < ' info > ,
193
- authority : & ' a AccountInfo < ' info > ,
194
- ctoken_accounts : & ' a [ AccountInfo < ' info > ] ,
195
- cpi_context : Option < & ' a AccountInfo < ' info > > ,
196
- // TODO: rename tree accounts to packed accounts
197
- tree_accounts : & ' a [ AccountInfo < ' info > ] ,
198
- }
199
-
200
- use anchor_lang:: solana_program:: instruction:: Instruction ;
201
- impl < ' info > TransferAccountInfos < ' _ , ' info > {
202
- // 874
203
- fn into_account_infos ( self ) -> Vec < AccountInfo < ' info > > {
204
- // TODO: experiment with array vec.
205
- // we can use array vec with default constant say 20 and in case it's not enough
206
- // we throw an error that the constant needs to be increased.
207
- let mut capacity = 2 + self . ctoken_accounts . len ( ) + self . tree_accounts . len ( ) ;
208
- let ctoken_program_id_index = self . ctoken_accounts . len ( ) - 2 ;
209
- if self . cpi_context . is_some ( ) {
210
- capacity += 1 ;
211
- }
212
- let mut account_infos = Vec :: with_capacity ( capacity) ;
213
- account_infos. push ( self . fee_payer . clone ( ) ) ;
214
- account_infos. push ( self . authority . clone ( ) ) ;
215
-
216
- account_infos. extend_from_slice ( self . ctoken_accounts ) ;
217
- if let Some ( cpi_context) = self . cpi_context {
218
- account_infos. push ( cpi_context. clone ( ) ) ;
219
- } else {
220
- account_infos. push ( self . ctoken_accounts [ ctoken_program_id_index] . clone ( ) ) ;
221
- }
222
- account_infos. extend_from_slice ( self . tree_accounts ) ;
223
- account_infos
224
- }
225
-
226
- // 1528
227
- fn into_account_infos_checked ( self , ix : & Instruction ) -> Result < Vec < AccountInfo < ' info > > > {
228
- let account_infos = self . into_account_infos ( ) ;
229
- for ( account_meta, account_info) in ix. accounts . iter ( ) . zip ( account_infos. iter ( ) ) {
230
- if account_meta. pubkey != * account_info. key {
231
- msg ! ( "account meta {:?}" , account_meta) ;
232
- msg ! ( "account info {:?}" , account_info) ;
233
-
234
- msg ! ( "account metas {:?}" , ix. accounts) ;
235
- msg ! ( "account infos {:?}" , account_infos) ;
236
- panic ! ( "account info and meta don't match." ) ;
237
- }
238
- }
239
- Ok ( account_infos)
240
- }
241
- }
242
-
243
- // TODO: test
244
- fn filter_tree_accounts < ' info > (
245
- token_accounts : & [ & CTokenAccount ] ,
246
- account_infos : & [ AccountInfo < ' info > ] ,
247
- ) -> Vec < AccountInfo < ' info > > {
248
- let mut selected_account_infos = Vec :: with_capacity ( account_infos. len ( ) ) ;
249
- account_infos
250
- . iter ( )
251
- . enumerate ( )
252
- . filter ( |( i, _) | {
253
- let i = * i as u8 ;
254
- token_accounts. iter ( ) . any ( |y| {
255
- y. merkle_tree_index == i
256
- || y. input_metas ( ) . iter ( ) . any ( |z| {
257
- z. packed_tree_info . merkle_tree_pubkey_index == i
258
- || z. packed_tree_info . queue_pubkey_index == i
259
- || {
260
- if let Some ( delegate_index) = z. delegate_index {
261
- delegate_index == i
262
- } else {
263
- false
264
- }
265
- }
266
- } )
267
- } )
268
- } )
269
- . for_each ( |x| selected_account_infos. push ( x. 1 . clone ( ) ) ) ;
270
- selected_account_infos
271
- }
0 commit comments