[Gas-5] Unnecessary Strings.sol import at PuttyV2Nft.sol
1
[Gas-6] Flipping the boolean check
1
[Gas-7] Not splitting the strings
1
[Gas-1] Use != 0 instead of > 0 on uint variables
uint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.
Here are list of the gas improvements by using != 0.
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
abi.encodePacked is used to combine the strings. This is good for the readability, but combining the strings using abi.encodePacked can add gas cost. This part can be written to reduce the gas cost, but this sacrifices the readability a bit.
Summary
!= 0
instead of> 0
on uint variablesStrings.sol
import at PuttyV2Nft.sol[Gas-1] Use
!= 0
instead of> 0
on uint variablesuint variables will never be lower than 0. Therefore,
> 0
and!= 0
have same meanings. Using!= 0
can reduce the gas deployment cost, so it is worth using!= 0
wherever possible.Here are list of the gas improvements by using
!= 0
.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#L327
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L351
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L427
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L498
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
[Gas-2] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
Here are list of the gas improvements.
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L497
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L556
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L594
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L611
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L627
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L637
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L647
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L658
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L670
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L728
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L742
[Gas-3] Gas cost of ++i is smaller than i++
Here are gas optimization opportunities.
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L556
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L594
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L611
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L627
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L637
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L647
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L658
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L670
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L728
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L742
[Gas-4] Use custom errors
Using custom errors can reduce the gas cost.
[Gas-5] Unnecessary
Strings.sol
import at PuttyV2Nft.solImport of
Strings.sol
is not necessary. It can remove this line completely.https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2Nft.sol#L5
[Gas-6] Flip the boolean check to reduce gas cost
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L404-L406
It uses
!order.isCall
but just removing!
and changing order can reduce a gas cost.[Gas-7] Not splitting the strings
https://github.com/code-423n4/2022-06-putty/blob/main/contracts/src/PuttyV2.sol#L101-L122
abi.encodePacked
is used to combine the strings. This is good for the readability, but combining the strings usingabi.encodePacked
can add gas cost. This part can be written to reduce the gas cost, but this sacrifices the readability a bit.