Skip to content
Smart contracts Audit finding Beginner

Storage slots only set in constructor should be declared immutable

Two state variables in a smart contract are set only in the constructor but are not declared immutable, so they occupy storage slots. This causes unnecessary gas costs when the contract reads them.

Victor_TheOracle Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

#41765 [SC-Insight] Storage slots only set in constructor should be declared `immutable`

Submitted on Mar 18th 2025 at 07:54:52 UTC by @Victor_TheOracle for Audit Comp | Yeet

Description

Brief/Intro

The issue involves two state variables in the yeetback.sol contract that are set only during construction but are not declared as immutable. In non-upgradeable contracts, failing to mark such variables as immutable results in unnecessary gas costs since these variables occupy storage slots, potentially increasing the cost of contract interactions on mainnet.

Vulnerability Details

In Solidity, variables that are assigned a value only once in the constructor and never modified should be declared as immutable. This allows the Solidity compiler to optimize these variables by embedding their values directly into the bytecode rather than storing them in a storage slot.

In yeetback.sol, the variables entropy and entropyProvider are initialized in the constructor but are not declared as immutable:

Solidity
/// @dev The entropy contract address
//@audit (info) -----> Should be immutable
IEntropy private entropy;
/// @dev The address of the entropy provider
//@audit (info) -----> Should be immutable
address private entropyProvider;
Solidity
constructor(address _entropy, address _entropyProvider) Ownable(msg.sender) {
    require(_entropy != address(0), "Yeetback: Invalid entropy address");
    require(_entropyProvider != address(0), "Yeetback: Invalid entropy provider address");
    entropy = IEntropy(_entropy);
    entropyProvider = _entropyProvider;
}

Impact Details

The main impact of this vulnerability is increased gas consumption during contract execution. By storing these values in storage rather than embedding them in the contract's code, each access to these variables requires an SLOAD operation, which is more gas-intensive.

References

Relevant Code snippet: https://github.com/immunefi-team/audit-comp-yeet/blob/da15231cdefd8f385fcdb85c27258b5f0d0cc270/src/Yeetback.sol#L33-L35

Proof of Concept

Proof of Concept

The original report as published on Immunefi, by Victor_TheOracle. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The variables entropy and entropyProvider are assigned only in the constructor but are declared as regular storage variables instead of immutable.

Impact

Each access to these variables requires a more expensive storage read (SLOAD), increasing gas consumption during contract execution.

What the attacker needed

None

The fix

The variables should be declared immutable so their values are embedded in the bytecode and reads become cheaper.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

In a smart contract, some values are set once when the contract is created and never change. If they are stored in the contract's permanent memory (storage), reading them later costs extra fees (gas). Marking them as immutable lets the compiler bake the values into the code, making reads cheaper.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Victor_TheOracle

View all

4 more disclosed reports, best first.

More from Yeet

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.