Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: low
Invalid

Non-Immutable Contract References in EggHuntGame Waste Gas

Summary

The EggHuntGame contract contains two state variables, eggNFT and eggVault, which are set in the constructor and never modified afterward. However, these variables are not declared as immutable, resulting in unnecessary storage reads and higher gas costs.

Vulnerability Details

The contract references are defined as regular state variables instead of immutable variables:

/// @notice References to the EggstravaganzaNFT and EggVault contracts.
EggstravaganzaNFT public eggNFT;
EggVault public eggVault;

These references are initialized in the constructor and never changed throughout the contract's lifecycle:

constructor(address _eggNFTAddress, address _eggVaultAddress) Ownable(msg.sender){
require(_eggNFTAddress != address(0), "Invalid NFT address");
require(_eggVaultAddress != address(0), "Invalid vault address");
eggNFT = EggstravaganzaNFT(_eggNFTAddress);
eggVault = EggVault(_eggVaultAddress);
}

Impact

  • Increased gas costs for all functions that read these variables

  • Missed optimization opportunity that would benefit users of the contract

  • Slightly higher deployment costs

Every time a function like searchForEgg() or depositEggToVault() is called, the contract must read these variables from storage, which costs more gas than reading from immutable variables that are stored in the contract bytecode.

Tools Used

Manual review

Recommendations

Declare the contract references as immutable to save gas and improve contract efficiency:

/// @notice References to the EggstravaganzaNFT and EggVault contracts.
EggstravaganzaNFT public immutable eggNFT;
EggVault public immutable eggVault;

This change will reduce gas costs for users interacting with the contract and is a best practice for variables that are set once and never changed.

Updates

Lead Judging Commences

m3dython Lead Judge about 2 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Gas optimization

Strategy to save gas and minimize transaction costs

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.