|
1 | | -use pinocchio::{ |
2 | | - account_info::AccountInfo, |
3 | | - program_error::ProgramError, |
4 | | - pubkey::Pubkey, |
5 | | - sysvars::{rent::Rent, Sysvar}, |
6 | | - ProgramResult, |
7 | | -}; |
8 | | -use std::mem::size_of; |
9 | | -use token_interface::{ |
10 | | - error::TokenError, |
11 | | - native_mint::is_native_mint, |
12 | | - state::{ |
13 | | - account::{Account, AccountState}, |
14 | | - mint::Mint, |
15 | | - PodCOption, |
16 | | - }, |
17 | | -}; |
| 1 | +use pinocchio::{account_info::AccountInfo, pubkey::Pubkey, ProgramResult}; |
18 | 2 |
|
19 | | -use super::check_account_owner; |
| 3 | +use super::shared; |
20 | 4 |
|
21 | | -pub fn process_initialize_account( |
22 | | - program_id: &Pubkey, |
23 | | - accounts: &[AccountInfo], |
24 | | - owner: Option<&Pubkey>, |
25 | | - rent_sysvar_account: bool, |
26 | | -) -> ProgramResult { |
27 | | - let (new_account_info, mint_info, owner, remaning) = if let Some(owner) = owner { |
28 | | - let [new_account_info, mint_info, remaning @ ..] = accounts else { |
29 | | - return Err(ProgramError::NotEnoughAccountKeys); |
30 | | - }; |
31 | | - (new_account_info, mint_info, owner, remaning) |
32 | | - } else { |
33 | | - let [new_account_info, mint_info, owner_info, remaning @ ..] = accounts else { |
34 | | - return Err(ProgramError::NotEnoughAccountKeys); |
35 | | - }; |
36 | | - (new_account_info, mint_info, owner_info.key(), remaning) |
37 | | - }; |
38 | | - |
39 | | - // Check rent-exempt status of the token account. |
40 | | - |
41 | | - let is_exempt = if rent_sysvar_account { |
42 | | - let rent_sysvar_info = remaning.first().ok_or(ProgramError::NotEnoughAccountKeys)?; |
43 | | - let rent = unsafe { Rent::from_bytes(rent_sysvar_info.borrow_data_unchecked()) }; |
44 | | - rent.is_exempt(new_account_info.lamports(), size_of::<Account>()) |
45 | | - } else { |
46 | | - Rent::get()?.is_exempt(new_account_info.lamports(), size_of::<Account>()) |
47 | | - }; |
48 | | - |
49 | | - if !is_exempt { |
50 | | - return Err(TokenError::NotRentExempt.into()); |
51 | | - } |
52 | | - |
53 | | - let account_data = unsafe { new_account_info.borrow_mut_data_unchecked() }; |
54 | | - let account = bytemuck::try_from_bytes_mut::<Account>(account_data) |
55 | | - .map_err(|_error| ProgramError::InvalidAccountData)?; |
56 | | - |
57 | | - if account.is_initialized() { |
58 | | - return Err(TokenError::AlreadyInUse.into()); |
59 | | - } |
60 | | - |
61 | | - let is_native_mint = is_native_mint(mint_info.key()); |
62 | | - |
63 | | - if !is_native_mint { |
64 | | - check_account_owner(program_id, mint_info)?; |
65 | | - |
66 | | - let mint_data = unsafe { mint_info.borrow_data_unchecked() }; |
67 | | - let mint = bytemuck::try_from_bytes::<Mint>(mint_data) |
68 | | - .map_err(|_error| ProgramError::InvalidAccountData)?; |
69 | | - |
70 | | - if !bool::from(mint.is_initialized) { |
71 | | - return Err(TokenError::InvalidMint.into()); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - account.mint = *mint_info.key(); |
76 | | - account.owner = *owner; |
77 | | - account.close_authority.clear(); |
78 | | - account.delegate.clear(); |
79 | | - account.delegated_amount = 0u64.into(); |
80 | | - account.state = AccountState::Initialized as u8; |
81 | | - |
82 | | - if is_native_mint { |
83 | | - let rent = Rent::get()?; |
84 | | - let rent_exempt_reserve = rent.minimum_balance(size_of::<Account>()); |
85 | | - |
86 | | - account.is_native = PodCOption::from(Some(rent_exempt_reserve.into())); |
87 | | - unsafe { |
88 | | - account.amount = new_account_info |
89 | | - .borrow_lamports_unchecked() |
90 | | - .checked_sub(rent_exempt_reserve) |
91 | | - .ok_or(TokenError::Overflow)? |
92 | | - .into() |
93 | | - } |
94 | | - } else { |
95 | | - account.is_native.clear(); |
96 | | - account.amount = 0u64.into(); |
97 | | - }; |
98 | | - |
99 | | - Ok(()) |
| 5 | +pub fn process_initialize_account(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult { |
| 6 | + shared::initialize_account::process_initialize_account(program_id, accounts, None, true) |
100 | 7 | } |
0 commit comments