Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Swap

Build and submit a direct DEX v2 swap, then inspect its result.

Swap TON or jettons directly through a STON.fi pool. This guide uses DEX v2 contracts and SDK 2.x. See Setup and environments for client setup and router selection.

Mainnet-first workflow

STON.fi’s REST API (api.ston.fi) serves mainnet data. For a mainnet swap:

  1. Simulate the swap to obtain routing metadata (expected amounts, vault info, and the full router object).

  2. Feed simulationResult.router directly into dexFactory() to build contracts dynamically.

  3. Generate the swap transaction parameters with the router helpers.

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

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

const apiClient = new StonApiClient();

// 1. Simulate the swap to discover routing details
const simulationResult = await apiClient.simulateSwap({
  offerAddress: "<from asset address or 'ton'>",
  askAddress: "<to asset address>",
  offerUnits: "<amount in blockchain units>",
  slippageTolerance: "0.01",
});

// 2. Router metadata ships with the simulation result
const { router: routerInfo } = simulationResult;
const dexContracts = dexFactory(routerInfo);

// 3. Open the router contract
const router = tonClient.open(
  dexContracts.Router.create(routerInfo.address)
);

// Optional helper when TON is part of the route
const proxyTon = dexContracts.pTON.create(routerInfo.ptonMasterAddress);

The simulationResult object contains offerUnits, minAskUnits, offerAddress, askAddress, and router. Reuse those values when building the actual swap transaction to ensure the signed transaction matches the simulated path.

Integrator fees: See DEX fees for the direct DEX fee model.

Swap TON to jetton

Send the resulting parameters using your preferred wallet integration. See our transaction sending guide for wallet-specific examples.

Swap jetton to jetton

Swap jetton to TON

Convert the built parameters to your wallet's message format using the transaction sending guide.

Testnet swaps (manual setup)

To run v2 swaps on TON testnet, configure testnet contracts and provide liquidity to the pools. The api.ston.fi endpoint serves mainnet data.

To mirror the three swap types from the mainnet example, testers often run the following sequence on testnet:

  1. swap 1 TON to TesREED (ton -> jetton)

  2. swap TesREED to TestBlue (jetton -> jetton)

  3. swap TestBlue back to TON (jetton -> ton)

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

Check the result

An implementation reference is available in the swap demo and SDK example application.

Resolve the submitted wallet transaction with the transaction query, then inspect swap status/action-tree data for that operation. An API lookup with no result yet is not proof of failure. Refresh expired simulations before a new submission; do not submit again solely because the UI timed out.

Use the REST transaction action tree to inspect the submitted operation.

Last updated