> ## Documentation Index
> Fetch the complete documentation index at: https://docs.krokoswap.io/llms.txt
> Use this file to discover all available pages before exploring further.

# V2 Factory & Pair

> V2 pool creation and pair contract interfaces

# V2 Factory & Pair

## V2 Factory

The V2 Factory creates and indexes V2 trading pairs. Each unique token pair has exactly one V2 pool.

### Key Functions

#### getPair

Returns the pair contract address for two tokens, or zero address if no pair exists.

```solidity theme={null}
function getPair(
    address tokenA,
    address tokenB
) external view returns (address pair);
```

<Note>
  Token order does not matter — `getPair(A, B)` returns the same result as `getPair(B, A)`.
</Note>

#### allPairs

Returns the pair address at a given index. Used for enumeration.

```solidity theme={null}
function allPairs(uint256 index) external view returns (address pair);
function allPairsLength() external view returns (uint256);
```

### ABI

```json theme={null}
[
  "function getPair(address tokenA, address tokenB) view returns (address pair)",
  "function allPairs(uint256 index) view returns (address pair)",
  "function allPairsLength() view returns (uint256)"
]
```

***

## V2 Pair

Each V2 Pair is an ERC-20 contract (LP token) that also holds the pool reserves and executes swaps.

### Key Functions

#### getReserves

Returns the current token reserves and the last block timestamp.

```solidity theme={null}
function getReserves() external view returns (
    uint112 reserve0,
    uint112 reserve1,
    uint32 blockTimestampLast
);
```

`token0` is always the token with the lower address.

#### token0 / token1

Returns the addresses of the pool's tokens.

```solidity theme={null}
function token0() external view returns (address);
function token1() external view returns (address);
```

#### totalSupply / balanceOf

Standard ERC-20 functions for LP token accounting.

```solidity theme={null}
function totalSupply() external view returns (uint256);
function balanceOf(address owner) external view returns (uint256);
```

### ABI

```json theme={null}
[
  "function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)",
  "function token0() view returns (address)",
  "function token1() view returns (address)",
  "function totalSupply() view returns (uint256)",
  "function balanceOf(address owner) view returns (uint256)",
  "function approve(address spender, uint256 value) returns (bool)",
  "function transfer(address to, uint256 value) returns (bool)"
]
```

### Example: Read Pool State

```typescript theme={null}
const FACTORY_ABI = [
  "function getPair(address, address) view returns (address)"
];
const PAIR_ABI = [
  "function getReserves() view returns (uint112, uint112, uint32)",
  "function token0() view returns (address)",
  "function token1() view returns (address)",
  "function totalSupply() view returns (uint256)"
];

const factory = new ethers.Contract(V2_FACTORY, FACTORY_ABI, provider);
const pairAddress = await factory.getPair(tokenA, tokenB);

if (pairAddress === ethers.ZeroAddress) {
  console.log("Pool does not exist");
} else {
  const pair = new ethers.Contract(pairAddress, PAIR_ABI, provider);
  const [reserve0, reserve1] = await pair.getReserves();
  const token0 = await pair.token0();

  console.log(`Reserve ${token0 === tokenA ? 'A' : 'B'}: ${reserve0}`);
  console.log(`Reserve ${token0 === tokenA ? 'B' : 'A'}: ${reserve1}`);
}
```
