1/5
_Follow along with this video:_ --- ### Storage Refresher With our function dispatcher and interfaces set up, we're finally ready to add some logic to our macros! We're going to begin with `SET_NUMBER_OF_HORSES()` as one of our first steps will be assigning a storage slot to our `numberOfHorses` variable. When we think of storage, we can think of it as a giant, persistent array. When a transaction's execution completes, storage remains and is not cleared. In Solidity each variable is mapped to a storage slot sequentially as they are defined in the contract. It's important to note however: - Constant Variables are not saved to storage - Mappings and arrays are handled uniquely using a hashing algorithm to assign the storage locations of their contents. Specific details on this can be found in the [**Ethereum Documentation**](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html). - Memory variables are not saved to storage So, in order to update storage, there are a few things we need to do. ```js #define macro SET_NUMBER_OF_HORSES() = takes(0) returns(0){ // 1. Give numberOfHorses a storage slot // 2. Get the value to store from call data // 3. Execute the SSTORE op code } ``` Great! Once we've implemented this logic, we're going to be able to write some tests to compare the functionality of our Huff contract with that of our Solidity version, and - hopefully - we'll see they do exactly the same thing. Let's go!
A comprehensive guide to creating a Huff smart contract that stores and retrieves information using storage slots. This lesson covers the basics of Huff, including function dispatching, function selectors, and the mechanics of storage, and concludes with the creation of tests to confirm the Huff implementation aligns with a Solidity implementation.
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