# Swap

## Swap

Swaps typically occur in two steps:

* the first step involves pre-calculating the potential result of the current transaction；
* the second step is to set the slippage based on the pre-calculated results, followed by executing the transaction.

After pre-calcualting then you can do swap via:

* swap
* [partner swap](/cetus-developer-docs/developer/via-sdk/features-available/partner-swap.md)

## Swap after preswap

After pre-calcualting then swap. [For a more detailed understanding of the pre-swap process and its intricacies, additional information is available here.](/cetus-developer-docs/developer/via-sdk/features-available/preswap.md)

#### Function input param

*Please refer to the original function for specific parameter types.*

* **`poolID`**: pool object id, you can get it by pre-calcualting.
* **`coinTypeA`**: the coin type address about coinA.
* **`coinTypeB`**: the coin type address about coinB.
* **`a2b`**: swap direction, `true` means swap from coinA to coinB, `false` means swap from coinB to CoinA.
* **`byAmountIn`**: `true` means fixed the amount of input, `false` means fixed the amount of output.
* **`amount`**: the amount of input (`byAmountIn` = `true`) or output (`byAmountIn` = `false`).
* **`amountLimit`**: the amount limit of coin what you get. There are two scenarios in amount limit, when by\_amount\_in equals true, amount limit means minimum number of outputs required to be obtained, when by\_amount\_in equals false, it means maximum number of input coin.
* **`partner`**: The partner address. If you do not have a partner, simply leave the partner field unset.

  *Notice*:

  * This is the amount out of result after slippage adjustment. Use `adjustForSlippage` to calculate the limit of amount out.
  * If you set amount limit equal 0, when you trade during extremely volatile price fluctuations, you might end up with a very small trading outcome. The `amountLimit` will help prevent your assets from incurring losses.
  * You can get more details in these [Partner swap ](/cetus-developer-docs/developer/via-sdk/features-available/partner-swap.md)parts.

#### Example

<pre class="language-ts"><code class="lang-ts"><strong>import BN from 'bn.js'
</strong>import { adjustForSlippage, d, initCetusSDK, Percentage, TransactionUtil } from '@cetusprotocol/cetus-sui-clmm-sdk'

const sdk = initCetusSDK({ network: 'mainnet' })
const sendKeypair = buildTestAccount()
// Whether the swap direction is token a to token b
const a2b = true
// fix input token amount
const coinAmount = new BN(120000)
// slippage value
const slippage = Percentage.fromDecimal(d(5))
// Fetch pool data
const pool = await sdk.Pool.getPool(poolAddress)
// Estimated amountIn amountOut fee
const res: any = await sdk.Swap.preswap({
  pool: pool,
  current_sqrt_price: pool.current_sqrt_price,
  coinTypeA: pool.coinTypeA,
  coinTypeB: pool.coinTypeB,
  decimalsA: 6, // coin a 's decimals
  decimalsB: 8, // coin b 's decimals
  a2b,
  by_amount_in: true, // fix token a amount
  amount,
})

const partner = '0x8e0b7668a79592f70fbfb1ae0aebaf9e2019a7049783b9a4b6fe7c6ae038b528'

const toAmount = byAmountIn ? res.estimatedAmountOut : res.estimatedAmountIn
const amountLimit =  adjustForSlippage(toAmount, slippage, !byAmountIn)

// build swap Payload
const swapPayload = sdk.Swap.createSwapTransactionPayload(
  {
    pool_id: pool.poolAddress,
    coinTypeA: pool.coinTypeA,
    coinTypeB: pool.coinTypeB
    a2b: a2b,
    by_amount_in: by_amount_in,
    amount: res.amount.toString(),
    amount_limit: amountLimit.toString(),
    swap_partner: partner,
  },
)

const swapTxn = await sdk.fullClient.sendTransaction(signer, swapPayload)
</code></pre>

## Swap without transfer coins

This methods support return two coins for user to build PTB.

#### Function input param

*Please refer to the original function for specific parameter types.*

* **`poolID`**: pool object id, you can get it by pre-calcualting.
* **`coinTypeA`**: the coin type address about coinA.
* **`coinTypeB`**: the coin type address about coinB.
* **`a2b`**: swap direction, `true` means swap from coinA to coinB, `false` means swap from coinB to CoinA.
* **`byAmountIn`**: `true` means fixed the amount of input, `false` means fixed the amount of output.
* **`amount`**: the amount of input (`byAmountIn` = `true`) or output (`byAmountIn` = `false`).
* **`amountLimit`**: the amount limit of coin what you get. There are two scenarios in amount limit, when by\_amount\_in equals true, amount limit means minimum number of outputs required to be obtained, when by\_amount\_in equals false, it means maximum number of input coin.
* **`partner`**: The partner address. If you do not have a partner, simply leave the partner field unset.

  *Notice*:

  * This is the amount out of result after slippage adjustment. Use `adjustForSlippage` to calculate the limit of amount out.
  * If you set amount limit equal 0, when you trade during extremely volatile price fluctuations, you might end up with a very small trading outcome. The `amountLimit` will help prevent your assets from incurring losses.
  * You can get more details in these [Partner swap ](/cetus-developer-docs/developer/via-sdk/features-available/partner-swap.md)parts.

#### Example

```ts
import BN from 'bn.js'
import { adjustForSlippage, d, initCetusSDK, Percentage, TransactionUtil } from '@cetusprotocol/cetus-sui-clmm-sdk'

const sdk = initCetusSDK({ network: 'mainnet' })
const sendKeypair = buildTestAccount()
// Whether the swap direction is token a to token b
const a2b = true
// fix input token amount
const coinAmount = new BN(120000)
// input token amount is token a
const byAmountIn = true
// slippage value
const slippage = Percentage.fromDecimal(d(5))
// Fetch pool data
const pool = await sdk.Pool.getPool(poolAddress)
// Estimated amountIn amountOut fee
const res: any = await sdk.Swap.preswap({
  pool: pool,
  current_sqrt_price: pool.current_sqrt_price,
  coinTypeA: pool.coinTypeA,
  coinTypeB: pool.coinTypeB,
  decimalsA: 6, // coin a 's decimals
  decimalsB: 8, // coin b 's decimals
  a2b,
  by_amount_in,
  amount,
})

const partner = '0x8e0b7668a79592f70fbfb1ae0aebaf9e2019a7049783b9a4b6fe7c6ae038b528'

const toAmount = byAmountIn ? res.estimatedAmountOut : res.estimatedAmountIn
const amountLimit =  adjustForSlippage(toAmount, slippage, !byAmountIn)

// build swap Tx
const {swapTx, coinsAB} = sdk.Swap.createSwapTransactionWithoutTransferCoinsPayload(
  {
    pool_id: pool.poolAddress,
    coinTypeA: pool.coinTypeA,
    coinTypeB: pool.coinTypeB
    a2b: a2b,
    by_amount_in: by_amount_in,
    amount: res.amount.toString(),
    amount_limit: amountLimit.toString(),
    swap_partner: partner,
  },
)
// transfer coin a and coin b
 swapTx.transferObjects(coins, tx.pure.address(recipient))

const transferTxn = await sdk.fullClient.sendTransaction(signer,swapPayload)
console.log('swap: ', transferTxn)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-sdk/features-available/swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
