Use != 0 instead of > 0 when comparing unsigned integers
!= 0 will do the same as > 0 for unsigned integers, but != 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled.
Instances include :
PuttyV2.sol:293: require(order.baseAsset.code.length > 0, "baseAsset is not contract");
PuttyV2.sol:598: require(token.code.length > 0, "ERC20: Token is not contract");
PuttyV2.sol:599: require(tokenAmount > 0, "ERC20: Amount too small");
Recommendation
It is recommended to replace > 0 with != 0, as they do the same thing for unsigned integers, and '!= 0' costs less gas compared to > 0 in require statements with the optimizer enabled, also enable the optimizer.
For example :
PuttyV2.sol:599: require(tokenAmount != 0, "ERC20: Amount too small");
Don't explicitly initialize variables with the default value
Uninitialized variables are assigned with the default value of their type, initializing a variable with its default value costs unnecessary gas.
Instances include :
PuttyV2.sol:497: uint256 feeAmount = 0;
PuttyV2.sol:556: for (uint256 i = 0; i < orders.length; i++) {
PuttyV2.sol:594: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:611: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:627: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:637: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:647: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:658: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:670: for (uint256 i = 0; i < whitelist.length; i++) {
PuttyV2.sol:728: for (uint256 i = 0; i < arr.length; i++) {
PuttyV2.sol:742: for (uint256 i = 0; i < arr.length; i++) {
Recommendation
It is recommended to initialize variables without assigning them the default value, for example :
PuttyV2.sol:497: uint256 feeAmount;
Cache array length outside of for loop
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.
Instances include :
PuttyV2.sol:556: for (uint256 i = 0; i < orders.length; i++) {
PuttyV2.sol:594: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:611: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:627: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:637: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:647: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:658: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:670: for (uint256 i = 0; i < whitelist.length; i++) {
PuttyV2.sol:728: for (uint256 i = 0; i < arr.length; i++) {
PuttyV2.sol:742: for (uint256 i = 0; i < arr.length; i++) {
Recommendation
It is recommended to cache the array length on a variable before running the loop, then it doesn't need to read the length on every iteration, which cost gas, for example :
uint256 len = orders.length;
PuttyV2.sol:556: for (uint256 i = 0; i < len; i++) {
If possible, use prefix increment instead of postfix increment
Prefix increment ++i returns the updated value after it's incremented and postfix increment i++ returns the original value then increments it. Prefix increment costs less gas compared to postfix increment.
Instances includes :
PuttyV2.sol:556: for (uint256 i = 0; i < orders.length; i++) {
PuttyV2.sol:594: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:611: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:627: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:637: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:647: for (uint256 i = 0; i < assets.length; i++) {
PuttyV2.sol:658: for (uint256 i = 0; i < floorTokens.length; i++) {
PuttyV2.sol:670: for (uint256 i = 0; i < whitelist.length; i++) {
PuttyV2.sol:728: for (uint256 i = 0; i < arr.length; i++) {
PuttyV2.sol:742: for (uint256 i = 0; i < arr.length; i++) {
Recommendation
It is recommended to use prefix increment instead of postfix one when the return value is not needed, as both of them will give the same result and prefix increment costs less gas.
For example :
PuttyV2.sol:556: for (uint256 i = 0; i < orders.length; ++i) {
Use != 0 instead of > 0 when comparing unsigned integers
!= 0 will do the same as > 0 for unsigned integers, but != 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled.
Instances include :
Recommendation
It is recommended to replace
> 0
with!= 0
, as they do the same thing for unsigned integers, and '!= 0' costs less gas compared to> 0
in require statements with the optimizer enabled, also enable the optimizer.For example :
Don't explicitly initialize variables with the default value
Uninitialized variables are assigned with the default value of their type, initializing a variable with its default value costs unnecessary gas.
Instances include :
Recommendation
It is recommended to initialize variables without assigning them the default value, for example :
Cache array length outside of for loop
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.
Instances include :
Recommendation
It is recommended to cache the array length on a variable before running the loop, then it doesn't need to read the length on every iteration, which cost gas, for example :
If possible, use prefix increment instead of postfix increment
Prefix increment
++i
returns the updated value after it's incremented and postfix incrementi++
returns the original value then increments it. Prefix increment costs less gas compared to postfix increment.Instances includes :
Recommendation
It is recommended to use prefix increment instead of postfix one when the return value is not needed, as both of them will give the same result and prefix increment costs less gas.
For example :