1/5
_Follow along with this video:_ --- ### Assigning Storage Slots Our numberOfHorses variable requires a storage slot, and there's a number of ways we could approach this. We could absolutely hardcode a storage slot for example. Fortunately, Huff makes working with storage slots a little bit easier through a convenient abstraction: `FREE_STORAGE_POINTER()`. `FREE_STORAGE_POINTER()` effectively works as a counter for available storage slots. Let's add this to our code base. ```js /*Interface*/ #define function updateHorseNumber(uint256) nonpayable returns() #define function readNumberOfHorses() view returns(uint256) #define constant NUMBER_OF_HORSES_STORAGE_SLOT = FREE_STORAGE_POINTER() #define macro MAIN() = takes(0) returns(0){ 0x00 calldataload 0xe0 shr // [function_selector] // updateHorseNumber, 0xcdfead2e dup1 __FUNC_SIG(updateHorseNumber) eq updateJump jumpi // [function_selector] // readNumberOfHorses, 0xe026c017 __FUNC_SIG(readNumberOfHorses) eq readJump jumpi // [] 0x00 0x00 revert updateJump: SET_NUMBER_OF_HORSES() readJump: GET_NUMBER_OF_HORSES() } #define macro SET_NUMBER_OF_HORSES() = takes(0) returns(0){} #define macro GET_NUMBER_OF_HORSES() = takes(0) returns(0){} ``` What the line `#define constant NUMBER_OF_HORSES_STORAGE_SLOT = FREE_STORAGE_POINTER()` is doing is defining a particular storage slot to be used for this constant variable. `FREE_STORAGE_POINTER()` will return the next sequentially available storage slot, assigning our constant, in this case, to 0. The next time `FREE_STORAGE_POINTER()` is called it will assign 1. This is exactly how Solidity handles things as well! ::image{src='/formal-verification-1/29-free-storage-pointer/free-storage-pointer-1.png' style='width: 50%; height: auto;'} With our above implementation, any time somebody looks at storage slot 0, that's where numberOfHorses will be found. Great work!
A beginner’s guide to working with storage slots in Huff - This lesson covers how storage slots work in Huff, and how they relate to Solidity. It introduces the keyword `FREE_STORAGE_POINTER`, which helps manage storage slots in a more concise way. The lesson also shows a simple implementation of a storage variable in Huff, and explains how to use it to store the number of horses in a smart contract.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
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