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

[G-02] - Push `tokenAddresses` array outside of the loop once in the counstructor to save deployment gas.

Details

In the constructor inside the for loop each time we are pushing the token address to s_collateralTokens array, which increases gas cost. We can initialize the whole array after the loop to save gas.

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L120

Calculation Type Before After Gas Saved
Avg 1004332 1004238 94
// For example ETH / USD, BTC / USD, MKR / USD, etc
for (uint256 i = 0; i < tokenAddresses.length; i++) {
s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
- s_collateralTokens.push(tokenAddresses[i]);
}
+ s_collateralTokens = tokenAddresses;
i_dsc = DecentralizedStableCoin(dscAddress);

Support

FAQs

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