swap

This section contains SDK example for performing swap in DEX

In this section, to illustrate all three possible types of a swap, we will do following exchange chain

  1. swap 1 TON to STON (ton to jetton swap)

  2. swap STON to GEMSTON (jetton to jetton swap)

  3. swap GEMSTON back to TON (jetton to ton swap)

Swap TON to jetton

import { TonClient, toNano } from "@ton/ton";
import { DEX, pTON } from "@ston-fi/sdk";

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

const router = client.open(new DEX.v1.Router());

// swap 1 TON to STON but not less than 1 nano STON
const txParams = await router.getSwapTonToJettonTxParams({
  userWalletAddress: "", // ! replace with your address
  proxyTon: new pTON.v1(),
  offerAmount: toNano("1"),
  askJettonAddress: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // STON
  minAskAmount: "1",
  queryId: 12345,
});

To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.

Swap jetton to jetton

import { TonClient, toNano } from "@ton/ton";
import { DEX, pTON } from "@ston-fi/sdk";

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

const router = client.open(new DEX.v1.Router());

// swap 1 STON to GEMSTON but not less than 1 nano GEMSTON
const txParams = await router.getSwapJettonToJettonTxParams({
  userWalletAddress: "", // ! replace with your address
  offerJettonAddress: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // STON
  offerAmount: toNano("1"),
  askJettonAddress: "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa", // GEMSTON
  minAskAmount: "1",
  queryId: 12345,
});

To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.

Swap jetton to TON

import { TonClient, toNano } from "@ton/ton";
import { DEX, pTON } from "@ston-fi/sdk";

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

const router = client.open(new DEX.v1.Router());

// swap 1 GEMSTON to TON but not less than 1 nano TON
const txParams = await router.getSwapJettonToTonTxParams({
  userWalletAddress: "", // ! replace with your address
  offerJettonAddress: "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa", // GEMSTON
  offerAmount: toNano("1"),
  proxyTon: new pTON.v1(),
  minAskAmount: "1",
  queryId: 12345,
});

To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.

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

Type
Tx TON
Forward TON

pTON -> Jetton

swap_amount + 0.185

0.185

Jetton -> Jetton

0.22

0.175

Jetton -> pTON

0.17

0.125

Formulas:

pTON -> Jetton

// pton_transfer + (pay_to + jetton_transfer) * 2 + (jetton_notification + swap)
fwd_amount = 0.01 + (0.02 + 0.045) * 2 + 0.045; // = 0.185
// jetton_transfer + fwd_amount
tx_amount = swap_amount + fwd_amount; // = swap_amount + 0.185

Jetton -> Jetton

// (jetton_notification + swap) + (pay_to + jetton_transfer) * 2
fwd_amount = 0.045 + (0.02 + 0.045) * 2; // = 0.175
// jetton_transfer + fwd_amount
tx_amount = 0.045 + fwd_amount; // = 0.22

Jetton -> pTON

// (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.045 + fwd_amount; // = 0.17

Last updated