-
Notifications
You must be signed in to change notification settings - Fork 50
p-token: Add unwrap_lamports
instruction
#87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
febo
wants to merge
7
commits into
febo/custom-entrypoint
Choose a base branch
from
febo/unwrap-lamports
base: febo/custom-entrypoint
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+521
−1
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
10ff7e5
Add processor
febo d86332e
Add tests
febo fb319d7
Add instruction
febo 82fa3b5
Fix formatting
febo 70797ba
Fix discriminator value
febo fecf709
Add check for unwrap lamports
febo 322a1b5
Update discriminator on tests
febo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use { | ||
super::validate_owner, | ||
crate::processor::{check_account_owner, unpack_amount}, | ||
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult}, | ||
pinocchio_token_interface::{ | ||
error::TokenError, | ||
state::{account::Account, load_mut}, | ||
}, | ||
}; | ||
|
||
#[allow(clippy::arithmetic_side_effects)] | ||
pub fn process_unwrap_lamports(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult { | ||
// Amount being unwrapped. | ||
let amount = unpack_amount(instruction_data)?; | ||
|
||
let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts | ||
else { | ||
return Err(ProgramError::NotEnoughAccountKeys); | ||
}; | ||
|
||
// SAFETY: single immutable borrow to `source_account_info` account data | ||
let source_account = | ||
unsafe { load_mut::<Account>(source_account_info.borrow_mut_data_unchecked())? }; | ||
|
||
if !source_account.is_native() { | ||
return Err(TokenError::NonNativeNotSupported.into()); | ||
} | ||
|
||
// SAFETY: `authority_info` is not currently borrowed; in the case | ||
// `authority_info` is the same as `source_account_info`, then it cannot be | ||
// a multisig. | ||
unsafe { validate_owner(&source_account.owner, authority_info, remaining)? }; | ||
|
||
let remaining_amount = source_account | ||
.amount() | ||
.checked_sub(amount) | ||
.ok_or(TokenError::InsufficientFunds)?; | ||
|
||
// Comparing whether the AccountInfo's "point" to the same account or | ||
// not - this is a faster comparison since it just checks the internal | ||
// raw pointer. | ||
let self_transfer = source_account_info == destination_account_info; | ||
|
||
if self_transfer || amount == 0 { | ||
// Validates the token account owner since we are not writing | ||
// to the account. | ||
check_account_owner(source_account_info)?; | ||
} else { | ||
source_account.set_amount(remaining_amount); | ||
|
||
// SAFETY: single mutable borrow to `source_account_info` lamports. | ||
let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() }; | ||
// Note: The amount of a source token account is already validated and the | ||
// `lamports` on the account is always greater than `amount`. | ||
*source_lamports -= amount; | ||
|
||
// SAFETY: single mutable borrow to `destination_account_info` lamports; the | ||
// account is already validated to be different from | ||
// `source_account_info`. | ||
let destination_lamports = | ||
unsafe { destination_account_info.borrow_mut_lamports_unchecked() }; | ||
// Note: The total lamports supply is bound to `u64::MAX`. | ||
*destination_lamports += amount; | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.