# Introduction and Diamond Explainer

SYMMIO's core contracts are organised as a **Diamond** — a single deployed proxy address that internally routes calls to a set of **facets** (logic contracts), each responsible for a slice of the protocol's functionality. The Diamond pattern is defined in [EIP-2535](https://eips.ethereum.org/EIPS/eip-2535).

There are two main Diamonds in the current (0.8.5) architecture:

* The **Symmio core diamond** — quotes, positions, funding, liquidation, settlement.
* The **AccountLayer diamond** — sub-accounts, virtual accounts, affiliates, fee distribution, hooks.

The InstantLayer is a separate (non-Diamond) contract that sits in front of both, providing EIP-712 batched execution and delegation.

### Why a Diamond?

The protocol is large — quote management, funding rates, liquidation, ADL, ClearingHouse, oracle-less binding, withdrawal flows — and would not fit inside a single contract under the 24 KB EIP-170 limit. The Diamond pattern lets us split the logic across many smaller facets while keeping a single stable address that users and integrators interact with.

It also gives us per-function upgradeability. New behaviour can be added or fixed without redeploying the whole protocol or changing the address frontends, indexers, and bots are pointing at.

### Facets and selectors

Each facet exposes a set of external functions. The Diamond maintains a mapping from **function selector** (the first four bytes of `keccak256(signature)`) to the facet address that implements it. When a call arrives:

1. The Diamond's fallback reads the selector from `msg.sig`.
2. It looks up which facet implements that selector.
3. It `delegatecall`s into that facet.

Because the call is a `delegatecall`, the facet code runs **in the Diamond's storage context** — facets are stateless executables that operate on the Diamond's storage. This is what lets the protocol be split across many facets while preserving a single shared state.

To see the full facet/selector mapping for a given chain, use the [Intent Inspector](https://intent.symmscan.com/inspector) — paste any diamond address and you'll get a live readout of every facet, every selector, and which function is at which selector.

### Storage layout

Sharing storage across facets is the most error-prone part of working with diamonds. SYMMIO uses the **Diamond Storage** pattern (also sometimes called `AppStorage`): each storage area is `namespaced` under a fixed slot derived from a string constant, e.g.

```solidity
bytes32 constant ACCOUNT_STORAGE_SLOT = keccak256("diamond.standard.storage.accountlayer.account");
```

Library functions resolve `slot → struct` and let facets read/write the same struct from anywhere. Because slots are derived from unique strings, two unrelated storage areas can't collide as long as the strings are unique.

Notable v0.8.5 storage areas on the AccountLayer:

| Storage contract      | Slot key                                                        | Contents                                                            |
| --------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------- |
| `AccountStorage`      | `keccak256("diamond.standard.storage.accountlayer.account")`    | SubAccount/VA data, nonces, `globalSigner`, AccountManager bytecode |
| `AffiliateStorage`    | `keccak256("diamond.standard.storage.accountlayer.affiliate")`  | Affiliate configs, fee details, hooks, operators, `hookContext`     |
| `AccountLayerStorage` | `keccak256("diamond.standard.storage.accountlayer")`            | RBAC (`hasRole`, `roleAdmins`), global pause flag                   |
| Reentrancy guard      | `keccak256("diamond.standard.storage.accountlayer.reentrancy")` | Reentrancy status flag                                              |

The Symmio core diamond has its own analogous set of storage areas for quote state, balances, funding rates, etc.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.symm.io/exchange-builder-documentation/frontend-builder-technical-guidance/introduction-and-diamond-explainer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
