Normal Behavior:
In Solidity, the immutable
keyword can be used for state variables that are assigned once during contract construction and never changed thereafter. Marking such variables as immutable
saves gas on every read and makes the contract’s intent clearer to auditors and developers, as it guarantees the value cannot be changed after deployment.
Issue:
In the FestivalPass
contract, the beatToken
address is set in the constructor and never changes, but it is not marked as immutable
. This results in unnecessary gas costs for every read and makes it less clear to reviewers that the address is intended to be permanent.
Relevant Code Example:
Likelihood:
This will occur in every deployment and every read of the beatToken
variable, as the lack of immutable
is a code quality and gas optimization issue.
The risk is present in all contracts where constructor-assigned addresses are not marked as immutable
.
Impact:
Gas Inefficiency: Every read of beatToken
costs more gas than necessary, especially in functions that are called frequently or in loops.
Intent Ambiguity: Auditors and developers may not immediately realize that beatToken
is meant to be permanent, increasing the risk of misunderstanding or accidental modification in future upgrades.
To observe the gas inefficiency, copy and paste the following test code into your test file (e.g., test/contract.t.sol
). This test compares the gas usage of reading a regular storage variable versus an immutable
variable:
Explanation:
This test deploys a contract with both a regular and an immutable address variable.
It measures the gas used to read each variable.
The read from the immutable
variable is cheaper, demonstrating the benefit.
Mark the beatToken
variable as immutable
to save gas and clarify intent.
Summary:
Using immutable
for constructor-assigned variables is a best practice for gas optimization and code clarity. It is a low-effort, high-impact improvement that should be applied to all relevant variables.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.