Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 115c988

Browse files
committed
instruction: SyncNative
1 parent c97ed8f commit 115c988

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

program/src/entrypoint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::processor::{
2121
mint_to_checked::{process_mint_to_checked, MintToChecked},
2222
revoke::process_revoke,
2323
set_authority::{process_set_authority, SetAuthority},
24+
sync_native::process_sync_native,
2425
thaw_account::process_thaw_account,
2526
transfer::process_transfer,
2627
transfer_checked::{process_transfer_checked, TransferChecked},
@@ -194,6 +195,13 @@ pub fn process_instruction(
194195

195196
process_initialize_account2(program_id, accounts, owner)
196197
}
198+
// 17 - SyncNative
199+
Some((&17, _)) => {
200+
#[cfg(feature = "logging")]
201+
pinocchio::msg!("Instruction: SyncNative");
202+
203+
process_sync_native(program_id, accounts)
204+
}
197205
// 18 - InitializeAccount3
198206
Some((&18, data)) => {
199207
#[cfg(feature = "logging")]

program/src/processor/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod mint_to;
2323
pub mod mint_to_checked;
2424
pub mod revoke;
2525
pub mod set_authority;
26+
pub mod sync_native;
2627
pub mod thaw_account;
2728
pub mod transfer;
2829
pub mod transfer_checked;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use pinocchio::{
2+
account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult,
3+
};
4+
use token_interface::{error::TokenError, state::account::Account};
5+
6+
use super::check_account_owner;
7+
8+
pub fn process_sync_native(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
9+
let [native_account_info, _remaning @ ..] = accounts else {
10+
return Err(ProgramError::NotEnoughAccountKeys);
11+
};
12+
13+
check_account_owner(program_id, native_account_info)?;
14+
15+
let native_account = bytemuck::try_from_bytes_mut::<Account>(unsafe {
16+
native_account_info.borrow_mut_data_unchecked()
17+
})
18+
.map_err(|_error| ProgramError::InvalidAccountData)?;
19+
20+
if let Option::Some(rent_exempt_reserve) = native_account.is_native.get() {
21+
let new_amount = native_account_info
22+
.lamports()
23+
.checked_sub(u64::from(rent_exempt_reserve))
24+
.ok_or(TokenError::Overflow)?;
25+
26+
if new_amount < native_account.amount.into() {
27+
return Err(TokenError::InvalidState.into());
28+
}
29+
native_account.amount = new_amount.into();
30+
} else {
31+
return Err(TokenError::NonNativeNotSupported.into());
32+
}
33+
34+
Ok(())
35+
}

0 commit comments

Comments
 (0)