From 8b778fa20d6d76340c5fac1ed66c80273f05b95a Mon Sep 17 00:00:00 2001 From: Linhai Song Date: Tue, 5 Jul 2022 13:49:24 -0400 Subject: [PATCH] Move adds on total earlier to enable the use of unchecked (#3527) Co-authored-by: Francisco --- contracts/finance/PaymentSplitter.sol | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/contracts/finance/PaymentSplitter.sol b/contracts/finance/PaymentSplitter.sol index f5823e999..381cc351b 100644 --- a/contracts/finance/PaymentSplitter.sol +++ b/contracts/finance/PaymentSplitter.sol @@ -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);