15,000 USDC
View results
Submission Details
Severity: gas

Setting the constructor to `payable`

Summary

10 opcodes can be cut if declare the constructor as payable. That eliminates the msg.value == 0 upon initialization and save 24 gas

-- DSCEngine

// Without payable - 1676245 gas
// With payable - 1676221 gas
// Saves - 24 gas
constructor(address[] memory tokenAddresses, address[] memory priceFeedAddresses, address dscAddress) payable {
// USD Price Feeds
if (tokenAddresses.length != priceFeedAddresses.length) {
revert DSCEngine__TokenAddressesAndPriceFeedAddressesMustBeSameLength();
}
// 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]);
}
i_dsc = DecentralizedStableCoin(dscAddress);
}

-- DecentralizedStableCoin

// Without payable - 1546645
// With payable - 1546621
// Saves - 24 gas
constructor() ERC20("DecentralizedStableCoin", "DSC") payable {}

Instances

2 files

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

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

Tools Used

Manual

Recommendations

Set the constructors to payable

Support

FAQs

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