1pub mod loaders;
2#[cfg(feature = "spl")]
3pub mod spl;
4
5use solana_program::{
6 account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
7 pubkey::Pubkey, rent::Rent, sysvar::Sysvar,
8};
9
10#[inline(always)]
12pub fn create_pda<'a, 'info>(
13 target_account: &'a AccountInfo<'info>,
14 owner: &Pubkey,
15 space: usize,
16 pda_seeds: &[&[u8]],
17 system_program: &'a AccountInfo<'info>,
18 payer: &'a AccountInfo<'info>,
19) -> ProgramResult {
20 let rent = Rent::get()?;
21 if target_account.lamports().eq(&0) {
22 solana_program::program::invoke_signed(
24 &solana_program::system_instruction::create_account(
25 payer.key,
26 target_account.key,
27 rent.minimum_balance(space),
28 space as u64,
29 owner,
30 ),
31 &[
32 payer.clone(),
33 target_account.clone(),
34 system_program.clone(),
35 ],
36 &[pda_seeds],
37 )?;
38 } else {
39 let rent_exempt_balance = rent
43 .minimum_balance(space)
44 .saturating_sub(target_account.lamports());
45 if rent_exempt_balance.gt(&0) {
46 solana_program::program::invoke(
47 &solana_program::system_instruction::transfer(
48 payer.key,
49 target_account.key,
50 rent_exempt_balance,
51 ),
52 &[
53 payer.clone(),
54 target_account.clone(),
55 system_program.clone(),
56 ],
57 )?;
58 }
59
60 solana_program::program::invoke_signed(
62 &solana_program::system_instruction::allocate(target_account.key, space as u64),
63 &[target_account.clone(), system_program.clone()],
64 &[pda_seeds],
65 )?;
66
67 solana_program::program::invoke_signed(
69 &solana_program::system_instruction::assign(target_account.key, owner),
70 &[target_account.clone(), system_program.clone()],
71 &[pda_seeds],
72 )?;
73 }
74
75 Ok(())
76}
77
78pub trait Discriminator {
79 fn discriminator() -> u8;
80}
81
82pub trait AccountDeserialize {
83 fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError>;
84 fn try_from_bytes_mut(data: &mut [u8]) -> Result<&mut Self, ProgramError>;
85}
86
87#[macro_export]
88macro_rules! impl_to_bytes {
89 ($struct_name:ident) => {
90 impl $struct_name {
91 pub fn to_bytes(&self) -> &[u8] {
92 bytemuck::bytes_of(self)
93 }
94 }
95 };
96}
97
98#[macro_export]
99macro_rules! impl_account_from_bytes {
100 ($struct_name:ident) => {
101 impl crate::utils::AccountDeserialize for $struct_name {
102 fn try_from_bytes(
103 data: &[u8],
104 ) -> Result<&Self, solana_program::program_error::ProgramError> {
105 if Self::discriminator().ne(&data[0]) {
106 return Err(solana_program::program_error::ProgramError::InvalidAccountData);
107 }
108 bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
109 solana_program::program_error::ProgramError::InvalidAccountData,
110 ))
111 }
112 fn try_from_bytes_mut(
113 data: &mut [u8],
114 ) -> Result<&mut Self, solana_program::program_error::ProgramError> {
115 if Self::discriminator().ne(&data[0]) {
116 return Err(solana_program::program_error::ProgramError::InvalidAccountData);
117 }
118 bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
119 solana_program::program_error::ProgramError::InvalidAccountData,
120 ))
121 }
122 }
123 };
124}
125
126#[macro_export]
127macro_rules! impl_instruction_from_bytes {
128 ($struct_name:ident) => {
129 impl $struct_name {
130 pub fn try_from_bytes(
131 data: &[u8],
132 ) -> Result<&Self, solana_program::program_error::ProgramError> {
133 bytemuck::try_from_bytes::<Self>(data).or(Err(
134 solana_program::program_error::ProgramError::InvalidInstructionData,
135 ))
136 }
137 }
138 };
139}