Skip to content

Commit 90b8f5c

Browse files
authored
p-token: Update amount on self-unwrap lamports (#113)
Update amount on self unwrap
1 parent 873eed6 commit 90b8f5c

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

pinocchio/program/src/processor/unwrap_lamports.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,30 @@ pub fn process_unwrap_lamports(accounts: &[AccountInfo], instruction_data: &[u8]
6060
(source_account.amount(), 0)
6161
};
6262

63-
// Comparing whether the AccountInfo's "point" to the same account or
64-
// not - this is a faster comparison since it just checks the internal
65-
// raw pointer.
66-
let self_transfer = source_account_info == destination_account_info;
67-
68-
if unlikely(self_transfer || amount == 0) {
63+
if unlikely(amount == 0) {
6964
// Validates the token account owner since we are not writing
7065
// to the account.
7166
check_account_owner(source_account_info)
7267
} else {
7368
source_account.set_amount(remaining_amount);
7469

75-
// SAFETY: single mutable borrow to `source_account_info` lamports.
76-
let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() };
77-
// Note: The amount of a source token account is already validated and the
78-
// `lamports` on the account is always greater than `amount`.
79-
*source_lamports -= amount;
70+
// Comparing whether the AccountInfo's "point" to the same account or
71+
// not - this is a faster comparison since it just checks the internal
72+
// raw pointer.
73+
if source_account_info != destination_account_info {
74+
// SAFETY: single mutable borrow to `source_account_info` lamports.
75+
let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() };
76+
// Note: The amount of a source token account is already validated and the
77+
// `lamports` on the account is always greater than `amount`.
78+
*source_lamports -= amount;
8079

81-
// SAFETY: single mutable borrow to `destination_account_info` lamports; the
82-
// account is already validated to be different from `source_account_info`.
83-
let destination_lamports =
84-
unsafe { destination_account_info.borrow_mut_lamports_unchecked() };
85-
// Note: The total lamports supply is bound to `u64::MAX`.
86-
*destination_lamports += amount;
80+
// SAFETY: single mutable borrow to `destination_account_info` lamports; the
81+
// account is already validated to be different from `source_account_info`.
82+
let destination_lamports =
83+
unsafe { destination_account_info.borrow_mut_lamports_unchecked() };
84+
// Note: The total lamports supply is bound to `u64::MAX`.
85+
*destination_lamports += amount;
86+
}
8787

8888
Ok(())
8989
}

pinocchio/program/tests/unwrap_lamports.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ fn unwrap_lamports_with_self_transfer() {
409409
.unwrap();
410410

411411
// It should succeed to unwrap lamports with the same source and destination
412-
// accounts.
412+
// accounts. The amount should be deducted from the source account but the
413+
// lamports should remain the same.
413414

414415
let result = mollusk().process_and_validate_instruction(
415416
&instruction,
@@ -432,7 +433,7 @@ fn unwrap_lamports_with_self_transfer() {
432433

433434
let account = account.unwrap();
434435
let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
435-
assert_eq!(token_account.amount, 2_000_000_000);
436+
assert_eq!(token_account.amount, 1_000_000_000);
436437
}
437438

438439
#[test]

0 commit comments

Comments
 (0)