# Pair Addresses

## getPair <a href="#getpair" id="getpair"></a>

The most obvious way to get the address for a pair is to call getPair on the factory. If the pair exists, this function will return its address, else `address(0)` (`0x0000000000000000000000000000000000000000`).

* The “canonical” way to determine whether or not a pair exists.
* Requires an on-chain lookup.

## CREATE2 <a href="#create2" id="create2"></a>

Thanks to some [fancy footwork in the factory](/reference/smart-contracts.md), we can also compute pair addresses *without any on-chain lookups* because of [CREATE2](https://eips.ethereum.org/EIPS/eip-1014). The following values are required for this technique:

|                        |                                                                      |
| ---------------------- | -------------------------------------------------------------------- |
| `address`              | The [factory address](/reference/smart-contracts.md)                 |
| `salt`                 | `keccak256(abi.encodePacked(token0, token1))`                        |
| `keccak256(init_code)` | `0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f` |

* `token0` must be strictly less than `token1` by sort order.
* Can be computed offline.
* Requires the ability to perform `keccak256`.

### Examples <a href="#examples" id="examples"></a>

#### TypeScript <a href="#typescript" id="typescript"></a>

This example makes use of the [WhiteSwap SDK](/reference/sdk.md). In reality, the SDK computes pair addresses behind the scenes, obviating the need to compute them manually like this.

```
import { FACTORY_ADDRESS, INIT_CODE_HASH } from '@whiteswap/sdk'
import { pack, keccak256 } from '@ethersproject/solidity'
import { getCreate2Address } from '@ethersproject/address'

const token0 = '0xCAFE000000000000000000000000000000000000' // change me!
const token1 = '0xF00D000000000000000000000000000000000000' // change me!

const pair = getCreate2Address(
  FACTORY_ADDRESS,
  keccak256(['bytes'], [pack(['address', 'address'], [token0, token1])]),
  INIT_CODE_HASH'
)
```


---

# 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.ws.exchange/developer-guides/javascript-sdk/pair-addresses.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.
