Skip to content

Commit

Permalink
unchecked for loop increments; further openzeppelin recs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites committed Jan 17, 2024
1 parent 2f73257 commit 3c76cb3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 29 deletions.
45 changes: 36 additions & 9 deletions source/batch-call/contracts/BatchCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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(
Expand All @@ -90,6 +93,12 @@ contract BatchCall {
} else {
balances[(i * tokenAddresses.length) + j] = userAddresses[i].balance;
}
unchecked {
++j;
}
}
unchecked {
++i;
}
}
return balances;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -236,6 +257,9 @@ contract BatchCall {
order.s
);
orderValidity[i] = errorCount == 0 ? true : false;
unchecked {
++i;
}
}
return orderValidity;
}
Expand All @@ -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;
}
Expand Down
63 changes: 50 additions & 13 deletions source/registry/contracts/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -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;
}
}
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -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;
}
}
}

Expand All @@ -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;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/staking/contracts/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down
5 changes: 4 additions & 1 deletion source/swap-erc20/contracts/SwapERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/swap-erc20/contracts/interfaces/INoReturnERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 10 additions & 3 deletions source/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 3c76cb3

Please sign in to comment.