Skip to content

p-token: Add optional rent sysvar account#137

Merged
febo merged 3 commits intomainfrom
febo/sync-native-rent
Mar 4, 2026
Merged

p-token: Add optional rent sysvar account#137
febo merged 3 commits intomainfrom
febo/sync-native-rent

Conversation

@febo
Copy link
Contributor

@febo febo commented Mar 4, 2026

Problem

PR #132 add the requirement of using the Rent sysvar in SyncNative to update the current rent-exempt reserve. This led to an increase in CUs from 61 to 207.

Solution

Allow the Rent sysvar account to be provided, which reduces the current CUs from 207 to 90.

@febo febo requested a review from joncinque March 4, 2026 12:06
/// 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

Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks good to me overall! Just some small extra bits to consider

/// 0. `[writable]` The native token account to sync with its underlying
/// lamports.
///
/// * Using Rent sysvar account
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


let instruction =
let mut instruction =
spl_token_interface::instruction::sync_native(&TOKEN_PROGRAM_ID, &source_account_key)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add another version of this function that adds the sysvar account? That way we don't have to do anything hacky in the test, and people will just get the new function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added it here: 2197726.

Comment on lines +26 to +27
// SAFETY: `remaining` is guaranteed to not be empty.
let rent_sysvar_info = unsafe { remaining.get_unchecked(0) };
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: not sure how this impacts performance, but we could change the condition to:

let rent_exempt_reserve = if let Some(rent_sysvar_info) = remaining.first() {
    let rent = unsafe { ... }
} else {
    Rent::get()?...
}

And that way we can remove one more instance of unsafe code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, nice, it actually reduced the CUs to 90.

@febo febo force-pushed the febo/sync-native-rent branch from e16dd79 to 2197726 Compare March 4, 2026 12:44
Base automatically changed from febo/compiler-hints to main March 4, 2026 12:44
@febo febo marked this pull request as ready for review March 4, 2026 12:47
@febo febo requested a review from joncinque March 4, 2026 12:53
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks great! Can you add the same thing to token-2022?

@febo
Copy link
Contributor Author

febo commented Mar 4, 2026

Looks great! Can you add the same thing to token-2022?

Yes, I will do that.

@febo febo force-pushed the febo/sync-native-rent branch from 2197726 to 78c3ffa Compare March 4, 2026 14:12
@febo febo merged commit 065786e into main Mar 4, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants