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

5 stars 0 forks source link

Gas Optimizations #411

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. changed using ++i than i++ for cost less gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++.

/contracts/src/PuttyV2.sol#L556        for (uint256 i = 0; i < orders.length; i++) {
/contracts/src/PuttyV2.sol#L611        for (uint256 i = 0; i < assets.length; i++) {
/contracts/src/PuttyV2.sol#L627        for (uint256 i = 0; i < assets.length; i++) {
/contracts/src/PuttyV2.sol#L637        for (uint256 i = 0; i < floorTokens.length; i++) {
/contracts/src/PuttyV2.sol#L647        for (uint256 i = 0; i < assets.length; i++) 
/contracts/src/PuttyV2.sol#L658         for (uint256 i = 0; i < floorTokens.length; i++) {
/contracts/src/PuttyV2.sol#L670         for (uint256 i = 0; i < whitelist.length; i++) {
/contracts/src/PuttyV2.sol#L728         for (uint256 i = 0; i < arr.length; i++) {
/contracts/src/PuttyV2.sol#L742         for (uint256 i = 0; i < arr.length; i++) {
  1. short reason string can be used for saving more gas

Every reason string takes at least 32 bytes. Use short reason strings that fits in 32 bytes or it will become more expensive.

/contracts/src/PuttyV2.sol#L297  "Wrong amount of floor tokenIds"
/contracts/src/PuttyV2.sol#L298  "Invalid floor tokens length"
/contracts/src/PuttyV2.sol#L405 "Wrong amount of floor tokenIds"
/contracts/src/PuttyV2.sol#L406  "Invalid floor tokens length"
/contracts/src/PuttyV2.sol#L481  "Must be exercised or expired"
/contracts/src/PuttyV2.sol#L765 "URI query for NOT_MINTED token"
  1. Saving gas by removing = 0

This implementation code can be saving more gas by removing = 0, it because If a variable was not set/initialized, it is assumed to have default value to 0

/contracts/src/PuttyV2.sol#L497            uint256 feeAmount = 0;
  1. change uint256 i = 0 into uint i for saving more gas

using this implementation can saving more gas for each loops.

/contracts/src/PuttyV2.sol#L556        for (uint256 i = 0; i < orders.length; i++) {
/contracts/src/PuttyV2.sol#L611        for (uint256 i = 0; i < assets.length; i++) {
/contracts/src/PuttyV2.sol#L627        for (uint256 i = 0; i < assets.length; i++) {
/contracts/src/PuttyV2.sol#L637        for (uint256 i = 0; i < floorTokens.length; i++) {
/contracts/src/PuttyV2.sol#L647        for (uint256 i = 0; i < assets.length; i++) 
/contracts/src/PuttyV2.sol#L658         for (uint256 i = 0; i < floorTokens.length; i++) {
/contracts/src/PuttyV2.sol#L670         for (uint256 i = 0; i < whitelist.length; i++) {
/contracts/src/PuttyV2.sol#L728         for (uint256 i = 0; i < arr.length; i++) {
/contracts/src/PuttyV2.sol#L742         for (uint256 i = 0; i < arr.length; i++) {