> 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/legacy/v0.4/swap.md).

# Swap

Legacy v0.4 token swap guide - execute swaps using deprecated SDK methods

## Jetton1/Jetton2

Perform a swap operation.

```typescript
import TonWeb from 'tonweb';

import { Router, ROUTER_REVISION, ROUTER_REVISION_ADDRESS } from '@ston-fi/sdk';

/**
 * This example shows how to swap two jettons using the router contract
 */
(async () => {
  const WALLET_ADDRESS = ''; // ! replace with your address
  const JETTON0 = 'EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO'; // STON
  const JETTON1 = 'EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA'; // jUSDT

  const provider = new TonWeb.HttpProvider();

  const router = new Router(provider, {
    revision: ROUTER_REVISION.V1,
    address: ROUTER_REVISION_ADDRESS.V1,
  });

  // transaction to swap 1.0 JETTON0 to JETTON1 but not less than 1 nano JETTON1
  const swapTxParams = await router.buildSwapJettonTxParams({
    // address of the wallet that holds offerJetton you want to swap
    userWalletAddress: WALLET_ADDRESS,
    // address of the jetton you want to swap
    offerJettonAddress: JETTON0,
    // amount of the jetton you want to swap
    offerAmount: new TonWeb.utils.BN('1000000000'),
    // address of the jetton you want to receive
    askJettonAddress: JETTON1,
    // minimal amount of the jetton you want to receive as a result of the swap.
    // If the amount of the jetton you want to receive is less than minAskAmount
    // the transaction will bounce
    minAskAmount: new TonWeb.utils.BN(1),
    // query id to identify your transaction in the blockchain (optional)
    queryId: 12345,
    // address of the wallet to receive the referral fee (optional)
    referralAddress: undefined,
  });

  // to execute the transaction you need to send transaction to the blockchain
  // (replace with your wallet implementation, logging is used for demonstration purposes)
  console.log({
    to: swapTxParams.to,
    amount: swapTxParams.gasAmount,
    payload: swapTxParams.payload,
  });

  // reverse transaction is the same,
  // you just need to swap `offerJettonAddress` and `askJettonAddress` values
  // and adjust `offerAmount` and `minAskAmount` accordingly
})();

```

## Proxy-ton/Jetton

Perform a swap operation using proxy-ton.

```typescript
import TonWeb from 'tonweb';

import { Router, ROUTER_REVISION, ROUTER_REVISION_ADDRESS } from '@ston-fi/sdk';

/**
 * This example shows how to swap TON to jetton using the router contract
 */
(async () => {
  const WALLET_ADDRESS = ''; // ! replace with your address
  const JETTON0 = 'EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO'; // STON
  const PROXY_TON = 'EQCM3B12QK1e4yZSf8GtBRT0aLMNyEsBc_DhVfRRtOEffLez'; // ProxyTON

  const provider = new TonWeb.HttpProvider();

  const router = new Router(provider, {
    revision: ROUTER_REVISION.V1,
    address: ROUTER_REVISION_ADDRESS.V1,
  });

  // Because TON is not a jetton, to be able to swap TON to jetton
  // you need to use special SDK method to build transaction to swap TON to jetton
  // using proxy jetton contract.

  // transaction to swap 1.0 TON to JETTON0 but not less than 1 nano JETTON0
  const tonToJettonTxParams = await router.buildSwapProxyTonTxParams({
    // address of the wallet that holds TON you want to swap
    userWalletAddress: WALLET_ADDRESS,
    proxyTonAddress: PROXY_TON,
    // amount of the TON you want to swap
    offerAmount: new TonWeb.utils.BN('1000000000'),
    // address of the jetton you want to receive
    askJettonAddress: JETTON0,
    // minimal amount of the jetton you want to receive as a result of the swap.
    // If the amount of the jetton you want to receive is less than minAskAmount
    // the transaction will bounce
    minAskAmount: new TonWeb.utils.BN(1),
    // query id to identify your transaction in the blockchain (optional)
    queryId: 12345,
    // address of the wallet to receive the referral fee (optional)
    referralAddress: undefined,
  });

  // to execute the transaction you need to send transaction to the blockchain
  // (replace with your wallet implementation, logging is used for demonstration purposes)
  console.log({
    to: tonToJettonTxParams.to,
    amount: tonToJettonTxParams.gasAmount,
    payload: tonToJettonTxParams.payload,
  });

  // reverse transaction is just a regular swap transaction where `askJettonAddress` is a ProxyTon address.
  // see swap example for more details
})();
```

## Recommended gas values

Below are recommended values for TON sent and forward gas for each type of the swap:

| Type                 | Tx TON                | Forward TON |
| -------------------- | --------------------- | ----------- |
| `Jetton` -> `Jetton` | `0.265`               | `0.205`     |
| `Jetton` -> `pTON`   | `0.185`               | `0.125`     |
| `pTON` -> `Jetton`   | `swap_amount + 0.215` | `0.215`     |

### Formulas:

#### `Jetton` -> `Jetton`

```typescript
// (jetton_notification + swap) + (pay_to + jetton_transfer) * 2
fwd_amount = 0.045 + (0.02 + 0.06) * 2 // = 0.205
// jetton_transfer + fwd_amount
tx_amount = 0.06 + fwd_amount // = 0.265
```

#### `Jetton` -> `pTON`

```typescript
// (jetton_notification + swap) + (pay_to + pton_transfer) * 2
fwd_amount = 0.045 + (0.02 + 0.02) * 2 // = 0.125
// jetton_transfer + fwd_amount
tx_amount = 0.06 + fwd_amount // = 0.185
```

#### `pTON` -> `Jetton`

```typescript
// pton_transfer + (pay_to + jetton_transfer) * 2 + (jetton_notification + swap)
fwd_amount = 0.01 + (0.02 + 0.06) * 2 + 0.045 // = 0.215
// jetton_transfer + fwd_amount
tx_amount = swap_amount + fwd_amount // = swap_amount + 0.215
```


---

# 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/legacy/v0.4/swap.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.
