# TickMath

[Git Source](https://github.com/Ion-Protocol/ion-protocol/blob/88cc595825f1dc2eb738fb93e172a3e8ab7a5c43/src/libraries/uniswap/TickMath.sol)

Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports prices between 2\*\*-128 and 2\*\*128

### [State Variables](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#state-variables" id="state-variables"></a>

#### [MIN\_TICK](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#min_tick" id="min_tick"></a>

*The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2*\*-128\*

```
int24 internal constant MIN_TICK = -887_272;
```

#### [MAX\_TICK](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#max_tick" id="max_tick"></a>

*The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2\*\*128*

```
int24 internal constant MAX_TICK = -MIN_TICK;
```

#### [MIN\_SQRT\_RATIO](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#min_sqrt_ratio" id="min_sqrt_ratio"></a>

*The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN\_TICK)*

```
uint160 public constant MIN_SQRT_RATIO = 4_295_128_739;
```

#### [MAX\_SQRT\_RATIO](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#max_sqrt_ratio" id="max_sqrt_ratio"></a>

*The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX\_TICK)*

```
uint160 internal constant MAX_SQRT_RATIO = 1_461_446_703_485_210_103_287_273_052_203_988_822_378_723_970_342;
```

### [Functions](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#functions" id="functions"></a>

#### [getSqrtRatioAtTick](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#getsqrtratioattick" id="getsqrtratioattick"></a>

Calculates sqrt(1.0001^tick) \* 2^96

*Throws if |tick| > max tick*

```
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96);
```

**Parameters**

| Name   | Type    | Description                          |
| ------ | ------- | ------------------------------------ |
| `tick` | `int24` | The input tick for the above formula |

**Returns**

| Name           | Type      | Description                                                                                                        |
| -------------- | --------- | ------------------------------------------------------------------------------------------------------------------ |
| `sqrtPriceX96` | `uint160` | A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0) at the given tick |

#### [getTickAtSqrtRatio](https://docs.ionprotocol.io/devs/smart-contract-architecture/libraries/uniswap/broken-reference) <a href="#gettickatsqrtratio" id="gettickatsqrtratio"></a>

Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio

*Throws in case sqrtPriceX96 < MIN\_SQRT\_RATIO, as MIN\_SQRT\_RATIO is the lowest value getRatioAtTick may ever return.*

```
function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick);
```

**Parameters**

| Name           | Type      | Description                                              |
| -------------- | --------- | -------------------------------------------------------- |
| `sqrtPriceX96` | `uint160` | The sqrt ratio for which to compute the tick as a Q64.96 |

**Returns**

| Name   | Type    | Description                                                                    |
| ------ | ------- | ------------------------------------------------------------------------------ |
| `tick` | `int24` | The greatest tick for which the ratio is less than or equal to the input ratio |
