light_registry/
sdk.rs

1#![cfg(not(target_os = "solana"))]
2use account_compression::{self, ID};
3use anchor_lang::{system_program, InstructionData, ToAccountMetas};
4use solana_sdk::{
5    instruction::{AccountMeta, Instruction},
6    pubkey::Pubkey,
7};
8
9use crate::{
10    protocol_config::state::ProtocolConfig,
11    utils::{
12        get_cpi_authority_pda, get_epoch_pda_address, get_forester_epoch_pda_from_authority,
13        get_forester_epoch_pda_from_derivation, get_forester_pda, get_protocol_config_pda_address,
14    },
15    ForesterConfig,
16};
17pub fn create_initialize_group_authority_instruction(
18    signer_pubkey: Pubkey,
19    group_accounts: Pubkey,
20    seed_pubkey: Pubkey,
21    authority: Pubkey,
22) -> Instruction {
23    let instruction_data = account_compression::instruction::InitializeGroupAuthority { authority };
24
25    Instruction {
26        program_id: ID,
27        accounts: vec![
28            AccountMeta::new(signer_pubkey, true),
29            AccountMeta::new(seed_pubkey, true),
30            AccountMeta::new(group_accounts, false),
31            AccountMeta::new_readonly(system_program::ID, false),
32        ],
33        data: instruction_data.data(),
34    }
35}
36
37pub fn create_update_protocol_config_instruction(
38    signer_pubkey: Pubkey,
39    new_authority: Option<Pubkey>,
40    protocol_config: Option<ProtocolConfig>,
41) -> Instruction {
42    let protocol_config_pda = get_protocol_config_pda_address();
43    let update_authority_ix = crate::instruction::UpdateProtocolConfig { protocol_config };
44    let accounts = crate::accounts::UpdateProtocolConfig {
45        protocol_config_pda: protocol_config_pda.0,
46        authority: signer_pubkey,
47        fee_payer: signer_pubkey,
48        new_authority,
49    };
50
51    // update with new authority
52    Instruction {
53        program_id: crate::ID,
54        accounts: accounts.to_account_metas(Some(true)),
55        data: update_authority_ix.data(),
56    }
57}
58
59pub fn create_register_program_instruction(
60    signer_pubkey: Pubkey,
61    protocol_config_pda: (Pubkey, u8),
62    group_account: Pubkey,
63    program_id_to_be_registered: Pubkey,
64) -> (Instruction, Pubkey) {
65    let cpi_authority_pda = get_cpi_authority_pda();
66    let registered_program_pda =
67        Pubkey::find_program_address(&[program_id_to_be_registered.to_bytes().as_slice()], &ID).0;
68
69    let register_program_ix = crate::instruction::RegisterSystemProgram {
70        bump: cpi_authority_pda.1,
71    };
72    let register_program_accounts = crate::accounts::RegisterProgram {
73        authority: signer_pubkey,
74        program_to_be_registered: program_id_to_be_registered,
75        registered_program_pda,
76        protocol_config_pda: protocol_config_pda.0,
77        group_pda: group_account,
78        cpi_authority: cpi_authority_pda.0,
79        account_compression_program: ID,
80        system_program: system_program::ID,
81    };
82
83    let instruction = Instruction {
84        program_id: crate::ID,
85        accounts: register_program_accounts.to_account_metas(Some(true)),
86        data: register_program_ix.data(),
87    };
88    (instruction, registered_program_pda)
89}
90
91pub fn create_deregister_program_instruction(
92    signer_pubkey: Pubkey,
93    protocol_config_pda: (Pubkey, u8),
94    group_account: Pubkey,
95    program_id_to_be_deregistered: Pubkey,
96) -> (Instruction, Pubkey) {
97    let cpi_authority_pda = get_cpi_authority_pda();
98    let registered_program_pda =
99        Pubkey::find_program_address(&[program_id_to_be_deregistered.to_bytes().as_slice()], &ID).0;
100
101    let register_program_ix = crate::instruction::DeregisterSystemProgram {
102        bump: cpi_authority_pda.1,
103    };
104    let register_program_accounts = crate::accounts::DeregisterProgram {
105        authority: signer_pubkey,
106        registered_program_pda,
107        protocol_config_pda: protocol_config_pda.0,
108        group_pda: group_account,
109        cpi_authority: cpi_authority_pda.0,
110        account_compression_program: ID,
111    };
112
113    let instruction = Instruction {
114        program_id: crate::ID,
115        accounts: register_program_accounts.to_account_metas(Some(true)),
116        data: register_program_ix.data(),
117    };
118    (instruction, registered_program_pda)
119}
120
121pub fn create_initialize_governance_authority_instruction(
122    fee_payer: Pubkey,
123    authority: Pubkey,
124    protocol_config: ProtocolConfig,
125) -> Instruction {
126    let protocol_config_pda = get_protocol_config_pda_address();
127    let ix = crate::instruction::InitializeProtocolConfig {
128        bump: protocol_config_pda.1,
129        protocol_config,
130    };
131
132    let accounts = crate::accounts::InitializeProtocolConfig {
133        protocol_config_pda: protocol_config_pda.0,
134        fee_payer,
135        authority,
136        system_program: system_program::ID,
137        self_program: crate::ID,
138    };
139    Instruction {
140        program_id: crate::ID,
141        accounts: accounts.to_account_metas(Some(true)),
142        data: ix.data(),
143    }
144}
145
146pub fn create_register_forester_instruction(
147    fee_payer: &Pubkey,
148    governance_authority: &Pubkey,
149    forester_authority: &Pubkey,
150    config: ForesterConfig,
151) -> Instruction {
152    let (forester_pda, _bump) = get_forester_pda(forester_authority);
153    let instruction_data = crate::instruction::RegisterForester {
154        _bump,
155        authority: *forester_authority,
156        config,
157        weight: Some(1),
158    };
159    let (protocol_config_pda, _) = get_protocol_config_pda_address();
160    let accounts = crate::accounts::RegisterForester {
161        forester_pda,
162        fee_payer: *fee_payer,
163        authority: *governance_authority,
164        protocol_config_pda,
165        system_program: solana_sdk::system_program::id(),
166    };
167    Instruction {
168        program_id: crate::ID,
169        accounts: accounts.to_account_metas(Some(true)),
170        data: instruction_data.data(),
171    }
172}
173
174pub fn create_update_forester_pda_weight_instruction(
175    forester_authority: &Pubkey,
176    protocol_authority: &Pubkey,
177    new_weight: u64,
178) -> Instruction {
179    let (forester_pda, _bump) = get_forester_pda(forester_authority);
180    let protocol_config_pda = get_protocol_config_pda_address().0;
181    let instruction_data = crate::instruction::UpdateForesterPdaWeight { new_weight };
182    let accounts = crate::accounts::UpdateForesterPdaWeight {
183        forester_pda,
184        authority: *protocol_authority,
185        protocol_config_pda,
186    };
187    Instruction {
188        program_id: crate::ID,
189        accounts: accounts.to_account_metas(Some(true)),
190        data: instruction_data.data(),
191    }
192}
193
194pub fn create_update_forester_pda_instruction(
195    forester_authority: &Pubkey,
196    derivation_key: &Pubkey,
197    new_authority: Option<Pubkey>,
198    config: Option<ForesterConfig>,
199) -> Instruction {
200    let (forester_pda, _) = get_forester_pda(derivation_key);
201    let instruction_data = crate::instruction::UpdateForesterPda { config };
202    let accounts = crate::accounts::UpdateForesterPda {
203        forester_pda,
204        authority: *forester_authority,
205        new_authority,
206    };
207    Instruction {
208        program_id: crate::ID,
209        accounts: accounts.to_account_metas(Some(true)),
210        data: instruction_data.data(),
211    }
212}
213
214pub fn create_register_forester_epoch_pda_instruction(
215    authority: &Pubkey,
216    derivation: &Pubkey,
217    epoch: u64,
218) -> Instruction {
219    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_authority(derivation, epoch);
220    let (forester_pda, _) = get_forester_pda(derivation);
221    let epoch_pda = get_epoch_pda_address(epoch);
222    let protocol_config_pda = get_protocol_config_pda_address().0;
223    let instruction_data = crate::instruction::RegisterForesterEpoch { epoch };
224    let accounts = crate::accounts::RegisterForesterEpoch {
225        fee_payer: *authority,
226        forester_epoch_pda,
227        forester_pda,
228        authority: *authority,
229        epoch_pda,
230        protocol_config: protocol_config_pda,
231        system_program: solana_sdk::system_program::id(),
232    };
233    Instruction {
234        program_id: crate::ID,
235        accounts: accounts.to_account_metas(Some(true)),
236        data: instruction_data.data(),
237    }
238}
239
240pub fn create_finalize_registration_instruction(
241    authority: &Pubkey,
242    derivation: &Pubkey,
243    epoch: u64,
244) -> Instruction {
245    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_derivation(derivation, epoch);
246    let epoch_pda = get_epoch_pda_address(epoch);
247    let instruction_data = crate::instruction::FinalizeRegistration {};
248    let accounts = crate::accounts::FinalizeRegistration {
249        forester_epoch_pda,
250        authority: *authority,
251        epoch_pda,
252    };
253    Instruction {
254        program_id: crate::ID,
255        accounts: accounts.to_account_metas(Some(true)),
256        data: instruction_data.data(),
257    }
258}
259
260pub fn create_report_work_instruction(
261    authority: &Pubkey,
262    derivation: &Pubkey,
263    epoch: u64,
264) -> Instruction {
265    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_authority(derivation, epoch);
266    let epoch_pda = get_epoch_pda_address(epoch);
267    let instruction_data = crate::instruction::ReportWork {};
268    let accounts = crate::accounts::ReportWork {
269        authority: *authority,
270        forester_epoch_pda,
271        epoch_pda,
272    };
273    Instruction {
274        program_id: crate::ID,
275        accounts: accounts.to_account_metas(Some(true)),
276        data: instruction_data.data(),
277    }
278}