-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.rs
50 lines (45 loc) · 1.49 KB
/
initialize.rs
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
use ephemeral_vrf_api::prelude::*;
use steel::*;
/// Process the initialization of the EphemeralVrf program
///
/// Accounts:
///
/// 0; `[signer]` The authority that initializes the program
/// 1; `[]` The oracles account (PDA to be created)
/// 2; `[]` The system program
///
/// Requirements:
///
/// - The authority (account 0) must be a signer.
/// - The oracles account (account 1) must be empty and use the correct seeds ([ORACLES]).
///
/// 1. Parse the instruction data and extract arguments (Initialize).
/// 2. Create the oracles PDA.
/// 3. Write the default Oracles data to the new PDA.
pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let [signer_info, oracles_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
oracles_info
.is_empty()?
.is_writable()?
.has_seeds(&[ORACLES], &ephemeral_vrf_api::ID)?;
system_program.is_program(&system_program::ID)?;
let mut oracles_bytes = vec![];
let oracles = Oracles::default();
oracles.to_bytes_with_discriminator(&mut oracles_bytes)?;
create_pda(
oracles_info,
&ephemeral_vrf_api::ID,
oracles_bytes.len(),
&[ORACLES],
oracles_pda().1,
system_program,
signer_info,
)?;
let mut oracles_data = oracles_info.try_borrow_mut_data()?;
oracles_data.copy_from_slice(&oracles_bytes);
Ok(())
}