Uninitialized variables are assigned with the types default value.
Explicitly initializing a variable with it's default value costs unnecessary gas.
Declaring uint256 i = 0;
means doing an MSTORE
of the value 0
Instead you could just declare uint256 i
to declare the variable without assigning itโs default value, saving 3 gas
per declaration
๐คฆ Bad:
๐ Good:
You can save 16000 gas per instance in deploying contract.
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L40
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L41
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L42
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L103
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L58
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L65
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L71
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L76
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L198
You can save 16000 gas
per instance in deploying contract.
You can save about 6 gas
per instance if using assembly to check for address (0)
public
functions to external
External call cost is less expensive than of public functions.
Contracts are allowed to override their parentsโ functions and change the visibility from external
to public
.
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/EscrowFactory.sol#L66
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L14
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L22
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L30
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L30
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L57
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L69
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L78
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L96
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L29
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L43
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L53
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L62
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L69
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L74
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L95
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L108
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L118
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L125
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L138
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L145
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L151
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L161
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L168
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L181
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L187
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L194
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L207
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L214
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L233
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L242
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L262
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L282
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L296
Costs more gas
is less efficient than
abi.encodePacked()Changing abi.encode
to abi.encodePacked
can save gas. abi.encode
pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked
is more gas-efficient.
Costs more gas
Changing abi.encode
to abi.encodePacked
can save gas. abi.encode
pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked
is more gas-efficient.
Uninitialized variables are assigned with the types default value.
Explicitly initializing a variable with it's default value costs unnecessary gas.
Declaring uint256 i = 0;
means doing an MSTORE
of the value 0
Instead you could just declare uint256 i
to declare the variable without assigning itโs default value, saving 3 gas
per declaration
๐คฆ Bad:
๐ Good:
You can save 16000 gas per instance in deploying contract.
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L40
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L41
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L42
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/Escrow.sol#L103
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L58
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L65
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L71
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L76
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L198
You can save 16000 gas
per instance in deploying contract.
You can save about 6 gas
per instance if using assembly to check for address (0)
public
functions to external
External call cost is less expensive than of public functions.
Contracts are allowed to override their parentsโ functions and change the visibility from external
to public
.
https://github.com/Cyfrin/2023-07-escrow/blob/main/./src/EscrowFactory.sol#L66
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L14
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L22
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/mocks/ERC20MockFailedTransfer.sol#L30
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L30
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L57
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L69
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L78
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowFactoryTest.t.sol#L96
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L29
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L43
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L53
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L62
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L69
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L74
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L95
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L108
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L118
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L125
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L138
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L145
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L151
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L161
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L168
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L181
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L187
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L194
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L207
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L214
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L233
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L242
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L262
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L282
https://github.com/Cyfrin/2023-07-escrow/blob/main/./test/unit/EscrowTest.t.sol#L296
Costs more gas
is less efficient than
abi.encodePacked()Changing abi.encode
to abi.encodePacked
can save gas. abi.encode
pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked
is more gas-efficient.
Costs more gas
Changing abi.encode
to abi.encodePacked
can save gas. abi.encode
pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked
is more gas-efficient.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.