code-423n4 / 2022-06-putty-findings

5 stars 0 forks source link

Gas Optimizations #343

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Use Custom Error instead of Revert / Require String to Save Gas

Custom error from solidity 0.8.4 are cheaper than revert strings, custom error are defined using the error statement can use inside and outside the contract.

source https://blog.soliditylang.org/2021/04/21/custom-errors/

i suggest replacing revert / require error strings with custom error.

POC :

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L214 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L241 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L278 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L281 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L284 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L287 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L290 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L293 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L329 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L353 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L395 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L398 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L401 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L405 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L406 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L429 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L470 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L475 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L481 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L527 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L551 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L552 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L598 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L599 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L765 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L12 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L13 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L26 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L27 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L30 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L41

  1. 0is less efficient than!= 0` for unsigned integers

!= 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled (6 gas)

While it may seem that > 0 is cheaper than !=, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND you’re in a require statement, this will save gas.

source : https://twitter.com/gzeon/status/1485428085885640706

I suggest changing > 0 with != 0

POC

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L599

  1. use calldata instead memory

In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.

In short, use calldata instead of memory if the function argument is only read.

https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L271 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L727 https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L741