-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstruction.rs
132 lines (113 loc) · 3.53 KB
/
instruction.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
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
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankInstruction;
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
program_error::ProgramError,
pubkey::Pubkey,
sysvar::Sysvar,
};
use crate::{
helpers::validate_pda,
state::{MyAccount, MyPdaAccount},
};
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct CreateMyAccountIx {
pub data: String,
}
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct CreateMyPdaAccountIx {
pub data: String,
pub bump: u8,
}
#[derive(BorshSerialize, BorshDeserialize, Debug, ShankInstruction)]
pub enum MyProgramInstruction {
#[account(
0,
writable,
signer,
name = "fee_payer_acc",
desc = "Fee payer account"
)]
#[account(1, writable, signer, name = "new_acc", desc = "New account")]
#[account(2, name = "system_program_acc", desc = "System program account")]
CreateAccount(CreateMyAccountIx),
#[account(
0,
writable,
signer,
name = "fee_payer_acc",
desc = "Fee payer account"
)]
#[account(1, writable, name = "new_pda_acc", desc = "New PDA account")]
#[account(2, name = "system_program_acc", desc = "System program account")]
CreatePDAAccount(CreateMyPdaAccountIx),
}
pub fn get_lamports_for_rent_exempt(size: u64) -> Result<u64, ProgramError> {
let rent = solana_program::rent::Rent::get()?;
let lamports = rent.minimum_balance(size as usize);
Ok(lamports)
}
pub fn create_my_account(
program_id: &Pubkey,
accounts: &[AccountInfo],
data: CreateMyAccountIx,
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let fee_payer_acc = next_account_info(accounts_iter)?;
let new_acc = next_account_info(accounts_iter)?;
let system_program_acc = next_account_info(accounts_iter)?;
let lamports = get_lamports_for_rent_exempt(MyAccount::size())?;
let create_account_ix = solana_program::system_instruction::create_account(
fee_payer_acc.key,
new_acc.key,
lamports,
MyAccount::size(),
program_id,
);
solana_program::program::invoke(
&create_account_ix,
&[
fee_payer_acc.clone(),
new_acc.clone(),
system_program_acc.clone(),
],
)?;
MyAccount::new(data, new_acc)?;
Ok(())
}
pub fn create_my_pda_account(
program_id: &Pubkey,
accounts: &[AccountInfo],
data: CreateMyPdaAccountIx,
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let fee_payer_acc = next_account_info(accounts_iter)?;
let new_pda_acc = next_account_info(accounts_iter)?;
let system_program_acc = next_account_info(accounts_iter)?;
let lamports = get_lamports_for_rent_exempt(MyPdaAccount::size())?;
let create_account_ix = solana_program::system_instruction::create_account(
fee_payer_acc.key,
new_pda_acc.key,
lamports,
MyPdaAccount::size(),
program_id,
);
let pda_acc_seeds = vec!["pda_accc".as_bytes(), fee_payer_acc.key.as_ref()];
validate_pda(pda_acc_seeds, new_pda_acc.key, data.bump, program_id)?;
solana_program::program::invoke_signed(
&create_account_ix,
&[
fee_payer_acc.clone(),
new_pda_acc.clone(),
system_program_acc.clone(),
],
&[&[
"pda_accc".as_bytes(),
fee_payer_acc.key.as_ref(),
&[data.bump],
]],
)?;
MyPdaAccount::new(data, new_pda_acc)?;
Ok(())
}