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

Gas optimizations

Gas optimizations list

Number Issue Instances
[G-01] Reduce gas usage by moving to Solidity 0.8.19 or later 4
[G-02] Use hardcode address instead of address(this) 2
[G-03] Use != 0 instead of > 0 for uints to save gas 3
[G-04] Use assembly to check for address(0) 4

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

See "References" below.

References

  • https://blog.soliditylang.org/2023/02/22/solidity-0.8.19-release-announcement/#preventing-dead-code-in-runtime-bytecode

There are 4 instances of this issue:

File: Escrow.sol
2: pragma solidity 0.8.18;

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L2

File: EscrowFactory.sol
2: pragma solidity 0.8.18;

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrowFactory.sol#L2

File: IEscrow.sol
2: pragma solidity 0.8.18;

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcIEscrow.sol#L2

File: IEscrowFactory.sol
2: pragma solidity 0.8.18;

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcIEscrowFactory.sol#L2

[G-02] 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:

  • https://book.getfoundry.sh/reference/forge-std/compute-create-address

  • https://twitter.com/transmissions11/status/1518507047943245824

There are 2 instances of this issue:

File: Escrow.sol
44: if (tokenContract.balanceOf(address(this)) < price) revert Escrow__MustDeployWithTokenBalance();

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L44

File: Escrow.sol
98: i_tokenContract.safeTransfer(i_seller, i_tokenContract.balanceOf(address(this)));

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L98

File: Escrow.sol
110: uint256 tokenBalance = i_tokenContract.balanceOf(address(this));

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L110

File: Escrow.sol
125: tokenBalance = i_tokenContract.balanceOf(address(this));

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L125

File: EscrowFactory.sol
30: address(this),

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrowFactory.sol#L30

[G-03] Use != 0 instead of > 0 for uints to save gas

Using != to check if a uint is greater than 0 is more gas efficient than using >.

There are 3 instances of this issue:

File: Escrow.sol
119: if (buyerAward > 0) {

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L119

File: Escrow.sol
122: if (i_arbiterFee > 0) {

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L122

File: Escrow.sol
126: if (tokenBalance > 0) {

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L126

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

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

References:

  • https://medium.com/@kalexotsu/solidity-assembly-checking-if-an-address-is-0-efficiently-d2bfe071331

File: Escrow.sol
40: if (address(tokenContract) == address(0)) revert Escrow__TokenZeroAddress();

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L40

File: Escrow.sol
41: if (buyer == address(0)) revert Escrow__BuyerZeroAddress();

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L41

File: Escrow.sol
42: if (seller == address(0)) revert Escrow__SellerZeroAddress();

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L42

File: Escrow.sol
103: if (i_arbiter == address(0)) revert Escrow__DisputeRequiresArbiter();

https://github.com/Cyfrin/2023-07-escrow/tree/main/srcEscrow.sol#L103

Support

FAQs

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