Perform Swap
This guide demonstrates how to perform a token swap using the Torch Finance SDK.
Example: Swapping TON for tsTON
1. Import modules & Initialize SDK
import { TorchSDK, generateQueryId, SwapParams, toUnit } from '@torch-finance/sdk';
import { Asset } from '@torch-finance/core';
const sdk = new TorchSDK();2. Define Swap Parameters
Now, configure the parameters for swapping 0.01 TON for tsTON.
Your transaction will be refunded if your slippage tolerance is too low. Unless you know what you are doing, it is recommended to set the slippageTolerance to 0.01 (1%) or leave it empty to avoid transaction failure.
// recommend to generate queryId for every actions
const queryId = await generateQueryId();
// Define assets
const tonAsset = Asset.ton();
const tsTONAsset = Asset.jetton("EQC98_qAmNEptUtPc7W6xdHh_ZHrBUFpw5Ft_IzNU20QAJav");
// TON has 9 decimals
const assetInDecimals = 9;
// Define the swap
const swapParams: SwapParams = {
mode: 'ExactIn',
queryId: queryId,
assetIn: tonAsset,
assetOut: tsTONAsset,
amountIn: toUnit('0.01', assetInDecimals), // 0.01 TON
slippageTolerance: 0.01, // 1%
};3. Preview the Swap
Simulate the swap to preview important details like the execution price and the estimated output amount.
Output Example:
4. Execute the Swap
Finally, execute the transaction and retrieve the msgHash, which can be used to track its status.
You may need to create wallet instance with mnemonic to send transaction, you can find full implantation here: https://github.com/torch-core/torch-sdk-examples/blob/main/backend-examples/swap.ts
4.1 Execute Swap with Simulation Result
Invoke the callback function returned by simulate swap method
4.2 Execute Swap Directly with Swap Params
Invoke sdk.getSwapPayload directly
Last updated