15,000 USDC
View results
Submission Details
Severity: gas
Valid

Unoptimized `constructor` code

Summary

The constructor code can be optimized further, to reduce deployment cost.

Vulnerability Details

The constructor can be marked as payable to save some gas.

Also, for better gas usage, a collateralTokens array can be created in memory, then addresses are pushed there and then made equal to the one in storage.

Impact

The gas saving would be significant, especially for a larger array of collateral tokens.

Tools Used

Manual review

Recommendations

The constructor can be optimized and then rewritten as:

constructor(address[] memory tokenAddresses, address[] memory priceFeedAddresses, address dscAddress) payable {
if (tokenAddresses.length != priceFeedAddresses.length) {
revert DSCEngine__TokenAddressesAndPriceFeedAddressesAmountsDontMatch();
}
// These feeds will be the USD pairs
// For example ETH / USD or MKR / USD
address[] memory collateralTokens = new address[](tokenAddresses.length); // <--- Here
for (uint256 i; i < tokenAddresses.length;) {
s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
collateralTokens[i] = tokenAddresses[i]; // <--- Here
unchecked {
++i;
}
}
s_collateralTokens = collateralTokens; // <--- Here
i_dsc = DecentralizedStableCoin(dscAddress);
}

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.