# Burn LP Tokens (v2)

Burn all liquidity tokens to release the underlying assets from a pool.

> The production-ready approach is **API-driven**. Query pool information through `@ston-fi/api`, let the API return the router metadata, and then build the on-chain contracts with `dexFactory`. This keeps your integration compatible with future router upgrades and avoids hardcoding contract addresses.

## Mainnet workflow

1. Locate the target pool via the STON.fi API (for example with `getPoolsByAssetPair` or `getPool`).
2. Fetch the router metadata with `getRouter(pool.routerAddress)`.
3. Build the pool and LP wallet contracts dynamically with `dexFactory` and `TonClient`.
4. Read the LP balance and generate the burn transaction parameters.

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

const tonClient = new Client({
  endpoint: "https://toncenter.com/api/v2/jsonRPC",
  apiKey: process.env.TON_API_KEY, // optional but recommended for higher rate limits
});

const apiClient = new StonApiClient();

// Discover the pool you want to exit (replace addresses with your assets)
const [poolInfo] = await apiClient.getPoolsByAssetPair({
  asset0Address: "<token A address or 'ton'>",
  asset1Address: "<token B address>",
});

if (!poolInfo) {
  throw new Error("Liquidity pool not found for the provided asset pair");
}

// Load router metadata and initialise contracts
const routerMetadata = await apiClient.getRouter(poolInfo.routerAddress);
const dexContracts = dexFactory(routerMetadata);

const pool = tonClient.open(
  dexContracts.Pool.create(poolInfo.address),
);

// Fetch the LP wallet owned by the user and read the balance
const lpWallet = tonClient.open(
  await pool.getJettonWallet({ ownerAddress: "<your wallet address>" }),
);

const { balance } = await lpWallet.getWalletData();

const burnTxParams = await pool.getBurnTxParams({
  amount: balance,
  userWalletAddress: "<your wallet address>",
  queryId: 12345,
});
```

Send the resulting transaction parameters using your preferred wallet integration (see the [transaction sending guide](https://docs.ston.fi/developer-section/common/transaction-sending)).

> **Tip**: If one side of the pool is TON, the API already exposes the correct `routerMetadata.ptonMasterAddress`. You do not need to hardcode the proxy TON contract—`dexFactory` picks the right implementation for you.

## Testnet burn (manual setup)

Only fall back to testnet when absolutely necessary. `api.ston.fi` serves mainnet exclusively, so you must hardcode contract addresses, obtain/mint the testnet jettons (e.g., TesREED/TestBlue), and supply liquidity before you can burn LP tokens.

```typescript
import { TonClient } from "@ton/ton";
import { DEX } from "@ston-fi/sdk";

const client = new TonClient({
  endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
});

const router = client.open(
  DEX.v2_1.Router.CPI.create("kQALh-JBBIKK7gr0o4AVf9JZnEsFndqO0qTCyT-D-yBsWk0v"), // CPI Router v2.1.0 (testnet)
);

const pool = client.open(
  await router.getPool({
    token0: "kQDLvsZol3juZyOAVG8tWsJntOxeEZWEaWCbbSjYakQpuYN5", // TesREED jetton (testnet)
    token1: "kQB_TOJSB7q3-Jm1O8s0jKFtqLElZDPjATs5uJGsujcjznq3", // TestBlue jetton (testnet)
  }),
);

const lpWallet = client.open(
  await pool.getJettonWallet({ ownerAddress: "<your testnet wallet>" }),
);

const { balance } = await lpWallet.getWalletData();

const txParams = await pool.getBurnTxParams({
  amount: balance,
  userWalletAddress: "<your testnet wallet>",
  queryId: 12345,
});
```

This manual approach is strictly **for testing**. Switch back to the API-driven workflow for any mainnet-facing integration.
