> For the complete documentation index, see [llms.txt](https://docs.ston.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ston.fi/developer-section/dex/migration/v1-to-v2.md).

# DEX v1 to v2 integration

Change the deployed contract generation without confusing it with an npm package upgrade.

This page describes moving an integration from deployed DEX v1 contracts to deployed DEX v2 contracts. It does not define a blanket npm SDK 1.x → 2.x migration. The current package includes wrappers for both generations; use the [official changelog](https://github.com/ston-fi/sdk/blob/main/CHANGELOG.md) for package release changes.

## Select the deployed contracts

Use API router metadata from the selected pool or simulation, then build the matching wrapper with `dexFactory()`. A v2 wrapper cannot turn a v1 pool/router into a v2 deployment. Existing v1 LP positions remain in their original pool until withdrawn; changing an import does not migrate liquidity.

```ts
import { Client, dexFactory } from "@ston-fi/sdk";
import { StonApiClient } from "@ston-fi/api";

const client = new Client({ endpoint: "https://toncenter.com/api/v2/jsonRPC" });
const apiClient = new StonApiClient();
const routerInfo = await apiClient.getRouter("<selected deployed v2 router>");
if (routerInfo.majorVersion !== 2) {
  throw new Error("Selected router is not a DEX v2 deployment");
}
const contracts = dexFactory(routerInfo);
const router = client.open(contracts.Router.create(routerInfo.address));
```

Pass the API router metadata to `dexFactory()` or `routerFactory()` to select the matching contract classes.

## Typed contract classes

For an explicitly known v2.1 deployment, the typed classes include `DEX.v2_1.Router.CPI` and `DEX.v2_1.Pool.CPI` for constant-product contracts, or `WStable` for weighted-stable contracts.

In production, prefer API-driven selection so the curve/version matches the chosen deployed router. Do not force CPI on metadata describing another pool type. The [SDK changelog](https://github.com/ston-fi/sdk/blob/main/CHANGELOG.md) describes deprecation of base v2 wrappers and the type-aware factory interface.

## Update the operation

* [Swap](/developer-section/dex/sdk/swap.md): use a fresh simulation and matching router/pTON metadata; verify minimum output and referral parameters.
* [Add liquidity](/developer-section/dex/sdk/provide-liquidity.md): choose the supported provision mode, preserve all required message legs, and inspect pending LP-account balances.
* [Withdraw liquidity](/developer-section/dex/sdk/burn-lp-tokens.md): burn LP tokens in the pool that issued them.
* [Recover pending liquidity](/developer-section/dex/sdk/refund-liquidity.md): refund pending deposits before LP issuance.
* [Integrator fees](/developer-section/dex/fees.md): v1 direct payouts and v2 Vault accumulation have different collection/data paths.

Use verified decimals with `toUnits()`/`fromUnits()` rather than treating every jetton as a nine-decimal token. Update wallet/RPC network context alongside contract selection, then follow the operation guide through submission and result checking.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ston.fi/developer-section/dex/migration/v1-to-v2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
