diff --git a/source/batch-call/contracts/BatchCall.sol b/source/batch-call/contracts/BatchCall.sol index cbae54b37..400940589 100644 --- a/source/batch-call/contracts/BatchCall.sol +++ b/source/batch-call/contracts/BatchCall.sol @@ -54,12 +54,15 @@ contract BatchCall { require(tokenAddresses.length > 0); uint256[] memory balances = new uint256[](tokenAddresses.length); - for (uint256 i; i < tokenAddresses.length; ++i) { + for (uint256 i; i < tokenAddresses.length; ) { if (tokenAddresses[i] != address(0x0)) { balances[i] = tokenBalance(userAddress, tokenAddresses[i]); } else { balances[i] = userAddress.balance; } + unchecked { + ++i; + } } return balances; } @@ -79,8 +82,8 @@ contract BatchCall { uint256[] memory balances = new uint256[]( tokenAddresses.length * userAddresses.length ); - for (uint256 i; i < userAddresses.length; ++i) { - for (uint256 j; j < tokenAddresses.length; ++j) { + for (uint256 i; i < userAddresses.length; ) { + for (uint256 j; j < tokenAddresses.length; ) { if (tokenAddresses[j] != address(0x0)) { // ETH address in Etherdelta config balances[(i * tokenAddresses.length) + j] = tokenBalance( @@ -90,6 +93,12 @@ contract BatchCall { } else { balances[(i * tokenAddresses.length) + j] = userAddresses[i].balance; } + unchecked { + ++j; + } + } + unchecked { + ++i; } } return balances; @@ -143,12 +152,15 @@ contract BatchCall { require(tokenAddresses.length > 0); uint256[] memory allowances = new uint256[](tokenAddresses.length); - for (uint256 i; i < tokenAddresses.length; ++i) { + for (uint256 i; i < tokenAddresses.length; ) { allowances[i] = tokenAllowance( userAddress, spenderAddress, tokenAddresses[i] ); + unchecked { + ++i; + } } return allowances; } @@ -171,13 +183,19 @@ contract BatchCall { tokenAddresses.length * userAddresses.length ); - for (uint256 i; i < userAddresses.length; ++i) { - for (uint256 j; j < tokenAddresses.length; ++j) { + for (uint256 i; i < userAddresses.length; ) { + for (uint256 j; j < tokenAddresses.length; ) { allowances[(i * tokenAddresses.length) + j] = tokenAllowance( userAddresses[i], spenderAddress, tokenAddresses[j] ); + unchecked { + ++j; + } + } + unchecked { + ++i; } } return allowances; @@ -198,9 +216,12 @@ contract BatchCall { require(orders.length > 0); bool[] memory orderValidity = new bool[](orders.length); - for (uint256 i; i < orders.length; ++i) { + for (uint256 i; i < orders.length; ) { (, uint256 errorCount) = swapContract.check(senderWallet, orders[i]); orderValidity[i] = errorCount == 0 ? true : false; + unchecked { + ++i; + } } return orderValidity; } @@ -220,7 +241,7 @@ contract BatchCall { require(orders.length > 0); bool[] memory orderValidity = new bool[](orders.length); - for (uint256 i; i < orders.length; ++i) { + for (uint256 i; i < orders.length; ) { ISwapERC20.OrderERC20 memory order = orders[i]; (uint256 errorCount, ) = swapERC20Contract.check( senderWallet, @@ -236,6 +257,9 @@ contract BatchCall { order.s ); orderValidity[i] = errorCount == 0 ? true : false; + unchecked { + ++i; + } } return orderValidity; } @@ -257,8 +281,11 @@ contract BatchCall { require(signerWallets.length == nonces.length); bool[] memory nonceUsed = new bool[](signerWallets.length); - for (uint256 i; i < signerWallets.length; ++i) { + for (uint256 i; i < signerWallets.length; ) { nonceUsed[i] = swapContract.nonceUsed(signerWallets[i], nonces[i]); + unchecked { + ++i; + } } return nonceUsed; } diff --git a/source/registry/contracts/Registry.sol b/source/registry/contracts/Registry.sol index 1ce3ce8f8..d2bcd82f6 100644 --- a/source/registry/contracts/Registry.sol +++ b/source/registry/contracts/Registry.sol @@ -89,7 +89,9 @@ contract Registry { bytes4[] memory _protocolList = new bytes4[](_protocolListLength); for (uint256 i = _protocolListLength; i > 0; ) { - i--; + unchecked { + --i; + } bytes4 _protocol = bytes4(supportedProtocolList.at(i)); _protocolList[i] = _protocol; supportedProtocolList.remove(_protocol); @@ -103,7 +105,9 @@ contract Registry { address[] memory _tokenList = new address[](_tokenListLength); for (uint256 i = _tokenListLength; i > 0; ) { - i--; + unchecked { + --i; + } address _token = supportedTokenList.at(i); _tokenList[i] = _token; supportedTokenList.remove(_token); @@ -135,10 +139,13 @@ contract Registry { msg.sender ]; - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { bytes4 protocol = _protocols[i]; if (!_protocolList.add(protocol)) revert ProtocolExists(protocol); stakersByProtocol[protocol].add(msg.sender); + unchecked { + ++i; + } } uint256 _transferAmount = supportCost * _length; @@ -159,11 +166,14 @@ contract Registry { EnumerableSet.Bytes32Set storage protocolList = protocolsByStaker[ msg.sender ]; - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { bytes4 _protocol = _protocols[i]; if (!protocolList.remove(_protocol)) revert ProtocolDoesNotExist(_protocol); stakersByProtocol[_protocol].remove(msg.sender); + unchecked { + ++i; + } } uint256 _transferAmount = supportCost * _length; @@ -185,8 +195,11 @@ contract Registry { EnumerableSet.AddressSet storage stakers = stakersByProtocol[_protocol]; uint256 _length = stakers.length(); _urls = new string[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { _urls[i] = stakerServerURLs[address(stakers.at(i))]; + unchecked { + ++i; + } } } @@ -214,8 +227,11 @@ contract Registry { EnumerableSet.Bytes32Set storage _protocols = protocolsByStaker[_staker]; uint256 _length = _protocols.length(); _protocolList = new bytes4[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { _protocolList[i] = bytes4(_protocols.at(i)); + unchecked { + ++i; + } } } @@ -230,8 +246,11 @@ contract Registry { EnumerableSet.AddressSet storage _stakerList = stakersByProtocol[_protocol]; uint256 _length = _stakerList.length(); _stakers = new address[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { _stakers[i] = _stakerList.at(i); + unchecked { + ++i; + } } } @@ -244,10 +263,13 @@ contract Registry { if (_length <= 0) revert ArgumentInvalid(); EnumerableSet.AddressSet storage tokenList = tokensByStaker[msg.sender]; - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { address _token = _tokens[i]; if (!tokenList.add(_token)) revert TokenExists(_token); stakersByToken[_token].add(msg.sender); + unchecked { + ++i; + } } uint256 _transferAmount = supportCost * _length; emit AddTokens(msg.sender, _tokens); @@ -264,10 +286,13 @@ contract Registry { uint256 _length = _tokens.length; if (_length <= 0) revert ArgumentInvalid(); EnumerableSet.AddressSet storage tokenList = tokensByStaker[msg.sender]; - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { address token = _tokens[i]; if (!tokenList.remove(token)) revert TokenDoesNotExist(token); stakersByToken[token].remove(msg.sender); + unchecked { + ++i; + } } uint256 _transferAmount = supportCost * _length; emit RemoveTokens(msg.sender, _tokens); @@ -287,8 +312,11 @@ contract Registry { EnumerableSet.AddressSet storage stakers = stakersByToken[_token]; uint256 _length = stakers.length(); urls = new string[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { urls[i] = stakerServerURLs[address(stakers.at(i))]; + unchecked { + ++i; + } } } @@ -316,8 +344,11 @@ contract Registry { EnumerableSet.AddressSet storage tokens = tokensByStaker[_staker]; uint256 _length = tokens.length(); tokenList = new address[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { tokenList[i] = tokens.at(i); + unchecked { + ++i; + } } } @@ -332,8 +363,11 @@ contract Registry { EnumerableSet.AddressSet storage stakerList = stakersByToken[_token]; uint256 _length = stakerList.length(); _stakers = new address[](_length); - for (uint256 i; i < _length; ++i) { + for (uint256 i; i < _length; ) { _stakers[i] = stakerList.at(i); + unchecked { + ++i; + } } } @@ -347,8 +381,11 @@ contract Registry { ) external view returns (string[] memory _urls) { uint256 stakersLength = _stakers.length; _urls = new string[](stakersLength); - for (uint256 i; i < stakersLength; ++i) { + for (uint256 i; i < stakersLength; ) { _urls[i] = stakerServerURLs[_stakers[i]]; + unchecked { + ++i; + } } } diff --git a/source/staking/contracts/Staking.sol b/source/staking/contracts/Staking.sol index 51ba12ec2..9a98f8486 100644 --- a/source/staking/contracts/Staking.sol +++ b/source/staking/contracts/Staking.sol @@ -21,7 +21,7 @@ contract Staking is IStaking, Ownable { uint256 public stakingDuration; // Minimum delay to staking duration change - uint256 private minDurationChangeDelay; + uint256 private immutable minDurationChangeDelay; // Timestamp after which staking duration change is possible uint256 private activeDurationChangeTimestamp; @@ -181,7 +181,7 @@ contract Staking is IStaking, Ownable { */ function getStakes( address _account - ) external view override returns (Stake memory _accountStake) { + ) external view override returns (Stake memory) { return stakes[_account]; } diff --git a/source/swap-erc20/contracts/SwapERC20.sol b/source/swap-erc20/contracts/SwapERC20.sol index 6d20512b9..0d57bec48 100644 --- a/source/swap-erc20/contracts/SwapERC20.sol +++ b/source/swap-erc20/contracts/SwapERC20.sol @@ -390,11 +390,14 @@ contract SwapERC20 is ISwapERC20, Ownable, EIP712 { * @param nonces uint256[] List of nonces to cancel */ function cancel(uint256[] calldata nonces) external override { - for (uint256 i; i < nonces.length; ++i) { + for (uint256 i; i < nonces.length; ) { uint256 nonce = nonces[i]; if (_markNonceAsUsed(msg.sender, nonce)) { emit Cancel(nonce, msg.sender); } + unchecked { + ++i; + } } } diff --git a/source/swap-erc20/contracts/interfaces/INoReturnERC20.sol b/source/swap-erc20/contracts/interfaces/INoReturnERC20.sol index 798013daa..b57329fc7 100644 --- a/source/swap-erc20/contracts/interfaces/INoReturnERC20.sol +++ b/source/swap-erc20/contracts/interfaces/INoReturnERC20.sol @@ -2,5 +2,5 @@ pragma solidity 0.8.23; interface INoReturnERC20 { - function transferFrom(address from, address to, uint value) external; + function transferFrom(address from, address to, uint256 value) external; } diff --git a/source/swap/contracts/Swap.sol b/source/swap/contracts/Swap.sol index 929e52381..4e4f81d63 100644 --- a/source/swap/contracts/Swap.sol +++ b/source/swap/contracts/Swap.sol @@ -48,7 +48,7 @@ contract Swap is ISwap, Ownable2Step, EIP712 { uint256 public protocolFee; address public protocolFeeWallet; - bytes4 public requiredSenderKind; + bytes4 public immutable requiredSenderKind; // Mapping of ERC165 interface ID to token adapter mapping(bytes4 => IAdapter) public adapters; @@ -73,8 +73,12 @@ contract Swap is ISwap, Ownable2Step, EIP712 { DOMAIN_CHAIN_ID = block.chainid; DOMAIN_SEPARATOR = _domainSeparatorV4(); - for (uint256 i; i < _adapters.length; ++i) { + uint256 adaptersLength = _adapters.length; + for (uint256 i; i < adaptersLength; ) { adapters[_adapters[i].interfaceId()] = _adapters[i]; + unchecked { + ++i; + } } requiredSenderKind = _requiredSenderKind; protocolFee = _protocolFee; @@ -230,10 +234,13 @@ contract Swap is ISwap, Ownable2Step, EIP712 { * @param nonces uint256[] List of nonces to cancel */ function cancel(uint256[] calldata nonces) external override { - for (uint256 i; i < nonces.length; ++i) { + for (uint256 i; i < nonces.length; ) { uint256 nonce = nonces[i]; _markNonceAsUsed(msg.sender, nonce); emit Cancel(nonce, msg.sender); + unchecked { + ++i; + } } }