1
1
use anchor_lang:: InstructionData ;
2
+ use light_client:: indexer:: CompressedTokenAccount ;
2
3
use light_compressed_token_sdk:: {
3
- instructions:: batch_compress:: {
4
- get_batch_compress_instruction_account_metas, BatchCompressMetaConfig , Recipient ,
4
+ instructions:: {
5
+ batch_compress:: {
6
+ get_batch_compress_instruction_account_metas, BatchCompressMetaConfig , Recipient ,
7
+ } ,
8
+ transfer:: account_metas:: {
9
+ get_transfer_instruction_account_metas, TokenAccountsMetaConfig ,
10
+ } ,
5
11
} ,
6
12
token_pool:: find_token_pool_pda_with_index,
7
- SPL_TOKEN_PROGRAM_ID ,
13
+ TokenAccountMeta , SPL_TOKEN_PROGRAM_ID ,
8
14
} ;
9
15
use light_program_test:: { AddressWithTree , Indexer , LightProgramTest , ProgramTestConfig , Rpc } ;
10
16
use light_sdk:: {
@@ -32,49 +38,72 @@ async fn test_deposit_compressed_account() {
32
38
. unwrap ( ) ;
33
39
34
40
let payer = rpc. get_payer ( ) . insecure_clone ( ) ;
35
- let owner = Keypair :: new ( ) ;
36
41
let deposit_amount = 1000u64 ;
37
42
38
- // Create recipients for batch compression
39
- let recipient1 = Keypair :: new ( ) . pubkey ( ) ;
40
- let recipient2 = Keypair :: new ( ) . pubkey ( ) ;
41
-
42
- let recipients = vec ! [
43
- Recipient {
44
- pubkey: recipient1,
45
- amount: 100_000 ,
46
- } ,
47
- Recipient {
48
- pubkey: recipient2,
49
- amount: 200_000 ,
50
- } ,
51
- ] ;
43
+ let recipients = vec ! [ Recipient {
44
+ pubkey: payer. pubkey( ) ,
45
+ amount: 100_000 ,
46
+ } ] ;
52
47
53
48
// Execute batch compress (this will create mint, token account, and compress)
54
- batch_compress_spl_tokens ( & mut rpc, & payer, recipients)
49
+ batch_compress_spl_tokens ( & mut rpc, & payer, recipients. clone ( ) )
55
50
. await
56
51
. unwrap ( ) ;
57
52
58
53
println ! ( "Batch compressed tokens successfully" ) ;
59
54
55
+ // Fetch the compressed token accounts created by batch compress
56
+ let recipient1 = recipients[ 0 ] . pubkey ;
57
+ let compressed_accounts = rpc
58
+ . indexer ( )
59
+ . unwrap ( )
60
+ . get_compressed_token_accounts_by_owner ( & recipient1, None , None )
61
+ . await
62
+ . unwrap ( )
63
+ . value
64
+ . items ;
65
+
66
+ assert ! (
67
+ !compressed_accounts. is_empty( ) ,
68
+ "Should have compressed token accounts"
69
+ ) ;
70
+ let ctoken_account = & compressed_accounts[ 0 ] ;
71
+
72
+ println ! (
73
+ "Found compressed token account: amount={}, owner={}" ,
74
+ ctoken_account. token. amount, ctoken_account. token. owner
75
+ ) ;
76
+
60
77
// Derive the address that will be created for deposit
61
78
let address_tree_info = rpc. get_address_tree_v1 ( ) ;
62
- let ( address , _) = derive_address (
63
- & [ b"deposit" , owner . pubkey ( ) . to_bytes ( ) . as_ref ( ) ] ,
79
+ let ( deposit_address , _) = derive_address (
80
+ & [ b"deposit" , payer . pubkey ( ) . to_bytes ( ) . as_ref ( ) ] ,
64
81
& address_tree_info. tree ,
65
82
& sdk_token_test:: ID ,
66
83
) ;
67
84
68
- // Create deposit instruction
69
- create_deposit_compressed_account ( & mut rpc, & payer, owner. pubkey ( ) , deposit_amount)
70
- . await
71
- . unwrap ( ) ;
85
+ // Derive recipient PDA from the deposit address
86
+ let ( recipient_pda, _) = Pubkey :: find_program_address (
87
+ & [ b"recipient" , deposit_address. as_ref ( ) ] ,
88
+ & sdk_token_test:: ID ,
89
+ ) ;
90
+
91
+ // Create deposit instruction with the compressed token account
92
+ create_deposit_compressed_account (
93
+ & mut rpc,
94
+ & payer,
95
+ ctoken_account,
96
+ recipient_pda,
97
+ deposit_amount,
98
+ )
99
+ . await
100
+ . unwrap ( ) ;
72
101
73
102
println ! ( "Created compressed account deposit successfully" ) ;
74
103
75
104
// Verify the compressed account was created at the expected address
76
105
let compressed_account = rpc
77
- . get_compressed_account ( address , None )
106
+ . get_compressed_account ( deposit_address , None )
78
107
. await
79
108
. unwrap ( )
80
109
. value ;
@@ -87,33 +116,45 @@ async fn test_deposit_compressed_account() {
87
116
async fn create_deposit_compressed_account (
88
117
rpc : & mut LightProgramTest ,
89
118
payer : & Keypair ,
90
- owner : Pubkey ,
119
+ ctoken_account : & CompressedTokenAccount ,
120
+ recipient : Pubkey ,
91
121
amount : u64 ,
92
122
) -> Result < Signature , RpcError > {
93
123
let tree_info = rpc. get_random_state_tree_info ( ) . unwrap ( ) ;
124
+ println ! ( "tree_info {:?}" , tree_info) ;
94
125
95
126
let mut remaining_accounts = PackedAccounts :: default ( ) ;
127
+ let config = TokenAccountsMetaConfig :: new_client ( ) ;
128
+ let metas = get_transfer_instruction_account_metas ( config) ;
129
+ println ! ( "metas {:?}" , metas) ;
130
+ println ! ( "metas len() {}" , metas. len( ) ) ;
131
+ remaining_accounts. add_pre_accounts_metas ( metas. as_slice ( ) ) ;
96
132
97
- let output_tree_index = tree_info
98
- . pack_output_tree_index ( & mut remaining_accounts)
99
- . unwrap ( ) ;
100
133
let config = SystemAccountMetaConfig :: new_with_cpi_context (
101
134
sdk_token_test:: ID ,
102
135
tree_info. cpi_context . unwrap ( ) ,
103
136
) ;
137
+ println ! ( "cpi_context {:?}" , config) ;
104
138
remaining_accounts. add_system_accounts ( config) ;
105
-
106
139
let address_tree_info = rpc. get_address_tree_v1 ( ) ;
107
140
108
141
let ( address, _) = derive_address (
109
- & [ b"deposit" , owner . to_bytes ( ) . as_ref ( ) ] ,
142
+ & [ b"deposit" , payer . pubkey ( ) . to_bytes ( ) . as_ref ( ) ] ,
110
143
& address_tree_info. tree ,
111
144
& sdk_token_test:: ID ,
112
145
) ;
113
146
147
+ // Get mint from the compressed token account
148
+ let mint = ctoken_account. token . mint ;
149
+ println ! (
150
+ "ctoken_account.account.hash {:?}" ,
151
+ ctoken_account. account. hash
152
+ ) ;
153
+ println ! ( "ctoken_account.account {:?}" , ctoken_account. account) ;
154
+ // Get validity proof for the compressed token account and new address
114
155
let rpc_result = rpc
115
156
. get_validity_proof (
116
- vec ! [ ] ,
157
+ vec ! [ ctoken_account . account . hash ] ,
117
158
vec ! [ AddressWithTree {
118
159
address,
119
160
tree: address_tree_info. tree,
@@ -123,9 +164,25 @@ async fn create_deposit_compressed_account(
123
164
. await ?
124
165
. value ;
125
166
let packed_accounts = rpc_result. pack_tree_infos ( & mut remaining_accounts) ;
167
+ println ! ( "packed_accounts {:?}" , packed_accounts. state_trees) ;
126
168
127
- let ( remaining_accounts, _, _) = remaining_accounts. to_account_metas ( ) ;
169
+ // Create token meta from compressed account
170
+ let tree_info = packed_accounts
171
+ . state_trees
172
+ . as_ref ( )
173
+ . unwrap ( )
174
+ . packed_tree_infos [ 0 ] ;
175
+
176
+ let token_metas = vec ! [ TokenAccountMeta {
177
+ amount: ctoken_account. token. amount,
178
+ delegate_index: None ,
179
+ packed_tree_info: tree_info,
180
+ lamports: None ,
181
+ tlv: None ,
182
+ } ] ;
128
183
184
+ let ( remaining_accounts, _, _) = remaining_accounts. to_account_metas ( ) ;
185
+ println ! ( "remaining_accounts {:?}" , remaining_accounts) ;
129
186
let instruction = Instruction {
130
187
program_id : sdk_token_test:: ID ,
131
188
accounts : [
@@ -136,8 +193,11 @@ async fn create_deposit_compressed_account(
136
193
data : sdk_token_test:: instruction:: Deposit {
137
194
proof : rpc_result. proof ,
138
195
address_tree_info : packed_accounts. address_trees [ 0 ] ,
139
- output_tree_index,
196
+ output_tree_index : packed_accounts . state_trees . unwrap ( ) . output_tree_index ,
140
197
deposit_amount : amount,
198
+ token_metas,
199
+ mint,
200
+ recipient,
141
201
}
142
202
. data ( ) ,
143
203
} ;
@@ -150,7 +210,7 @@ async fn batch_compress_spl_tokens(
150
210
rpc : & mut LightProgramTest ,
151
211
payer : & Keypair ,
152
212
recipients : Vec < Recipient > ,
153
- ) -> Result < Signature , RpcError > {
213
+ ) -> Result < Pubkey , RpcError > {
154
214
// Create mint and token account
155
215
let mint = create_mint_helper ( rpc, payer) . await ;
156
216
println ! ( "Created mint: {}" , mint) ;
@@ -214,5 +274,7 @@ async fn batch_compress_spl_tokens(
214
274
} ;
215
275
216
276
rpc. create_and_send_transaction ( & [ instruction] , & payer. pubkey ( ) , & [ payer] )
217
- . await
277
+ . await ?;
278
+
279
+ Ok ( mint)
218
280
}
0 commit comments