1
- use crate :: account_infos:: generic_struct:: AccountInfoIndexGetter ;
1
+ use light_account_checks:: AccountInfoTrait ;
2
+
3
+ use crate :: {
4
+ account_infos:: MintToAccountInfosConfig ,
5
+ error:: { LightTokenSdkTypeError , Result } ,
6
+ } ;
2
7
3
8
#[ repr( usize ) ]
4
9
pub enum BatchCompressAccountInfosIndex {
5
- FeePayer ,
6
- Authority ,
10
+ // FeePayer,
11
+ // Authority,
7
12
CpiAuthorityPda ,
8
- Mint ,
9
13
TokenPoolPda ,
10
14
TokenProgram ,
11
15
LightSystemProgram ,
@@ -16,59 +20,173 @@ pub enum BatchCompressAccountInfosIndex {
16
20
MerkleTree ,
17
21
SelfProgram ,
18
22
SystemProgram ,
23
+ SolPoolPda ,
24
+ SenderTokenAccount ,
25
+ }
26
+
27
+ pub struct BatchCompressAccountInfos < ' a , T : AccountInfoTrait + Clone > {
28
+ fee_payer : & ' a T ,
29
+ authority : & ' a T ,
30
+ accounts : & ' a [ T ] ,
31
+ config : MintToAccountInfosConfig ,
19
32
}
20
33
21
- impl AccountInfoIndexGetter for BatchCompressAccountInfosIndex {
22
- const SYSTEM_ACCOUNTS_LEN : usize = 14 ;
23
-
24
- fn cpi_authority_index ( ) -> usize {
25
- BatchCompressAccountInfosIndex :: CpiAuthorityPda as usize
34
+ impl < ' a , T : AccountInfoTrait + Clone > BatchCompressAccountInfos < ' a , T > {
35
+ pub fn new ( fee_payer : & ' a T , authority : & ' a T , accounts : & ' a [ T ] ) -> Self {
36
+ Self {
37
+ fee_payer,
38
+ authority,
39
+ accounts,
40
+ config : MintToAccountInfosConfig :: new_batch_compress ( ) ,
41
+ }
26
42
}
27
43
28
- fn light_system_program_index ( ) -> usize {
29
- BatchCompressAccountInfosIndex :: LightSystemProgram as usize
44
+ pub fn new_with_config (
45
+ fee_payer : & ' a T ,
46
+ authority : & ' a T ,
47
+ accounts : & ' a [ T ] ,
48
+ config : MintToAccountInfosConfig ,
49
+ ) -> Self {
50
+ Self {
51
+ fee_payer,
52
+ authority,
53
+ accounts,
54
+ config,
55
+ }
30
56
}
31
57
32
- fn registered_program_pda_index ( ) -> usize {
33
- BatchCompressAccountInfosIndex :: RegisteredProgramPda as usize
58
+ pub fn fee_payer ( & self ) -> & ' a T {
59
+ self . fee_payer
34
60
}
35
61
36
- fn noop_program_index ( ) -> usize {
37
- BatchCompressAccountInfosIndex :: NoopProgram as usize
62
+ pub fn authority ( & self ) -> & ' a T {
63
+ self . authority
38
64
}
39
65
40
- fn account_compression_authority_index ( ) -> usize {
41
- BatchCompressAccountInfosIndex :: AccountCompressionAuthority as usize
66
+ pub fn cpi_authority_pda ( & self ) -> Result < & ' a T > {
67
+ let index = BatchCompressAccountInfosIndex :: CpiAuthorityPda as usize ;
68
+ self . accounts
69
+ . get ( index)
70
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
42
71
}
43
72
44
- fn account_compression_program_index ( ) -> usize {
45
- BatchCompressAccountInfosIndex :: AccountCompressionProgram as usize
73
+ pub fn token_pool_pda ( & self ) -> Result < & ' a T > {
74
+ let index = BatchCompressAccountInfosIndex :: TokenPoolPda as usize ;
75
+ self . accounts
76
+ . get ( index)
77
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
46
78
}
47
79
48
- fn ctoken_program_index ( ) -> usize {
49
- BatchCompressAccountInfosIndex :: SelfProgram as usize
80
+ pub fn token_program ( & self ) -> Result < & ' a T > {
81
+ let index = BatchCompressAccountInfosIndex :: TokenProgram as usize ;
82
+ self . accounts
83
+ . get ( index)
84
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
50
85
}
51
86
52
- fn token_pool_pda_index ( ) -> usize {
53
- BatchCompressAccountInfosIndex :: TokenPoolPda as usize
87
+ pub fn light_system_program ( & self ) -> Result < & ' a T > {
88
+ let index = BatchCompressAccountInfosIndex :: LightSystemProgram as usize ;
89
+ self . accounts
90
+ . get ( index)
91
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
54
92
}
55
93
56
- fn decompression_recipient_index ( ) -> usize {
57
- // BatchCompress doesn't use decompression recipient
58
- 0
94
+ pub fn registered_program_pda ( & self ) -> Result < & ' a T > {
95
+ let index = BatchCompressAccountInfosIndex :: RegisteredProgramPda as usize ;
96
+ self . accounts
97
+ . get ( index)
98
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
59
99
}
60
100
61
- fn spl_token_program_index ( ) -> usize {
62
- BatchCompressAccountInfosIndex :: TokenProgram as usize
101
+ pub fn noop_program ( & self ) -> Result < & ' a T > {
102
+ let index = BatchCompressAccountInfosIndex :: NoopProgram as usize ;
103
+ self . accounts
104
+ . get ( index)
105
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
63
106
}
64
107
65
- fn system_program_index ( ) -> usize {
66
- BatchCompressAccountInfosIndex :: SystemProgram as usize
108
+ pub fn account_compression_authority ( & self ) -> Result < & ' a T > {
109
+ let index = BatchCompressAccountInfosIndex :: AccountCompressionAuthority as usize ;
110
+ self . accounts
111
+ . get ( index)
112
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
67
113
}
68
114
69
- fn cpi_context_index ( ) -> usize {
70
- // BatchCompress doesn't use cpi context
71
- 0
115
+ pub fn account_compression_program ( & self ) -> Result < & ' a T > {
116
+ let index = BatchCompressAccountInfosIndex :: AccountCompressionProgram as usize ;
117
+ self . accounts
118
+ . get ( index)
119
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
120
+ }
121
+
122
+ pub fn merkle_tree ( & self ) -> Result < & ' a T > {
123
+ let index = BatchCompressAccountInfosIndex :: MerkleTree as usize ;
124
+ self . accounts
125
+ . get ( index)
126
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
127
+ }
128
+
129
+ pub fn self_program ( & self ) -> Result < & ' a T > {
130
+ let index = BatchCompressAccountInfosIndex :: SelfProgram as usize ;
131
+ self . accounts
132
+ . get ( index)
133
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
134
+ }
135
+
136
+ pub fn system_program ( & self ) -> Result < & ' a T > {
137
+ let index = BatchCompressAccountInfosIndex :: SystemProgram as usize ;
138
+ self . accounts
139
+ . get ( index)
140
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
141
+ }
142
+
143
+ pub fn sol_pool_pda ( & self ) -> Result < & ' a T > {
144
+ if !self . config . has_sol_pool_pda {
145
+ return Err ( LightTokenSdkTypeError :: SolPoolPdaUndefined ) ;
146
+ }
147
+ let index = BatchCompressAccountInfosIndex :: SolPoolPda as usize ;
148
+ self . accounts
149
+ . get ( index)
150
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
72
151
}
73
- }
74
152
153
+ pub fn sender_token_account ( & self ) -> Result < & ' a T > {
154
+ let mut index = BatchCompressAccountInfosIndex :: SenderTokenAccount as usize ;
155
+ if !self . config . has_sol_pool_pda {
156
+ index -= 1 ;
157
+ }
158
+ self . accounts
159
+ . get ( index)
160
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
161
+ }
162
+
163
+ pub fn get_account_info ( & self , index : usize ) -> Result < & ' a T > {
164
+ self . accounts
165
+ . get ( index)
166
+ . ok_or ( LightTokenSdkTypeError :: CpiAccountsIndexOutOfBounds ( index) )
167
+ }
168
+ pub fn to_account_infos ( & self ) -> Vec < T > {
169
+ [
170
+ vec ! [ self . fee_payer. clone( ) ] ,
171
+ vec ! [ self . authority. clone( ) ] ,
172
+ self . accounts . to_vec ( ) ,
173
+ ]
174
+ . concat ( )
175
+ }
176
+
177
+ pub fn account_infos ( & self ) -> & ' a [ T ] {
178
+ self . accounts
179
+ }
180
+
181
+ pub fn config ( & self ) -> & MintToAccountInfosConfig {
182
+ & self . config
183
+ }
184
+
185
+ pub fn system_accounts_len ( & self ) -> usize {
186
+ let mut len = 13 ; // Base accounts from the enum (including sender_token_account)
187
+ if !self . config . has_sol_pool_pda {
188
+ len -= 1 ; // Remove sol_pool_pda if it's None
189
+ }
190
+ len
191
+ }
192
+ }
0 commit comments