code-423n4 / 2021-09-defiprotocol-findings

1 stars 0 forks source link

State variables that can be set to immutable #233

Closed code423n4 closed 2 years ago

code423n4 commented 3 years ago

Handle

hrkrshnn

Vulnerability details

State variables that can be set to immutable

Solidity 0.6.5 introduced immutable as a major feature. It allows setting contract-level variables at construction time which gets stored in code rather than storage.

Consider the following generic example:

contract C {
    /// The owner is set during contruction time, and never changed afterwards.
    address public owner = msg.sender;
}

In the above example, each call to the function owner() reads from storage, using a sload. After EIP-2929, this costs 2100 gas cold or 100 gas warm. However, the following snippet is more gas efficient:

contract C {
    /// The owner is set during contruction time, and never changed afterwards.
    address public immutable owner = msg.sender;
}

In the above example, each storage read of the owner state variable is replaced by the instruction push32 value, where value is set during contract construction time. Unlike the last example, this costs only 3 gas.

Examples

A non-exhaustive list of examples:

  1. https://github.com/code-423n4/2021-09-defiProtocol/blob/main/contracts/contracts/Factory.sol#L26
  2. https://github.com/code-423n4/2021-09-defiProtocol/blob/main/contracts/contracts/Factory.sol#L27
  3. https://github.com/code-423n4/2021-09-defiProtocol/blob/main/contracts/contracts/Basket.sol#L22
  4. https://github.com/code-423n4/2021-09-defiProtocol/blob/main/contracts/contracts/Basket.sol#L23
GalloDaSballo commented 2 years ago

Because factory is immutable, I agree with the finding

GalloDaSballo commented 2 years ago

Duplicate of #15