1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#![cfg(not(target_os = "solana"))]
use crate::{
    protocol_config::state::ProtocolConfig,
    utils::{
        get_cpi_authority_pda, get_epoch_pda_address, get_forester_epoch_pda_from_authority,
        get_forester_pda, get_protocol_config_pda_address,
    },
    ForesterConfig,
};
use account_compression::{self, ID};
use anchor_lang::{system_program, InstructionData, ToAccountMetas};
use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};
pub fn create_initialize_group_authority_instruction(
    signer_pubkey: Pubkey,
    group_accounts: Pubkey,
    seed_pubkey: Pubkey,
    authority: Pubkey,
) -> Instruction {
    let instruction_data = account_compression::instruction::InitializeGroupAuthority { authority };

    Instruction {
        program_id: ID,
        accounts: vec![
            AccountMeta::new(signer_pubkey, true),
            AccountMeta::new(seed_pubkey, true),
            AccountMeta::new(group_accounts, false),
            AccountMeta::new_readonly(system_program::ID, false),
        ],
        data: instruction_data.data(),
    }
}

pub fn create_update_protocol_config_instruction(
    signer_pubkey: Pubkey,
    new_authority: Option<Pubkey>,
    protocol_config: Option<ProtocolConfig>,
) -> Instruction {
    let protocol_config_pda = get_protocol_config_pda_address();
    let update_authority_ix = crate::instruction::UpdateProtocolConfig { protocol_config };
    let accounts = crate::accounts::UpdateProtocolConfig {
        protocol_config_pda: protocol_config_pda.0,
        authority: signer_pubkey,
        fee_payer: signer_pubkey,
        new_authority,
    };

    // update with new authority
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: update_authority_ix.data(),
    }
}

pub fn create_register_program_instruction(
    signer_pubkey: Pubkey,
    protocol_config_pda: (Pubkey, u8),
    group_account: Pubkey,
    program_id_to_be_registered: Pubkey,
) -> (Instruction, Pubkey) {
    let cpi_authority_pda = get_cpi_authority_pda();
    let registered_program_pda =
        Pubkey::find_program_address(&[program_id_to_be_registered.to_bytes().as_slice()], &ID).0;

    let register_program_ix = crate::instruction::RegisterSystemProgram {
        bump: cpi_authority_pda.1,
    };
    let register_program_accounts = crate::accounts::RegisterProgram {
        authority: signer_pubkey,
        program_to_be_registered: program_id_to_be_registered,
        registered_program_pda,
        protocol_config_pda: protocol_config_pda.0,
        group_pda: group_account,
        cpi_authority: cpi_authority_pda.0,
        account_compression_program: ID,
        system_program: system_program::ID,
    };

    let instruction = Instruction {
        program_id: crate::ID,
        accounts: register_program_accounts.to_account_metas(Some(true)),
        data: register_program_ix.data(),
    };
    (instruction, registered_program_pda)
}

pub fn create_deregister_program_instruction(
    signer_pubkey: Pubkey,
    protocol_config_pda: (Pubkey, u8),
    group_account: Pubkey,
    program_id_to_be_deregistered: Pubkey,
) -> (Instruction, Pubkey) {
    let cpi_authority_pda = get_cpi_authority_pda();
    let registered_program_pda =
        Pubkey::find_program_address(&[program_id_to_be_deregistered.to_bytes().as_slice()], &ID).0;

    let register_program_ix = crate::instruction::DeregisterSystemProgram {
        bump: cpi_authority_pda.1,
    };
    let register_program_accounts = crate::accounts::DeregisterProgram {
        authority: signer_pubkey,
        registered_program_pda,
        protocol_config_pda: protocol_config_pda.0,
        group_pda: group_account,
        cpi_authority: cpi_authority_pda.0,
        account_compression_program: ID,
    };

    let instruction = Instruction {
        program_id: crate::ID,
        accounts: register_program_accounts.to_account_metas(Some(true)),
        data: register_program_ix.data(),
    };
    (instruction, registered_program_pda)
}

