-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsync_native.rs
More file actions
49 lines (43 loc) · 1.66 KB
/
sync_native.rs
File metadata and controls
49 lines (43 loc) · 1.66 KB
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
use {
super::check_account_owner,
pinocchio::{
account_info::AccountInfo,
program_error::ProgramError,
sysvars::{rent::Rent, Sysvar},
ProgramResult,
},
pinocchio_token_interface::{
error::TokenError,
state::{account::Account, load_mut},
},
};
#[inline(always)]
pub fn process_sync_native(accounts: &[AccountInfo]) -> ProgramResult {
let [native_account_info, remaining @ ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
check_account_owner(native_account_info)?;
let rent_exempt_reserve = if let Some(rent_sysvar_info) = remaining.first() {
// SAFETY: single immutable borrow to `rent_sysvar_info`; account ID and length
// are checked by `from_account_info_unchecked`.
let rent = unsafe { Rent::from_account_info_unchecked(rent_sysvar_info)? };
rent.minimum_balance(native_account_info.data_len())
} else {
Rent::get()?.minimum_balance(native_account_info.data_len())
};
// SAFETY: single mutable borrow to `native_account_info` account data and
// `load_mut` validates that the account is initialized.
let native_account =
unsafe { load_mut::<Account>(native_account_info.borrow_mut_data_unchecked())? };
if native_account.is_native() {
let new_amount = native_account_info
.lamports()
.checked_sub(rent_exempt_reserve)
.ok_or(TokenError::Overflow)?;
native_account.set_native_amount(rent_exempt_reserve);
native_account.set_amount(new_amount);
} else {
return Err(TokenError::NonNativeNotSupported.into());
}
Ok(())
}