Skip to content

Commit

Permalink
Use unchecked arithmetic in "_transfer", "_mint" and "_burn" (OpenZep…
Browse files Browse the repository at this point in the history
  • Loading branch information
ronhuafeng committed Sep 9, 2022
1 parent d6923a9 commit 6746516
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
* `Address`: optimize `functionCall` by calling `functionCallWithValue` directly. ([#3468](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3468))
* `Address`: optimize `functionCall` functions by checking contract size only if there is no returned data. ([#3469](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3469))
* `GovernorCompatibilityBravo`: remove unused `using` statements ([#3506](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3506))
* `ERC20`: optimize `_transfer`, `_mint` and `_burn` by using `unchecked` arithmetic when possible. ([#3513](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3513))

## 4.7.0 (2022-06-29)

Expand Down
12 changes: 9 additions & 3 deletions contracts/token/ERC20/ERC20.sol
Expand Up @@ -237,8 +237,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
_balances[to] += amount;

emit Transfer(from, to, amount);

Expand All @@ -260,7 +262,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
_beforeTokenTransfer(address(0), account, amount);

_totalSupply += amount;
_balances[account] += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);

_afterTokenTransfer(address(0), account, amount);
Expand All @@ -286,8 +291,9 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
_totalSupply -= amount;

emit Transfer(account, address(0), amount);

Expand Down

0 comments on commit 6746516

Please sign in to comment.