-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.rs
81 lines (72 loc) · 3 KB
/
processor.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
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
sysvar::Sysvar,
system_instruction,
};
use crate::instruction::SolanaInstruction;
pub fn process(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let instruction = SolanaInstruction::unpack(instruction_data)?;
match instruction {
SolanaInstruction::InitializeAccount => {
msg!("Instruction: InitializeAccount");
let accounts_iter = &mut accounts.iter();
let initializer = next_account_info(accounts_iter)?;
let account_to_initialize = next_account_info(accounts_iter)?;
let system_program = next_account_info(accounts_iter)?;
let rent_exemption = Rent::get()?.minimum_balance(0);
// Create the account with a balance sufficient to be rent-exempt
invoke_signed(
&system_instruction::create_account(
initializer.key,
account_to_initialize.key,
rent_exemption,
0, // No data storage, just storing SOL
program_id,
),
&[initializer.clone(), account_to_initialize.clone(), system_program.clone()],
&[],
)?;
},
SolanaInstruction::Deposit { amount } => {
if amount == 0 {
return Err(ProgramError::InvalidInstructionData);
}
msg!("Instruction: Deposit");
let accounts_iter = &mut accounts.iter();
let depositor = next_account_info(accounts_iter)?;
let account_to_deposit = next_account_info(accounts_iter)?;
let system_program = next_account_info(accounts_iter)?;
invoke(
&system_instruction::transfer(depositor.key, account_to_deposit.key, amount),
&[depositor.clone(), account_to_deposit.clone(), system_program.clone()],
)?;
},
SolanaInstruction::WithdrawTenPercent => {
msg!("Instruction: WithdrawTenPercent");
let accounts_iter = &mut accounts.iter();
let withdrawing_account = next_account_info(accounts_iter)?;
let destination_account = next_account_info(accounts_iter)?;
let system_program = next_account_info(accounts_iter)?;
let balance = **withdrawing_account.lamports.borrow();
let amount_to_withdraw = balance / 10;
if amount_to_withdraw == 0 {
return Err(ProgramError::InsufficientFunds);
}
invoke(
&system_instruction::transfer(withdrawing_account.key, destination_account.key, amount_to_withdraw),
&[withdrawing_account.clone(), destination_account.clone(), system_program.clone()],
)?;
}
}
Ok(())
}