OneSwap Series 7 — Basic Data Structures

Every smart contract on Ethereum can read and write a dedicated KVStore, and both Key and Value are 256 bits long. Of course, you can also regard the KVStore as a huge array with a length of 2256, and each element must be a 256-bit-long byte sequence. Such a large KVStore/array has a cost. At present, it takes 800 Gas to access KVStore once (using the SLOADinstruction) and 20,000 Gas for the first writing (using the SSTOREinstruction). If you access a key/index that has never been written, 0 is returned; if you write 0, it is equivalent to deleting the value at the key/index (if there is a non-zero value here). To encourage smart contracts to delete useless KV pairs and save storage space, the system will refund 15,000 Gas when KV pairs are deleted (but the total Gas refunded for a transaction cannot exceed half of the total Gas consumption).

When we write smart contracts using Solidity, we don’t need to directly consider the SLOADand SSTOREinstructions. We can define state variables in Solidity contracts, which will be stored in KVStore in a certain order based on some rules (to be determined by the compiler). In addition to simple value variables (such as addresses, booleans, and various types of integers), Solidity also supports arrays and mapping. On that basis, we can also construct data structures such as Set, List, Stack, and Queue. This article will discuss the various storage variables supported by Solidity and their implementation details as well as the principles and applications of various data structures with OneSwap as a reference.

How State Variables Work?

Let’s first discuss the state variables supported by the Solidity language and see how these variables occupy storage space. We will take a simple contract as an example to analyze its compiled runtime bytecode (for an introduction to the runtime bytecode, please refer to the second article in this series). Here is the complete code for this example contract:

Simple Value Variables

If a smart contract only uses state variables of value type and each state variable occupies one slot (256 bits), then these state variables will be stored in the KVStore in the defined order. In other words, the Nth state variable is in the Nth slot (N starts from 0). As mentioned earlier, the reading and writing of storage space are very Gas-consuming, so the compiler needs to try its best to pack multiple small-length value variables into one slot. Currently, the Solidity compiler has been optimized for this purpose but it will not rearrange these state variables. Therefore, to optimize Gas consumption to the utmost, the programmer must carefully arrange the state variables of the contract by himself.

In the example contract above, the first three state variables are value types, and the first two state variables can be placed in one slot. So these three state variables occupy a total of two slots, and the indexes are 0 and 1. This can be confirmed through the analysis of the runtime bytecode after the contract is compiled. To better observe the bytecode, we can use the online disassemblerto disassemble the code. The following is the disassembled result of the setV123()function:

The above logic can be expressed in pseudo code as:

Almost all contracts use state variables of the value type, so there is no need to take examples one by one here. In addition, if the state variable is a simple structure type (the fields are all the value type), it will be implemented similarly: the compiler will try to put as many consecutive fields as possible into one slot, and reserve enough slots for the entire structure. The state variables of the structure type are not to be introduced in detail, and you can analyze the variables using the method described in this article.

Fixed-length Array

Solidity supports two types of arrays: fixed-length arrays and variable-length arrays. The length of the fixed-length array is known at compile time, so the Solidity compiler will reserve enough slots for the array. In other words, if the length of the fixed-length array is L, the compiler will reserve L slot(s). Assuming that other variables have used N slots before a fixed-length array, then the Mth element of this fixed-length array will be placed in the N+Mth slot (L, M, N all start from 0).

In the above example contract, a1 is a fixed-length array with a length of 0x2000. Two slots have been used before, so the compiler reserves the following 0x2000 slots for a1. We can confirm this by observing the bytecodes of the setA1() and setA2() functions. The following is the disassembled result of the setA1() function:

As you can see, since the length of the fixed-length array is determined at compile time, the compiler also checks the validity of the index by the way. The above logic can be expressed in pseudo code as:

OneSwap has implemented an order book using two fixed-length arrays in the OneSwapPaircontract. The key code is as follows (the contract will be introduced again when we discuss the linked list later):

Variable-length Array

Unlike fixed-length arrays, the length of variable-length arrays can be known only at runtime and can change dynamically. Since the length is variable, slots cannot be reserved like in fixed-length arrays. Solidity reserves one slot for the fixed-length array to record the actual length of the array. The starting slot of the array is calculated by the hash algorithm (keccak256) on the basis of the slot reserved for the array. Assuming that N slots have been used before a variable-length array, the length of the array is recorded in the Nth slot; the Mth element of the array is stored in the hash(N) + Mth slot (M and N both start from 0).

In the above example contract, a2is a variable-length array, and 0x2002slots have been used before it, so the Solidity compiler records the length of a2in the 0x2002th slot and the starting index of a2is hash(0x2002). We can confirm this by observing the bytecode of the setA2()function. The following is the disassembled result of the function:

As you can see, the compiler also checks the validity of the array index. The above logic can be expressed in pseudo code as:

OneSwap uses a variable-length array (allPairsfield) in the OneSwapFactorycontract to record the addresses of all trading pairs it creates. The key code is as follows:

Simple Mapping

