> For the complete documentation index, see [llms.txt](https://doc.torch.finance/dex/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.torch.finance/dex/developer-guide/dex-sdk-guide/perform-swap.md).

# Perform Swap

This guide demonstrates how to perform a token swap using the Torch Finance SDK.&#x20;

## Example: Swapping TON for tsTON

### 1. Import modules & Initialize SDK

```typescript
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**. &#x20;

{% hint style="warning" %}
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.
{% endhint %}

```typescript
// 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.

```typescript
const { result, getSwapPayload } = await sdk.simulateSwap(swapParams);
console.log(`Execution Price: 1 tsTON = ${result.executionPrice} TON`);
console.log(`Estimated Amount Out: ${result.amountOut}`);
console.log(`Min Amount Out: ${result.minAmountOut}`);
```

**Output Example:**

```bash
Execution Price: 1 tsTON = 1.04 TON (This is human readable execution price)
Estimated Amount Out: 9900000 (You should handle decimal part by yourself)
Min Amount Out: 9801000
```

### 4. Execute the Swap

Finally, execute the transaction and retrieve the `msgHash`, which can be used to track its status.&#x20;

{% hint style="success" %}
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>
{% endhint %}

#### 4.1 Execute Swap with Simulation Result

Invoke the callback function returned by simulate swap method

```typescript
// Send Transaction and get msghash
const sender = wallet.address;
const senderArgs = await getSwapPayload(sender, {
    blockNumber: blockNumber, // Optional, but it will speed up a lot if you provided the latest blockNumber here
  });
const msgHash = await send(senderArgs); 
console.log(`Transaction sent with msghash: ${msgHash}`);

// ... Query transaction hash by message hash via Toncenter API V3 or TON Console
```

#### 4.2 Execute Swap Directly with Swap Params

Invoke sdk.getSwapPayload directly

```typescript
const sender = wallet.address;
const senderArgs = await sdk.getSwapPayload(sender, {
    blockNumber: blockNumber, // Optional, but it will speed up a lot if you provided the latest blockNumber here
  });
const msgHash = await send(senderArgs); 
console.log(`Transaction sent with msghash: ${msgHash}`);
```
