Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ solana-program-option = "3.0.0"
solana-program-pack = "3.0.0"
solana-pubkey = "3.0.0"
solana-system-interface = { version = "2.0", features = ["bincode"] }
spl-token-interface = { path = "interface" }
14 changes: 14 additions & 0 deletions interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,20 @@ pub fn sync_native(
})
}

/// Creates a `SyncNative` instruction with the Rent sysvar account
/// added to the accounts list.
pub fn sync_native_with_rent_sysvar(
token_program_id: &Pubkey,
account_pubkey: &Pubkey,
) -> Result<Instruction, ProgramError> {
let mut instruction = sync_native(token_program_id, account_pubkey)?;
instruction
.accounts
.push(AccountMeta::new_readonly(sysvar::rent::id(), false));

Ok(instruction)
}

/// Creates a `GetAccountDataSize` instruction
pub fn get_account_data_size(
token_program_id: &Pubkey,
Expand Down
6 changes: 6 additions & 0 deletions pinocchio/interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,14 @@ pub enum TokenInstruction {
///
/// Accounts expected by this instruction:
///
/// * Using runtime Rent sysvar
/// 0. `[writable]` The native token account to sync with its underlying
/// lamports.
///
/// * Using Rent sysvar account
Copy link
Contributor Author

@febo febo Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the best way to specify an optional account in the docs. There were no other examples, so I followed a pattern similar to the multisig case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me

/// 0. `[writable]` The native token account to sync with its underlying
/// lamports.
/// 1. `[]` Rent sysvar.
SyncNative,

/// Like [`TokenInstruction::InitializeAccount2`], but does not require the
Expand Down
2 changes: 1 addition & 1 deletion pinocchio/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ solana-signer = "3.0.0"
solana-transaction = "3.0.0"
solana-transaction-error = "3.0.0"
solana-system-interface = { workspace = true }
spl-token-interface = "2"
spl-token-interface = { workspace = true }
spl-token-2022-interface = "2"

[lints]
Expand Down
14 changes: 11 additions & 3 deletions pinocchio/program/src/processor/sync_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ use {

#[inline(always)]
pub fn process_sync_native(accounts: &[AccountInfo]) -> ProgramResult {
let native_account_info = accounts.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
let [native_account_info, remaining @ ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

check_account_owner(native_account_info)?;

let rent = Rent::get()?;
let rent_exempt_reserve = rent.minimum_balance(native_account_info.data_len());
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.
Expand Down
15 changes: 10 additions & 5 deletions pinocchio/program/tests/sync_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod setup;

use {
crate::setup::TOKEN_PROGRAM_ID,
mollusk_svm::{result::Check, Mollusk},
mollusk_svm::{result::Check, sysvar::Sysvars, Mollusk},
pinocchio_token_interface::{
native_mint,
state::{
Expand Down Expand Up @@ -75,15 +75,20 @@ async fn sync_native() {
create_token_account(&native_mint, &authority_key, true, 0, &TOKEN_PROGRAM_ID);
source_account.lamports += 2_000_000_000;

let instruction =
spl_token_interface::instruction::sync_native(&TOKEN_PROGRAM_ID, &source_account_key)
.unwrap();
let instruction = spl_token_interface::instruction::sync_native_with_rent_sysvar(
&TOKEN_PROGRAM_ID,
&source_account_key,
)
.unwrap();

// Executes the sync_native instruction.

let result = mollusk().process_and_validate_instruction_chain(
&[(&instruction, &[Check::success()])],
&[(source_account_key, source_account)],
&[
(source_account_key, source_account),
Sysvars::default().keyed_account_for_rent_sysvar(),
],
);

result.resulting_accounts.iter().for_each(|(key, account)| {
Expand Down