In Solidity, the mapped Key must be a built-in value type, but Value can be a value type or a complex type such as structure, array, and mapping. The fundamental principle remains the same. Let’s first look at how the simplest mapping (Value is a value type) works. Assuming that N slots has been used before a mapping, the compiler will reserve the Nth slot for the mapping (instead of storing the data). And the slot corresponding to a Key K of this mapping is the hash result of the concatenating of K and N, which is hash(K, N).

In the above example contract, m1 is a simple mapping, its Key is an address type, and Value is an integer of uint256. Before m1 0x2003 slots have been used, so for m1, the slot corresponding to Key K is hash(K, 0x2003). We can confirm this by observing the bytecode of the setM1() function. The following is the disassembled result of the function (manually generated by the author):

The above logic can be expressed in pseudo code as:

Many contracts in OneSwap use mapping. For example, the OneSwapFactory contract mentioned above defines two mappings (_tokensToPair and _pairWithToken). It is worth noting that if we use the mapping alone, we cannot iterate through the KV pairs. To obtain the iteration capability, the mapping often needs to be used with an array. For example, the allPairs array in the OneSwapFactory contract records addresses of all trading pairs, making it possible to iterate through all trading pairs created by the factory.

Complex Mapping

As mentioned earlier, the mapped Value can also be a complex type, such as structure, array, and mapping. Here we only analyze the most commonly used case where Value is a simple mapping. You can use the method described in this article to analyze other cases by yourself. In the above example contract, m2 defines a "mapping of mapping", and the setM2() function operates on the mapping. This time, let’s first take a look at the decompilation result of the setM2() function (manually generated by the author):

It is clear from the above code that the compiler also reserves one slot for m1. Through twice hashes on the basis of the slot index and the actual key, we can get the final slot index of Value. Assuming that the slot occupied by the mapping is N, the compiler will reserve the Nth slot. Assuming that the two Keys are K1and K2respectively, the slot occupied by Value is hash(K2, hash(K1, N)). The logic of the above function can be expressed in pseudo code as:

In OneSwap, the OneSwapTokencontract uses the "mapping of mapping" to implement the "approve" function of ERC20 token. The key states of the contract is given below:

Note that despite the huge space of KVStorage provided by EVM and the strong collision resistance of the keccak256 hash algorithm, collisions remain possible theoretically, but the possibility is too small to be considered. However, the use of variable length arrays will increase the possibility of collisions and may bring safety hazards, so prudent treatment is required. For more discussion on Ethereum storage hash collision, please refer to this article.

Data Structure

Arrays and mappings are the basic data structures provided by the Solidity language. On this basis, it is easy to construct data structures such as Sets, Linked Lists, Stacks, and Queues. Next, we will introduce in brief the implementation of these data structures and their application in OneSwap.


The implementation of the Set is very simple. Just define a mapping where Value is a Boolean type. For example, the OneSwapBlackList abstract contract uses a collection to record blacklisted users. The key code is as follows:

Like mapping, the set is not iterable. If you want to implement a iterable set, you need the help of an additional array. For example, the OneSwapBuybackcontract uses mapping and arrays to implement a iterable list of mainstream tokens. The key code is as follows:

Linked List

The linked list can be implemented by mapping + struct, or array + struct. If it is a single-linked list, then we only need to record the previous (or next) node in the structure; if it is a double-linked list, both the previous and next nodes need to be recorded. For example, the OneSwapPair contract uses a fixed-length array + struct to construct single-linked list to keep an order book. The key code is as follows:

Since the order list may be very long, OneSwap applies a technique of “off-chain query and on-chain confirmation”. For example, when placing a limit order, you can query the position of the order on the chain beforehand, and then directly confirm the position and insert the order when the transaction is executed. Thanks to these optimization techniques and other various optimization methods, OneSwap keeps the gas consumption of various operations at the same low level as UniSwap while supporting limit orders.

Stacks and Queues

Solidity variable-length arrays provide push() and pop() functions, so they can be used directly as stacks. The queue can be implemented with a variable length array or a mapping. OneSwap does not directly use stacks and queues, so we will not introduce them in detail in this article. You can refer to this article to know more about the implementation of these two data structures.


Ethereum provides a huge KVStore for each smart contract deployed on it, and the state of the contract is stored in the KVStore. The underlying EVM provides SSTORE and SLOAD instructions to read and write this KVStore, and these two instructions are relatively Gas-consuming. In the Solidity contract, we can define various types of state variables, including value types, structure types, fixed-length or variable-length arrays, and mappings. Whatever type of state variable, it must be stored in the same KVStore. To use this storage space properly and efficiently, the Solidity compiler has been optimized a lot. However, the contract author must also carefully arrange various state variables.

This article introduces the underlying mechanisms of various state variables in Solidity language, how to construct data structures such as Set and Linked List based on arrays and mappings, and the application of these data structures in the OneSwap project. For more information about OneSwap, please follow our subsequent articles.


A fully decentralized exchange protocol on Smart Contract, with permission-free token listing and automated market making.