_Follow along with this video:_ --- ### Unchanged State Variables Should Be Constant or Immutable Searching for our @Audit comment again, it looks like the next finding we identified was: ```js // @Audit-Gas: raffleDuration doesn't change and should be immutable. ``` Now, just a few lines further in the contract, we've also noted that several variables should be `constant`. ```js // @Audit-Gas: Unchanged state variables can be marked as constant string private commonImageUri = "ipfs://QmSsYRx3LpDAb1GZQm7zZ1AuHZjfbPkD6J7s9r41xu1mf8"; string private rareImageUri = "ipfs://QmUPjADFGEKmfohdTaNcWhp7VGk26h5jXDA7v3VtTnTLcW"; string private legendaryImageUri = "ipfs://QmYx6GsYAKnNzZ9A6NvEKV9nf1VaDzJrqDR23Y8YSkebLU"; ``` We should compile these into a single gas issue in our `findings.md` document. ```md #Gas ### [G-1] Unchanged state variables should be declared constant or immutable Reading from storage is much more expensive than reading a constant or immutable variable. Instances: - `PuppyRaffle::raffleDuration` should be `immutable` - `PuppyRaffle::commonImageUri` should be `constant` - `PuppyRaffle::rareImageUri` should be `constant` - `PuppyRaffle::legendaryImageUri` should be `constant` ``` Great! Done! Make note in the contract that we've written up this finding and lets move on to the next.
Learn how to track gas consumption and identify where improvements can be made. Focus on gas optimization techniques and proper variable scoping.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Solidity Developer
Smart Contract SecurityDuration: 25min
Duration: 1h 18min
Duration: 35min
Duration: 2h 28min
Duration: 5h 03min
Duration: 5h 22min
Duration: 4h 33min
Duration: 2h 01min
Duration: 1h 40min
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar