> For the complete documentation index, see [llms.txt](https://docs.fathom.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fathom.fi/dex/architecture.md).

# Architecture

Fathom DEX relies on a robust and secure smart contract architecture to facilitate its decentralized exchange functionalities.

These smart contracts are the [Uniswap V2 direct fork](https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/smart-contracts). They are battle-tested on various networks and are considered safe.

### **Overview of Smart Contract Architecture**

#### **Factory Contract**

* **Role:** The factory contract is responsible for deploying new liquidity pool contracts. It maintains a registry of all active liquidity pools on the platform.
* **Functions:**
  * CreatePair: Deploys a new pair contract for a given token pair.
  * GetPair: Retrieves the address of an existing pair contract.

#### **Pair Contract**

* **Role:** Each pair contract represents a liquidity pool for a specific token pair. It manages the addition and removal of liquidity, as well as the execution of token swaps.
* **Functions:**
  * AddLiquidity: Allows users to add tokens to the liquidity pool.
  * RemoveLiquidity: Allows users to withdraw their tokens from the liquidity pool.
  * Swap: Facilitates token swaps between the two tokens in the pair.

#### **Router Contract**

* **Role:** The router contract provides the main interface for users to interact with the DEX. It routes transactions to the appropriate pair contracts.
* **Functions:**
  * AddLiquidity: Simplifies the process of adding liquidity by automatically calculating the optimal token amounts.
  * RemoveLiquidity: Manages the removal of liquidity, ensuring users receive the correct token amounts.
  * SwapExactTokensForTokens: Executes a token swap with a specified input amount.
  * SwapTokensForExactTokens: Executes a token swap with a specified output amount.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.fathom.fi/dex/architecture.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
