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

Gas optimization

[G-01] Tightly pack storage variables/optimize the order of variable declaration

Here, the storage variables can be tightly packed by putting data type that can fit together next to each other.

save Gas 1K : 1 SLOT

File: /src/DSCEngine.sol
72: uint256 private constant LIQUIDATION_THRESHOLD = 50; // 200% overcollateralized
73: uint256 private constant LIQUIDATION_PRECISION = 100;
File: /src/DSCEngine.sol
-72: uint256 private constant LIQUIDATION_THRESHOLD = 50; // 200% overcollateralized
-73: uint256 private constant LIQUIDATION_PRECISION = 100;
+72: uint128 private constant LIQUIDATION_THRESHOLD = 50; // 200% overcollateralized
+73: uint128 private constant LIQUIDATION_PRECISION = 100;

[G-02] ++i/i++ should be unchecked{++i}/unchecked{i++} when it is not possible for them to overflow, as is the case when used in for- and while-loops

Save Gas 170

File: src/DSCEngine.sol
118: for (uint256 i = 0; i < tokenAddresses.length; i++) {
353: for (uint256 i = 0; i < s_collateralTokens.length; i++) {

[G-03] Can Make The Variable Outside The Loop To Save Gas

When you declare a variable inside a loop, Solidity creates a new instance of the variable for each iteration of the loop. This can lead to unnecessary gas costs, especially if the loop is executed frequently or iterates over a large number of elements.

By declaring the variable outside the loop, you can avoid the creation of multiple instances of the variable and reduce the gas cost of your contract. Here's an example:

Save Gas 200

contract MyContract {
function sum(uint256[] memory values) public pure returns (uint256) {
uint256 total = 0;
for (uint256 i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
}
File: /src/DSCEngine.sol
354: address token = s_collateralTokens[i];
355: uint256 amount = s_collateralDeposited[user][token];

[G-04] Functions guaranteed to revert when called by normal users can be marked payable

Save Gas 42

File: /src/DecentralizedStableCoin.sol
46: function burn(uint256 _amount) public override onlyOwner {
64: function mint(address _to, uint256 _amount) external onlyOwner returns (bool) {

[G-05] Reduce gas usage by moving to Solidity 0.8.19 or later

[G-06] Use assembly to emit events

With the use of inline assembly in Solidity, we can take advantage of low-level features like scratch space and the free memory pointer, offering more gas-efficient ways of emitting events. The scratch space is a certain area of memory where we can temporarily store data, and the free memory pointer indicates the next available memory slot. Using these, we can efficiently assemble event data without incurring additional memory expansion costs. However, safety is paramount: to avoid overwriting or leakage

File: /src/DSCEngine.sol
156: emit CollateralDeposited(msg.sender, tokenCollateralAddress, amountCollateral);
286: emit CollateralRedeemed(from, to, tokenCollateralAddress, amountCollateral);

[G-07] Use assembly to check for the zero address

Using assembly for address comparisons in Solidity can save gas because it allows for more direct access to the Ethereum Virtual Machine (EVM), reducing the overhead of higher-level operations. Solidity's high-level abstraction simpl

File: /src/DSCEngine.sol
103: if (s_priceFeeds[token] == address(0)) {

Support

FAQs

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