# Uniswap v3 Architecture (/docs/protocols/v3/concepts/architecture)

Understand Uniswap v3 architecture, including core contracts, factory design, and pool interaction patterns.

Uniswap v3 is a binary smart contract system comprised of many libraries, which together make the Core and Periphery.

Core contracts provide fundamental safety guarantees for all parties interacting with Uniswap. They define the logic of pool generation, the pools themselves, and the interactions involving the respective assets therein.

Periphery contracts interact with one or more Core contracts but are not part of the core. They are designed to provide methods of interacting with the core that increase clarity and user safety.

External calls will primarily call the periphery interfaces. Externally available functions are all viewable in the reference documentation. Internal functions are viewable on the Uniswap v3 GitHub repo.

## Core
The core consists of a single factory, a pool deployer, and the many pools the factory will create.

A significant amount of care and attention has been given to gas optimization in the core contracts. The result is a substantial reduction in gas costs for all protocol interactions compared to v2, at the cost of a reduction in code clarity.

> [!NOTE]
> **Source Code**
>
> [uniswap-v3-core](https://github.com/Uniswap/uniswap-v3-core)

## Factory
The factory defines the logic for generating pools. A pool is defined by two tokens, which make up the asset pair, and a fee. There can be multiple pools of the same asset pair, distinguished only by their swap fee.

> [!NOTE]
> Source code: [UniswapV3Factory.sol](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/UniswapV3Factory.sol)

## Pools
Pools primarily serve as automated market makers for the paired assets. Additionally, they expose price oracle data and may be used as an asset source for flash transactions.

> [!NOTE]
> Source code: [UniswapV3Pool.sol](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/UniswapV3Pool.sol)

## Core libraries
The pool contract delegates most of its arithmetic to a set of libraries. They are the contracts a v3 integration reads most often when reasoning about ticks, prices and liquidity, and each one is small enough to read end to end.

| Library                                                                                                     | What it does                                                             |
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| [TickMath](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/TickMath.sol)           | Computes sqrt prices from ticks and vice versa, for ticks of size 1.0001 |
| [SqrtPriceMath](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/SqrtPriceMath.sol) | Price and amount functions based on Q64.96 sqrt price and liquidity      |
| [SwapMath](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/SwapMath.sol)           | Computes the result of a swap within a single tick                       |
| [FullMath](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/FullMath.sol)           | 512-bit multiply and divide without loss of precision                    |
| [LiquidityMath](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/LiquidityMath.sol) | Adds a signed liquidity delta to liquidity                               |
| [TickBitmap](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/TickBitmap.sol)       | Packed record of which ticks are initialized                             |
| [Tick](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/Tick.sol)                   | Per-tick state, including liquidity and fee growth                       |
| [Position](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/Position.sol)           | Per-position state, keyed by owner and tick range                        |

Alongside these sit the numeric helpers the pool uses internally: `BitMath`, `FixedPoint96`, `FixedPoint128`, `LowGasSafeMath`, `SafeCast`, `UnsafeMath` and `TransferHelper`.

Every library carries natspec on each function, so the source is the reference.

> [!NOTE]
> Source code: [libraries/](https://github.com/Uniswap/uniswap-v3-core/tree/main/contracts/libraries)

## Periphery
The periphery is a constellation of smart contracts designed to support domain-specific interactions with the core. As the Uniswap protocol is a permissionless system, the contracts described below have no special privileges and are only a small subset of possible periphery-like contracts.

## SwapRouter
The swap router supports all the basic requirements of a front-end offering trading. It natively supports single trades (x to y) and multihop trades (e.g. x to y to z).

> [!NOTE]
> Source code: [SwapRouter.sol](https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/SwapRouter.sol)

## Nonfungible Position Manager
The position manager handles the logic transactions involving the creation, adjustment, or exiting of positions.

> [!NOTE]
> Source code: [NonfungiblePositionManager.sol](https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/NonfungiblePositionManager.sol)

## Oracle
The oracle provides price and liquidity data useful for a wide variety of system designs, and is available in every deployed pool.

> [!NOTE]
> Source code: [Oracle.sol](https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/Oracle.sol)

## Periphery libraries
The libraries provide a variety of helper functions developers may need, like calculating pool addresses, safe transfer functions, and more.

> [!NOTE]
> Source code: [libraries/](https://github.com/Uniswap/uniswap-v3-periphery/tree/main/contracts/libraries)
