Better Programming

Advice for programmers.

Follow publication

Member-only story

All About Solidity Data Locations — Storage

Jean Cvllr
Better Programming
Published in
19 min readJul 20, 2022

This is Part I of the “All About Data Locations” sub-series.

In today's article, we cover in more detail an important data location in the EVM: the smart contract storage.

We will see how the layout of the contract storage works, storage references. how to write to the contract storage in assembly. We will also use some contracts from OpenZeppelin and Compound to learn how storage references work in practice while learning the Solidity code behind these popular contracts and protocols along the way.

Table of Content

  • Introduction
  • Layout of Storage
  • Basics of Storage
  • Interacting with Storage
  • Storage pointers in function parameters
  • Storage pointers in function body
  • The cost of reading storage.
  • Conclusion

Introduction

Understanding the storage model in Ethereum and EVM-based chain is essential for good smart contract development.

The only place where you can store data permanently on a smart contract so that it can then be accessed for future executions is in its storage. Each smart contract maintains its state in its own permanent storage. It acts like “a mini-database for a smart contract”, but unlike other databases, this database is publicly accessible. All the values stored in the smart contract storage are available for external reading for free (via static calls), without the need to send a transaction to the blockchain.

Writing to the storage, however, is quite expensive. Actually, it is the most expensive operation in the EVM when it comes to gas cost. The content of the storage can be changed with sendTransaction calls. Such calls change state. This is the reason why contract-level variables are referred to as state variables.

One important thing to remember is that by design in Ethereum and the EVM, a contract can neither read nor write any storage apart from its own. The only way contract A can read or write from…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jean Cvllr
Jean Cvllr

Written by Jean Cvllr

Smart Contract engineer at @LUKSO. Full Stack Developer. https://github.com/CJ42

Write a response