# contracts

> This module provides contract addresses for the GMX protocol across different blockchain networks. It includes addresses for V1, V2 (Synthetics), simulation, GLV, Multichain, Gelato relay, and external contracts, as well as typed lookup utilities.

# contracts

This module provides contract addresses for the GMX protocol across different blockchain networks. It includes addresses for V1, V2 (Synthetics), simulation, GLV, Multichain, Gelato relay, and external contracts, as well as typed lookup utilities.

## Constants

### `CONTRACTS`

```typescript
const CONTRACTS: Record<ContractsChainId, Record<string, Address>>;
```

A complete mapping of chain IDs to contract addresses for all GMX protocol contracts.

`CONTRACTS` covers five chain IDs:

| Chain            | Chain ID | Type                  |
| ---------------- | -------- | --------------------- |
| Arbitrum         | `42161`  | Production            |
| Avalanche        | `43114`  | Production            |
| MegaETH          | `4326`   | Production            |
| Avalanche Fuji   | `43113`  | Development / testnet |
| Arbitrum Sepolia | `421614` | Development / testnet |

Contract categories vary by chain. Production chains Arbitrum and Avalanche include both V1 contracts (Vault, GLP, staking trackers) and V2 Synthetics contracts (DataStore, ExchangeRouter, SimulationRouter, SyntheticsReader, GLV router, and Multichain routers). MegaETH and the testnet chains include V2 Synthetics contracts only, with V1 slots set to `zeroAddress`.

```typescript

// Get the DataStore address on Arbitrum
const dataStoreAddress = CONTRACTS[42161].DataStore;
// Returns: "0xFD70de6b91282D8017aA4E741e9Ae325CAb992d8"

// Get the ExchangeRouter address on Avalanche
const exchangeRouterAddress = CONTRACTS[43114].ExchangeRouter;
// Returns: "0x5b7693839ef8d767168891797B9d2f319e351672"

// Iterate all production chains

for (const chainId of CONTRACTS_CHAIN_IDS) {
  const router = CONTRACTS[chainId].ExchangeRouter;
  console.log(`ExchangeRouter on ${chainId}: ${router}`);
}
```

:::tip
Prefer `getContract` over direct `CONTRACTS` indexing in application code. It provides a typed lookup and throws a descriptive error if the chain ID or contract name is not found, making integration bugs easier to diagnose.
:::

## Types

### `ContractName`

```typescript
type ContractName = ExtractContractNames<typeof CONTRACTS>;
// resolves to: "DataStore" | "ExchangeRouter" | "SyntheticsReader" | "Vault" | ...
```

A union of every contract key name defined across all chains in `CONTRACTS`. The type is derived automatically from the `CONTRACTS` object, so it stays in sync with the source without manual maintenance.

```typescript

// Valid: "DataStore" is a key in CONTRACTS
const name: ContractName = "DataStore";

// Also valid
const names: ContractName[] = ["ExchangeRouter", "SimulationRouter", "SyntheticsReader", "GlvRouter"];
```

`ContractName` includes all contract keys from all chains, including V1-only contracts (such as `"Vault"`, `"GlpManager"`) and V2-only contracts (such as `"DataStore"`, `"GlvRouter"`). Some contracts are set to `zeroAddress` on chains where they are not deployed — use `getContract` to retrieve the address and check the result before use if the contract may not be active on a given chain.

## Methods

### `getContract`

```typescript
function getContract(chainId: ContractsChainId, name: ContractName): Address;
```

Retrieves a specific contract address for a given chain ID and contract name.

#### Parameters

| Parameter | Type               | Description                                                            |
| --------- | ------------------ | ---------------------------------------------------------------------- |
| `chainId` | `ContractsChainId` | The numeric chain ID (42161, 43114, 4326, 43113, or 421614)            |
| `name`    | `ContractName`     | The contract name key (for example, `"DataStore"`, `"ExchangeRouter"`) |

#### Returns

`Address` — the checksummed hex address string for the contract on the given chain.

:::warning
`getContract` throws an `Error` if `chainId` is not in `CONTRACTS` or if `name` is not found for that chain. Always call with a valid `ContractsChainId` value.
:::

```typescript

// Get DataStore on Arbitrum
const dataStoreAddress = getContract(ARBITRUM, "DataStore");
// Returns: "0xFD70de6b91282D8017aA4E741e9Ae325CAb992d8"

// Get ExchangeRouter on Avalanche
const exchangeRouterAddress = getContract(AVALANCHE, "ExchangeRouter");
// Returns: "0x5b7693839ef8d767168891797B9d2f319e351672"

// Use with a dynamic chain ID (validated at runtime)
function lookupContract(chainId: number, name: ContractName): Address | null {
  try {
    return getContract(chainId as ContractsChainId, name);
  } catch {
    return null;
  }
}
```

### `tryGetContract`

```typescript
function tryGetContract(chainId: ContractsChainId, name: ContractName): Address | undefined;
```

Returns the same address as `getContract`, or `undefined` when the chain or contract name is not configured. Use it for optional capabilities such as `SimulationRouter` without implementing your own `try`/`catch` wrapper.

```typescript

const simulationRouter = tryGetContract(ARBITRUM, "SimulationRouter");

if (simulationRouter) {
  console.log(`SimulationRouter: ${simulationRouter}`);
}
```

## Related

- [`chains`](./chains.md) — chain IDs, `ContractsChainId`, `ContractsChainIdProduction`
- [SDK v1 Getting Started](../../readme.mdx)