pub fn create_initialize_governance_authority_instruction(
    fee_payer: Pubkey,
    authority: Pubkey,
    protocol_config: ProtocolConfig,
) -> Instruction {
    let protocol_config_pda = get_protocol_config_pda_address();
    let ix = crate::instruction::InitializeProtocolConfig {
        bump: protocol_config_pda.1,
        protocol_config,
    };

    let accounts = crate::accounts::InitializeProtocolConfig {
        protocol_config_pda: protocol_config_pda.0,
        fee_payer,
        authority,
        system_program: system_program::ID,
        self_program: crate::ID,
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: ix.data(),
    }
}

pub fn create_register_forester_instruction(
    fee_payer: &Pubkey,
    governance_authority: &Pubkey,
    forester_authority: &Pubkey,
    config: ForesterConfig,
) -> Instruction {
    let (forester_pda, _bump) = get_forester_pda(forester_authority);
    let instruction_data = crate::instruction::RegisterForester {
        _bump,
        authority: *forester_authority,
        config,
        weight: Some(1),
    };
    let (protocol_config_pda, _) = get_protocol_config_pda_address();
    let accounts = crate::accounts::RegisterForester {
        forester_pda,
        fee_payer: *fee_payer,
        authority: *governance_authority,
        protocol_config_pda,
        system_program: solana_sdk::system_program::id(),
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}

pub fn create_update_forester_pda_weight_instruction(
    forester_authority: &Pubkey,
    protocol_authority: &Pubkey,
    new_weight: u64,
) -> Instruction {
    let (forester_pda, _bump) = get_forester_pda(forester_authority);
    let protocol_config_pda = get_protocol_config_pda_address().0;
    let instruction_data = crate::instruction::UpdateForesterPdaWeight { new_weight };
    let accounts = crate::accounts::UpdateForesterPdaWeight {
        forester_pda,
        authority: *protocol_authority,
        protocol_config_pda,
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}

pub fn create_update_forester_pda_instruction(
    forester_authority: &Pubkey,
    derivation_key: &Pubkey,
    new_authority: Option<Pubkey>,
    config: Option<ForesterConfig>,
) -> Instruction {
    let (forester_pda, _) = get_forester_pda(derivation_key);
    let instruction_data = crate::instruction::UpdateForesterPda { config };
    let accounts = crate::accounts::UpdateForesterPda {
        forester_pda,
        authority: *forester_authority,
        new_authority,
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}

pub fn create_register_forester_epoch_pda_instruction(
    authority: &Pubkey,
    epoch: u64,
) -> Instruction {
    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_authority(authority, epoch);
    let (forester_pda, _) = get_forester_pda(authority);
    let epoch_pda = get_epoch_pda_address(epoch);
    let protocol_config_pda = get_protocol_config_pda_address().0;
    let instruction_data = crate::instruction::RegisterForesterEpoch { epoch };
    let accounts = crate::accounts::RegisterForesterEpoch {
        fee_payer: *authority,
        forester_epoch_pda,
        forester_pda,
        authority: *authority,
        epoch_pda,
        protocol_config: protocol_config_pda,
        system_program: solana_sdk::system_program::id(),
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}

pub fn create_finalize_registration_instruction(authority: &Pubkey, epoch: u64) -> Instruction {
    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_authority(authority, epoch);
    let epoch_pda = get_epoch_pda_address(epoch);
    let instruction_data = crate::instruction::FinalizeRegistration {};
    let accounts = crate::accounts::FinalizeRegistration {
        forester_epoch_pda,
        authority: *authority,
        epoch_pda,
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}

pub fn create_report_work_instruction(authority: &Pubkey, epoch: u64) -> Instruction {
    let (forester_epoch_pda, _bump) = get_forester_epoch_pda_from_authority(authority, epoch);
    let epoch_pda = get_epoch_pda_address(epoch);
    let instruction_data = crate::instruction::ReportWork {};
    let accounts = crate::accounts::ReportWork {
        authority: *authority,
        forester_epoch_pda,
        epoch_pda,
    };
    Instruction {
        program_id: crate::ID,
        accounts: accounts.to_account_metas(Some(true)),
        data: instruction_data.data(),
    }
}