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

Gas Report

Gas optimizations list

Number Issue Instances
[G-01] Using == for uints to check for 0 value saves gas 2
[G-02] Use assembly to check for address(0) 2
[G-03] Cache array length outside of for loop 2
[G-04] Reduce gas usage by moving to Solidity 0.8.19 or later 3
[G-05] Use unchecked{} for increments/decrements that cannot overflow 2
[G-06] ++i costs less gas than i++, especially when used in for-loops 2
[G-07] <x> += <y> costs more gas than <x> = <x> + <y> for state variables 2
[G-08] Re-initialising variables to their default value wastes gas 2
[G-09] Use constants instead of type(uintx).max 1
[G-10] Use hardcode address instead of address(this) 2

[G-01] Using == for uints to check for 0 value saves gas

Using == 0 when comparing uints instead of, for example, <= 0 is more gas efficient.

There are 2 instances of this issue:

File: DecentralizedStableCoin.sol
48: if (_amount <= 0) {

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

File: DecentralizedStableCoin.sol
61: if (_amount <= 0) {

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

[G-02] Use assembly to check for address(0)

Assembly can be used to more efficiently check for a zero address.

References:

There are 2 instances of this issue:

File: DecentralizedStableCoin.sol
58: if (_to == address(0)) {

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

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

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

[G-03] Cache array length outside of for loop

Storing the array length before looping is more efficient as it prevents its length from being checked with each iteration.

There are 2 instances of this issue:

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

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

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

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

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

See "References" below.

References

There are 3 instances of this issue:

File: DecentralizedStableCoin.sol
24: pragma solidity ^0.8.18;

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

File: DSCEngine.sol
24: pragma solidity ^0.8.18;

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

File: libraries\OracleLib.sol
3: pragma solidity ^0.8.18;

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/libraries\OracleLib.sol#L3

[G-05] Use unchecked{} for increments/decrements that cannot overflow

Use unchecked{i++} to avoid checking for underflows/overflows when appropriate, such as in a for loop, to save gas.

There are 2 instances of this issue:

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

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

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

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

[G-06] ++i costs less gas than i++, especially when it's used in for-loops (--i/i-- too)

This saves 5 gas per loop.

There are 2 instances of this issue:

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

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

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

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

[G-07] <x> += <y> costs more gas than <x> = <x> + <y> for state variables

Using the addition operator instead of plus-equals saves 113 gas per instance.

There are 2 instances of this issue:

File: DSCEngine.sol
155: s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral;

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

File: DSCEngine.sol
198: s_DSCMinted[msg.sender] += amountDscToMint;

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

[G-08] Re-initialising variables to their default value wastes gas

Setting for example a uint to 0 incurs overhead as 0 is already the default value when the variable is defined - there is no need to reset it to 0 again.

There are 2 instances of this issue:

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

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

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

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

[G-09] Use constants instead of type(uintx).max

Using type(uintx).max such as type(uint128).max incurs more gas in distribution and for each transaction. Constants (such as 340282366920938463463374607431768211455 for type(uint128).max) should be used instead.

There is 1 instance of this issue:

File: DSCEngine.sol
329: if (totalDscMinted == 0) return type(uint256).max;

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

[G-10] Use hardcode address instead of address(this)

Instead of address(this), it is more gas-efficient to pre-calculate and use the address with a hardcode. Foundry's script.sol and solmate LibRlp.sol contracts can do this.

References:

There are 2 instances of this issue:

File: DSCEngine.sol
157: bool success = IERC20(tokenCollateralAddress).transferFrom(msg.sender, address(this), amountCollateral);

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

File: DSCEngine.sol
274: bool success = i_dsc.transferFrom(dscFrom, address(this), amountDscToBurn);

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

Support

FAQs

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