Skip to content

Commit 9949dee

Browse files
authored
p-token: Simplify math operations (#83)
* Simplify math * Lint * Add note * Update note
1 parent 569a37b commit 9949dee

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

p-token/src/processor/close_account.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use {
1111
};
1212

1313
#[inline(always)]
14+
#[allow(clippy::arithmetic_side_effects)]
1415
pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
1516
let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts
1617
else {
@@ -44,14 +45,15 @@ pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
4445
}
4546
}
4647

47-
let destination_starting_lamports = destination_account_info.lamports();
4848
// SAFETY: single mutable borrow to `destination_account_info` lamports and
4949
// there are no "active" borrows of `source_account_info` account data.
5050
unsafe {
5151
// Moves the lamports to the destination account.
52-
*destination_account_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
53-
.checked_add(source_account_info.lamports())
54-
.ok_or(TokenError::Overflow)?;
52+
//
53+
// Note: This is safe since the runtime checks for balanced instructions
54+
// before and after each CPI and instruction, and the total lamports
55+
// supply is bound to `u64::MAX`.
56+
*destination_account_info.borrow_mut_lamports_unchecked() += source_account_info.lamports();
5557
// Closes the source account.
5658
source_account_info.close_unchecked();
5759
}

p-token/src/processor/shared/burn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use {
88
};
99

1010
#[inline(always)]
11+
#[allow(clippy::arithmetic_side_effects)]
1112
pub fn process_burn(
1213
accounts: &[AccountInfo],
1314
amount: u64,
@@ -83,8 +84,7 @@ pub fn process_burn(
8384
source_account.set_amount(updated_source_amount);
8485
// Note: The amount of a token account is always within the range of the
8586
// mint supply (`u64`).
86-
let mint_supply = mint.supply().checked_sub(amount).unwrap();
87-
mint.set_supply(mint_supply);
87+
mint.set_supply(mint.supply() - amount);
8888
}
8989

9090
Ok(())

p-token/src/processor/withdraw_excess_lamports.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ pub fn process_withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResu
8686
source_starting_lamports - transfer_amount;
8787
}
8888

89-
let destination_starting_lamports = destination_info.lamports();
9089
// SAFETY: single mutable borrow to `destination_info` lamports.
9190
unsafe {
9291
// Moves the lamports to the destination account.
93-
*destination_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
94-
.checked_add(transfer_amount)
95-
.ok_or(TokenError::Overflow)?;
92+
//
93+
// Note: The total lamports supply is bound to `u64::MAX`.
94+
*destination_info.borrow_mut_lamports_unchecked() += transfer_amount;
9695
}
9796

9897
Ok(())

0 commit comments

Comments
 (0)