At the core of this approach is a three-level binary tree designed to store 24-bit IDs. Each level of the tree caters to 8 bits of the ID, with the entire structure allowing for efficient categorization and retrieval of data. Our smart contracts use this tree to store whether orders exist against a price point (ID). It is important to note that we use the tree to only identify whether an order exists at a price point and not to store the actual orders. The actual orders are stored in a doubly linked list(insertions and deletions can be done in constant gas).

To understand how we achieve placing orders in constant gas it is important to understand how we can add an ID to the tree in a constant number of operations. As mentioned earlier, each level of the tree corresponds to an 8-bit segment of the ID. In other words, level0 has no key, the key for level1 is the first 8 bits and the key for level2 is the first 16 bits.

  1. First we right shift the identifier by 8 bits, leaving the 16 most significant bits(MSB). This is the key for level2. Suppose the 8 LSBs are 10000001, which is 129 in the decimal system, then we flip the 130th LSB in the value of the mapping to one(if it is not already flipped), signifying that $ABC10000001$ exists. ABC is the 16 MSB which is the key.
  2. Then we right-shift the identifier by 16 bits, leaving the 8 MSBs. This is the key for level1. Similar to the previous step we flip a bit in the value based on the middle 8 bits of the uin24 identifier.
  3. Similarly we also flip a bit in level0 based on the 8 MSBs.

This is how we can add a price point to the orderbook in at most 3 SSTORE operations. After adding the price point, we store the actual order in a doubly linked list by performing additional SSTOREs.

Navigating the Tree

The tree structure supports efficient traversal to find the largest and smallest ID:

This allows us to find matches for taker orders in constant gas.