Move adds on total earlier to enable the use of unchecked (#3527)

Co-authored-by: Francisco <frangio.1@gmail.com>
This commit is contained in:
Linhai Song
2022-07-05 13:49:24 -04:00
committed by GitHub
parent e7397844f8
commit 8b778fa20d

View File

@ -149,8 +149,12 @@ contract PaymentSplitter is Context {
require(payment != 0, "PaymentSplitter: account is not due payment");
_released[account] += payment;
// _totalReleased is the sum of all values in _released.
// If "_totalReleased += payment" does not overflow, then "_released[account] += payment" cannot overflow.
_totalReleased += payment;
unchecked {
_released[account] += payment;
}
Address.sendValue(account, payment);
emit PaymentReleased(account, payment);
@ -168,8 +172,13 @@ contract PaymentSplitter is Context {
require(payment != 0, "PaymentSplitter: account is not due payment");
_erc20Released[token][account] += payment;
// _erc20TotalReleased[token] is the sum of all values in _erc20Released[token].
// If "_erc20TotalReleased[token] += payment" does not overflow, then "_erc20Released[token][account] += payment"
// cannot overflow.
_erc20TotalReleased[token] += payment;
unchecked {
_erc20Released[token][account] += payment;
}
SafeERC20.safeTransfer(token, account, payment);
emit ERC20PaymentReleased(token, account, payment);