You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can change the order of the storage variables to decrease memory uses.
In InsuranceFund.sol,rearranging the storage fields can optimize to: 4 slots from: 5 slots.
The new order of types (you choose the actual variables):
1. uint
2. IERC20
3. uint
4. address
5. uint8
Title: Prefix increments are cheaper than postfix increments
Severity: GAS
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the ++i in for (uint256 i = 0; i < numIterations; ++i)).
But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (++x) or not using the unchecked keyword:
change to prefix increment and unchecked: MarginAccount.sol, i, 552
change to prefix increment and unchecked: ClearingHouse.sol, i, 194
change to prefix increment and unchecked: ClearingHouse.sol, i, 277
change to prefix increment and unchecked: MarginAccount.sol, i, 373
change to prefix increment and unchecked: MarginAccount.sol, i, 521
change to prefix increment and unchecked: ClearingHouse.sol, i, 122
change to prefix increment and unchecked: ClearingHouse.sol, i, 251
change to prefix increment and unchecked: MarginAccount.sol, i, 331
change to prefix increment and unchecked: ClearingHouse.sol, i, 170
change to prefix increment and unchecked: ClearingHouse.sol, i, 263
change to prefix increment and unchecked: ClearingHouse.sol, i, 130
Title: Public functions to external
Severity: GAS
The following functions could be set external to save gas and improve code quality.
External call cost is less expensive than of public functions.
Title: Short the following require messages
Severity: GAS
The following require messages are of length more than 32 and we think are short enough to short
them into exactly 32 characters such that it will be placed in one slot of memory and the require
function will cost less gas.
The list:
Solidity file: AMM.sol, In line 511, Require message length to shorten: 38, The message: VAMM._short: baseAssetQuantity is >= 0
Solidity file: MarginAccount.sol, In line 453, Require message length to shorten: 37, The message: Need to repay more to seize that much
Solidity file: AMM.sol, In line 487, Require message length to shorten: 37, The message: VAMM._long: baseAssetQuantity is <= 0
Solidity file: ClearingHouse.sol, In line 101, Require message length to shorten: 34, The message: CH: Below Minimum Allowable Margin
Title: Unnecessary index init
Severity: GAS
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas.
It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
Unused state variables are gas consuming at deployment (since they are located in storage) and are
a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.
Title: Caching array length can save gas
Severity: GAS
Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory.
We recommend to change from:
Title: Storage double reading. Could save SLOAD
Severity: GAS
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the
overall gas uses. The following is a list of functions and the storage variables that you read twice:
MarginAccount.sol: PRECISION is read twice in isLiquidatable
Title: Unused imports
Severity: GAS
In the following files there are contract imports that aren't used
Import of unnecessary files costs deployment gas (and is a bad coding practice that is important to ignore)
InsuranceFund.sol, line 10, import { IRegistry } from "./Interfaces.sol";
AMM.sol, line 4, import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
Oracle.sol, line 4, import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
ClearingHouse.sol, line 7, import { IAMM, IInsuranceFund, IMarginAccount, IClearingHouse } from "./Interfaces.sol";
Oracle.sol, line 8, import { AggregatorV3Interface } from "./Interfaces.sol";
AMM.sol, line 8, import { ERC20Detailed, IOracle, IRegistry, IVAMM, IAMM } from "./Interfaces.sol";
Title: State variables that could be set immutable
Severity: GAS
In the following files there are state variables that could be set immutable to save gas.
vusd in MarginAccount.sol
insuranceFund in ClearingHouse.sol
name in AMM.sol
marginAccount in ClearingHouse.sol
vamm in AMM.sol
vusd in ClearingHouse.sol
underlyingAsset in AMM.sol
Title: Unnecessary default assignment
Severity: GAS
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
Title: Rearrange state variables
Severity: GAS
You can change the order of the storage variables to decrease memory uses.
In InsuranceFund.sol,rearranging the storage fields can optimize to: 4 slots from: 5 slots.
The new order of types (you choose the actual variables):
Title: Prefix increments are cheaper than postfix increments
Severity: GAS
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the
++i
infor (uint256 i = 0; i < numIterations; ++i)
).But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (
++x
) or not using the unchecked keyword:Title: Public functions to external
Severity: GAS
The following functions could be set external to save gas and improve code quality.
External call cost is less expensive than of public functions.
Title: Consider inline the following functions to save gas
Severity: GAS
Title: Unnecessary cast
Severity: Gas
Title: Inline one time use functions
Severity: GAS
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
Title: Unnecessary equals boolean
Severity: GAS
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
Title: Short the following require messages
Severity: GAS
The following require messages are of length more than 32 and we think are short enough to short
them into exactly 32 characters such that it will be placed in one slot of memory and the require
function will cost less gas.
The list:
Title: Unnecessary index init
Severity: GAS
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas.
It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
Title: Unused state variables
Severity: GAS
Unused state variables are gas consuming at deployment (since they are located in storage) and are
a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.
Title: Unnecessary constructor
Severity: GAS
The following constructors are empty.
(A similar issue code-423n4/2021-11-fei-findings#12)
Title: Caching array length can save gas
Severity: GAS
Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory.
We recommend to change from:
to:
Title: Storage double reading. Could save SLOAD
Severity: GAS
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the
overall gas uses. The following is a list of functions and the storage variables that you read twice:
Title: Unused imports
Severity: GAS
In the following files there are contract imports that aren't used
Import of unnecessary files costs deployment gas (and is a bad coding practice that is important to ignore)
Title: State variables that could be set immutable
Severity: GAS
In the following files there are state variables that could be set immutable to save gas.
Title: Unnecessary default assignment
Severity: GAS
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
Title: Use calldata instead of memory
Severity: GAS
Use calldata instead of memory for function parameters
In some cases, having function arguments in calldata instead of
memory is more optimal.
Title: Internal functions to private
Severity: GAS
The following functions could be set private to save gas and improve code quality:
Title: Use != 0 instead of > 0
Severity: GAS
Using != 0 is slightly cheaper than > 0. (see code-423n4/2021-12-maple-findings#75 for similar issue)
The text was updated successfully, but these errors were encountered: