# Overview

Marginly architecture consists of the following contracts:

* marginly pool implementation
* factory
* pool
* router

Marginly pool implementation (`contracts/contract/MarginlyPool.sol`) is a pool bytecode. This deployed contract is basically an uninitialized version of marginly pool, which is cloned during creation of a new pool. The cloning approach is chosen in order for our factory  contract size to fit with ethereum evm limit of 24 KiB.

Factory (`contracts/contracts/MarginlyFactory.sol`) creates new Marginly pools via 'createPool' method. This method clones marginly pool implementation and calls 'initialize' method, which sets pool variables and parameters that can be found [here](/protocol-architecture/pools/pool-variables.md) and [here](/protocol-architecture/pools/pool-parameters.md).&#x20;

\
Factory stores the following info common for all our pools:

* router address
* fee holder address
* tech position owner address

Though 'createPool' method is 'ownerOnly' right now, we are planning to make it permissionless in the future versions.

Router (`router/contracts/MarginlyRouter.sol`) is a contract that makes swaps on different DEXs of user's choice. It has two main methods:

* `swapExactInput` -- makes swap with exact amount of swapped tokens
* `swapExactOutput` -- makes swap with exact amount of tokens, received after swap

The user's choice is defined by a `swapCalldata` argument, which is decoded to an array of DEX indexes and their swap ratio. Default value for this argument is 0. It represents swap on UniswapV3 and is used during liquidation. Such swaps can be performed by any wallet or contract not limited to Marginly pools.

DEXs Swap implementations are located in `router/contract/abstract/dex` directory.


---

# 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.marginly.com/protocol-architecture/overview.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.
