1
1
use borsh:: { BorshDeserialize , BorshSerialize } ;
2
2
use light_sdk:: {
3
- account:: LightAccount ,
4
- cpi:: { CpiAccounts , CpiAccountsConfig , CpiInputs } ,
5
3
error:: LightSdkError ,
6
- instruction:: ValidityProof ,
4
+ instruction:: { account_meta :: CompressedAccountMeta , ValidityProof } ,
7
5
} ;
8
6
use solana_program:: {
9
- account_info:: AccountInfo , clock:: Clock , msg, program_error:: ProgramError , pubkey:: Pubkey ,
10
- sysvar:: Sysvar ,
7
+ account_info:: AccountInfo , clock:: Clock , msg, pubkey:: Pubkey , sysvar:: Sysvar ,
11
8
} ;
12
9
13
- use crate :: { create_pda:: MyCompressedAccount , decompress_to_pda:: DecompressedPdaAccount } ;
10
+ use crate :: {
11
+ create_pda:: MyCompressedAccount , decompress_to_pda:: DecompressedPdaAccount ,
12
+ sdk:: compress_pda:: compress_pda,
13
+ } ;
14
14
15
15
/// Compresses a PDA back into a compressed account
16
16
/// Anyone can call this after the timeout period has elapsed
17
- pub fn compress_from_pda (
18
- accounts : & [ AccountInfo ] ,
17
+ /// pda check missing yet.
18
+ pub fn compress_from_pda < ' a > (
19
+ accounts : & ' a [ AccountInfo < ' a > ] ,
19
20
instruction_data : & [ u8 ] ,
20
21
) -> Result < ( ) , LightSdkError > {
21
22
msg ! ( "Compressing PDA back to compressed account" ) ;
@@ -27,104 +28,25 @@ pub fn compress_from_pda(
27
28
// Get accounts
28
29
let fee_payer = & accounts[ 0 ] ;
29
30
let pda_account = & accounts[ 1 ] ;
30
- let rent_recipient = & accounts[ 2 ] ; // Hardcoded by caller program
31
- let _system_program = & accounts[ 3 ] ;
31
+ let rent_recipient = & accounts[ 2 ] ; // can be hardcoded by caller program
32
32
33
33
// Verify the PDA account is owned by our program
34
34
if pda_account. owner != & crate :: ID {
35
35
msg ! ( "PDA account not owned by this program" ) ;
36
36
return Err ( LightSdkError :: ConstraintViolation ) ;
37
37
}
38
38
39
- // Read and deserialize PDA data
40
- let pda_data = pda_account. try_borrow_data ( ) ?;
41
-
42
- // Check discriminator
43
- if & pda_data[ ..8 ] != b"decomppd" {
44
- msg ! ( "Invalid PDA discriminator" ) ;
45
- return Err ( LightSdkError :: ConstraintViolation ) ;
46
- }
47
-
48
- let decompressed_pda = DecompressedPdaAccount :: deserialize ( & mut & pda_data[ 8 ..] )
49
- . map_err ( |_| LightSdkError :: Borsh ) ?;
50
-
51
- // Check if enough time has passed
52
- let clock = Clock :: get ( ) . map_err ( |_| LightSdkError :: Borsh ) ?;
53
- let current_slot = clock. slot ;
54
- let slots_elapsed = current_slot. saturating_sub ( decompressed_pda. last_written_slot ) ;
55
-
56
- if slots_elapsed < decompressed_pda. slots_until_compression {
57
- msg ! (
58
- "Cannot compress yet. {} slots remaining" ,
59
- decompressed_pda
60
- . slots_until_compression
61
- . saturating_sub( slots_elapsed)
62
- ) ;
63
- return Err ( LightSdkError :: ConstraintViolation ) ;
64
- }
65
-
66
- // Derive PDA to verify it matches
67
- let ( pda_pubkey, _pda_bump) = Pubkey :: find_program_address (
68
- & [
69
- b"decompressed_pda" ,
70
- & decompressed_pda. compressed_address ,
71
- & instruction_data. additional_seed ,
72
- ] ,
73
- & crate :: ID ,
74
- ) ;
75
-
76
- if pda_pubkey != * pda_account. key {
77
- msg ! ( "PDA derivation mismatch" ) ;
78
- return Err ( LightSdkError :: ConstraintViolation ) ;
79
- }
80
-
81
- // Drop the borrow before we close the account
82
- drop ( pda_data) ;
83
-
84
- // Close the PDA account and send rent to recipient
85
- let pda_lamports = pda_account. lamports ( ) ;
86
- * * pda_account. try_borrow_mut_lamports ( ) ? = 0 ;
87
- * * rent_recipient. try_borrow_mut_lamports ( ) ? = rent_recipient
88
- . lamports ( )
89
- . checked_add ( pda_lamports)
90
- . ok_or ( ProgramError :: ArithmeticOverflow ) ?;
91
-
92
- // Clear the PDA data
93
- pda_account. try_borrow_mut_data ( ) ?. fill ( 0 ) ;
94
-
95
- // Now create the compressed account with the latest data
96
- let mut compressed_account = LightAccount :: < ' _ , MyCompressedAccount > :: new_init (
97
- & crate :: ID ,
98
- Some ( decompressed_pda. compressed_address ) ,
99
- instruction_data. output_merkle_tree_index ,
100
- ) ;
101
-
102
- compressed_account. data = decompressed_pda. data ;
103
-
104
- // Set up CPI accounts for light system program
105
- let mut config = CpiAccountsConfig :: new ( crate :: LIGHT_CPI_SIGNER ) ;
106
- config. sol_pool_pda = true ; // We're compressing SOL
107
-
108
- let cpi_accounts = CpiAccounts :: new_with_config (
39
+ compress_pda :: < MyCompressedAccount > (
40
+ pda_account,
41
+ & instruction_data. compressed_account_meta ,
42
+ Some ( instruction_data. proof ) ,
43
+ accounts,
44
+ instruction_data. system_accounts_offset ,
109
45
fee_payer,
110
- & accounts[ instruction_data. system_accounts_offset as usize ..] ,
111
- config,
112
- ) ;
113
-
114
- // Create CPI inputs
115
- let mut cpi_inputs = CpiInputs :: new_with_address (
116
- instruction_data. proof ,
117
- vec ! [ compressed_account. to_account_info( ) ?] ,
118
- vec ! [ instruction_data. new_address_params] ,
119
- ) ;
120
-
121
- // Set compression parameters
122
- // We're compressing the lamports that were in the PDA
123
- cpi_inputs. compress_or_decompress_lamports = Some ( instruction_data. lamports_to_compress ) ;
124
- cpi_inputs. is_compress = true ;
125
-
126
- // Invoke light system program
127
- cpi_inputs. invoke_light_system_program ( cpi_accounts) ?;
46
+ crate :: LIGHT_CPI_SIGNER ,
47
+ & crate :: ID ,
48
+ rent_recipient,
49
+ ) ?;
128
50
129
51
msg ! ( "Successfully compressed PDA back to compressed account" ) ;
130
52
Ok ( ( ) )
@@ -133,9 +55,6 @@ pub fn compress_from_pda(
133
55
#[ derive( Clone , Debug , Default , BorshDeserialize , BorshSerialize ) ]
134
56
pub struct CompressFromPdaInstructionData {
135
57
pub proof : ValidityProof ,
136
- pub new_address_params : light_sdk:: address:: PackedNewAddressParams ,
137
- pub output_merkle_tree_index : u8 ,
138
- pub additional_seed : [ u8 ; 32 ] , // Must match the seed used in decompression
139
- pub lamports_to_compress : u64 ,
58
+ pub compressed_account_meta : CompressedAccountMeta ,
140
59
pub system_accounts_offset : u8 ,
141
60
}
0 commit comments