1
- constant keyword
    - uint256 public constant MINIMUM_USD = 50 * 1e18;
    - Helps in Gas Optimization
- immutable keyword
    - The difference is that constant variables can never be changed after compilation, while immutable variables can be set within the constructor but cannot be changed later on.
    - Helps in Gas Optimization
    - address public immutable i_owner

I know and understand this much of theory but unable to figure out in what cases is immutable used and in what cases is constant used?

2 Answers 2

4
  • constant variables should be used when you’re hardcoding particular values to the corresponding variables, and want them to remain the same throughout the contract.

    Some examples would be MIN or MAX constants, that you need for comparisons, DECIMAL_PLACES that want to use to format other numeric values, and some existing token addresses like WETH, USDC, etc.

    uint256 public constant MAX_AMOUNT_TO_MINT = 1000e18:
    uint256 public constant MIN_PASSING_MARKS = 33;
    uint256 public constant DECIMAL_PLACES = 18;
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    
  • immutable variables should be used when you want to assign the values to the variables during the contract’s deployment i.e., in the constructor, and after that you never want them to be changed.

    Some examples would be the address of the owner (if you don’t want to transfer the ownership), some configuration parameters like routerAddress, etc. that you want to assign depending on the network, and any other variables that you want to be assigned only once via constructor args.

    error NetworkNotSupported();
    
    address public immutable i_owner;
    address public immutable i_routerAddress;
    uint256 public immutable i_totalSupply;
    
    constructor(_owner, _totalSupply) {
        i_owner = _owner;
        i_totalSupply = totalSupply
        if (block.chainid == 1) {
           i_routerAddress = 0x1111111111111111111111111111111111111111;
        }
        else if (block.chainid == 8453) {
           i_routerAddress = 0x2222222222222222222222222222222222222222;
        }
        else {
           revert NetworkNotSupported();
        }
    }
    
2

I just want to complete the other answer(s) and talk about gas cost.

If you wanted to know which one would be gas efficient and you knew the value while writing your code, using constant would be the efficient way to do it.

Based on this example, both of them will copy the value where it is used and is much more efficient for accessing and lot of other scenarios. The difference is that constant will use only the storage it needs while immutable will use 32 bytes, even in places were it can be less (ex. addresses, 20 bytes), so there are many places where using constant instead of immutable would be preferable. That is to say, declaring a uint8 type, using immutable, would require 32 times the storage that constant needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.