This section contains SDK example for withdrawing liquidity from DEX
Burn all liquidity tokens to free liquidity from a pool
import TonWeb from "tonweb";
import { DEX } from "@ston-fi/sdk";
const USER_WALLET_ADDRESS = ""; // ! replace with your address
const JETTON_0_ADDRESS = "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO"; // STON
const JETTON_1_ADDRESS = "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa"; // GEMSTON
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
const pool = await router.getPool({
token0: JETTON_0_ADDRESS,
token1: JETTON_1_ADDRESS,
});
if (!pool) {
throw Error(`Pool for ${JETTON_0_ADDRESS}/${JETTON_1_ADDRESS} not found`);
}
const lpTokenWallet = await pool.getJettonWallet({
ownerAddress: USER_WALLET_ADDRESS,
});
const lpTokenWalletData = await lpTokenWallet.getData();
const txParams = await pool.buildBurnTxParams({
amount: lpTokenWalletData.balance,
responseAddress: USER_WALLET_ADDRESS,
queryId: 12345,
});
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
});
This section contains SDK reference for DEX
The remaining sections of the documentation will demonstrate specific examples of the DEX usage:
This section contains SDK example for providing liquidity in DEX
Provide liquidity for a pool
import TonWeb from "tonweb";
import { DEX } from "@ston-fi/sdk";
const USER_WALLET_ADDRESS = ""; // ! replace with your address
const JETTON_0_ADDRESS = "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO"; // STON
const JETTON_1_ADDRESS = "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa"; // GEMSTON
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
const txsParams = await Promise.all([
// deposit 5 STON to the STON/GEMSTON pool and get at least 1 nano LP token
router.buildProvideLiquidityJettonTxParams({
userWalletAddress: USER_WALLET_ADDRESS,
sendTokenAddress: JETTON_0_ADDRESS,
sendAmount: new TonWeb.utils.BN("500000000"),
otherTokenAddress: JETTON_1_ADDRESS,
minLpOut: new TonWeb.utils.BN("1"),
queryId: 12345,
}),
// deposit 2 GEMSTON to the STON/GEMSTON pool and get at least 1 nano LP token
router.buildProvideLiquidityJettonTxParams({
userWalletAddress: USER_WALLET_ADDRESS,
sendTokenAddress: JETTON_1_ADDRESS,
sendAmount: new TonWeb.utils.BN("200000000"),
otherTokenAddress: JETTON_0_ADDRESS,
minLpOut: new TonWeb.utils.BN("1"),
queryId: 123456,
}),
]);
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
txsParams.map((txParams) => console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
}));
import TonWeb from "tonweb";
import { DEX, pTON } from "@ston-fi/sdk";
const USER_WALLET_ADDRESS = ""; // ! replace with your address
const JETTON_0_ADDRESS = "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO"; // STON
const pTON_ADDRESS = pTON.v1.address;
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
const txsParams = await Promise.all([
// deposit 1 TON to the STON/TON pool and get at least 1 nano LP token
router.buildProvideLiquidityTonTxParams({
userWalletAddress: USER_WALLET_ADDRESS,
proxyTonAddress: pTON_ADDRESS,
sendAmount: new TonWeb.utils.BN("100000000"),
otherTokenAddress: JETTON_0_ADDRESS,
minLpOut: new TonWeb.utils.BN("1"),
queryId: 12345,
}),
// deposit 5 STON to the STON/TON pool and get at least 1 nano LP token
router.buildProvideLiquidityJettonTxParams({
userWalletAddress: USER_WALLET_ADDRESS,
sendTokenAddress: JETTON_0_ADDRESS,
sendAmount: new TonWeb.utils.BN("500000000"),
otherTokenAddress: pTON_ADDRESS,
minLpOut: new TonWeb.utils.BN("1"),
queryId: 123456,
}),
]);
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
txsParams.map((txParams) => console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
}));
This section contains SDK example for refunding undeposited liquidity in DEX
Refund tokens that were sent to LP account but weren't added to a liquidity pool yet
import TonWeb from "tonweb";
import { DEX } from "@ston-fi/sdk";
const USER_WALLET_ADDRESS = ""; // ! replace with your address
const JETTON_0_ADDRESS = "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO"; // STON
const JETTON_1_ADDRESS = "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa"; // GEMSTON
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
const pool = await router.getPool({
token0: JETTON_0_ADDRESS,
token1: JETTON_1_ADDRESS,
});
if (!pool) {
throw Error(`Pool for ${JETTON_0_ADDRESS}/${JETTON_1_ADDRESS} not found`);
}
const lpAccount = await pool.getLpAccount({ ownerAddress: USER_WALLET_ADDRESS });
if (!lpAccount) {
throw Error(
`LpAccount for ${USER_WALLET_ADDRESS} at ${JETTON_0_ADDRESS}/${JETTON_1_ADDRESS} pool not found`
);
}
const txParams = await lpAccount.buildRefundTxParams({
queryId: 12345,
});
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
});
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
swap 1 TON to STON (ton to jetton swap)
swap STON to GEMSTON (jetton to jetton swap)
swap GEMSTON back to TON (jetton to ton swap)
import TonWeb from "tonweb";
import { DEX, pTON } from "@ston-fi/sdk";
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
// swap 1 TON to STON but not less than 1 nano STON
const txParams = await router.buildSwapTonToJettonTxParams({
userWalletAddress: "", // ! replace with your address
proxyTonAddress: pTON.v1.address,
offerAmount: new TonWeb.utils.BN("1000000000"),
askJettonAddress: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // STON
minAskAmount: new TonWeb.utils.BN("1"),
queryId: 12345,
});
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
});
import TonWeb from "tonweb";
import { DEX, pTON } from "@ston-fi/sdk";
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
// swap 1 STON to GEMSTON but not less than 1 nano GEMSTON
const txParams = await router.buildSwapJettonToJettonTxParams({
userWalletAddress: "", // ! replace with your address
offerJettonAddress: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // STON
offerAmount: new TonWeb.utils.BN("1000000000"),
askJettonAddress: "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa", // GEMSTON
minAskAmount: new TonWeb.utils.BN("1"),
queryId: 12345,
});
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
});
import TonWeb from "tonweb";
import { DEX, pTON } from "@ston-fi/sdk";
const router = new DEX.v1.Router({
tonApiClient: new TonWeb.HttpProvider(),
});
// swap 1 GEMSTON to TON but not less than 1 nano TON
const txParams = await router.buildSwapJettonToTonTxParams({
userWalletAddress: "", // ! replace with your address
offerJettonAddress: "EQBX6K9aXVl3nXINCyPPL86C4ONVmQ8vK360u6dykFKXpHCa", // GEMSTON
offerAmount: new TonWeb.utils.BN("1000000000"),
proxyTonAddress: pTON.v1.address,
minAskAmount: new TonWeb.utils.BN("1"),
queryId: 12345,
});
// To execute the transaction, you need to send a transaction to the blockchain.
// This code will be different based on the wallet you are using to send the tx from
// logging is used for demonstration purposes
console.log({
to: txParams.to,
amount: txParams.gasAmount,
payload: txParams.payload,
});
Below are recommended values for TON sent and forward gas for each type of the swap:
pTON
-> Jetton
swap_amount + 0.215
0.215
Jetton
-> Jetton
0.265
0.205
Jetton
-> pTON
0.185
0.125
pTON
-> Jetton
// 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
Jetton
-> Jetton
// (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
// (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