# Calculate Swap Output

The `calculateRates` function is used to estimate the result of a token swap within Ferra’s **Discrete Liquidity Market Maker (DLMM)** system. It calculates how much output you would receive from swapping a given amount, along with estimated fees and liquidity availability.

### Prerequisites

* Pair object with current state
* Input amount and direction

### Basic Output Calculation

```typescript
// Calculate output for a swap
const result = sdk.Swap.calculateRates(pair, {
  amount: bigint,
  swapBins: LbPairBinData[],
  xtoy: boolean,
});
```

### Example

```typescript
async function estimateSwap() {
    const pair = await sdk.Pair.getPair(pairId);
    const bins = await sdk.Pair.getPairBinsData(pairId);
    const decimals = a2b ? coinA.decimals : coinB.decimals;
    const parsedAmount = parseUnits(amount.toString(), decimals);

    const result = sdk.Swap.calculateRates(pair, {
      amount: parsedAmount,
      swapBins: bins,
      xtoy: a2b,
    });
}
```

| Name       | Type              | Description                                                                                                            |
| ---------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------- |
| `pair`     | `DLMM.Pair`       | The liquidity pair object, retrieved via `getPair(pairId)`                                                             |
| `amount`   | `bigint`          | The amount to swap, expressed in smallest units (e.g., wei). Use `parseUnits()` to convert from human-readable numbers |
| `swapBins` | `LbPairBinData[]` | The list of liquidity bins for the pair, retrieved via `getPairBinsData(pairId)`                                       |
| `xtoy`     | `boolean`         | Direction of the swap. `true` means swap token X to Y (A to B), `false` for the reverse                                |

| Property             | Type      | Description                                                        |
| -------------------- | --------- | ------------------------------------------------------------------ |
| `amount`             | `bigint`  | Input amount (as provided)                                         |
| `estimatedAmountIn`  | `bigint`  | Estimated actual input required to complete the swap               |
| `estimatedAmountOut` | `bigint`  | Estimated amount received from the swap                            |
| `estimatedFeeAmount` | `bigint`  | Estimated fee deducted during the swap                             |
| `isExceed`           | `boolean` | Indicates whether the requested amount exceeds available liquidity |
| `estimatedEndBinId`  | `bigint`  | The estimated ending square root price                             |
| `isMaxLoop`          | `boolean` | Indicates if the estimated amount exceeds the limit                |
| `priceImpactPct`     | `number`  | The price impact percentage                                        |

### Important Notes

* Output depends on current bin liquidity
* Large swaps may have significant impact
* Always include fees in calculations

### Related Topics

* [Execute Swap](/integration/dlmm/typescript-sdk/swap-operations/execute-swap.md) - Perform the swap
* [Price Impact](/integration/dlmm/typescript-sdk/swap-operations/price-impact.md) - Calculate market impact
* [Slippage Protection](#slippage-calculation) - Set safety limits
* [Get Pair Bins](/integration/dlmm/typescript-sdk/trading-pairs/get-pair-bins.md) - Check liquidity


---

# 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://docs.ferra.ag/integration/dlmm/typescript-sdk/swap-operations/calculate-swap-output.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.
