> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xyzchain.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Swapping

> How to swap tokens on the XYZ AMM

# Swapping

You can swap between XYZ and any graduated token on the AMM. Swaps execute instantly against the pool's reserves using the constant-product formula.

## Swap XYZ for Tokens

To buy tokens with XYZ:

1. Navigate to the token's trading page
2. Enter the amount of **XYZ** you want to spend
3. Review the estimated tokens you'll receive and price impact
4. Click **Swap** and confirm in your wallet

### Contract Message

```json theme={null}
{
  "swap": {
    "token_address": "<cw20_token_address>",
    "offer_xyz": true,
    "min_output": "1000000"
  }
}
```

**Funds**: `[{ "denom": "uxyz", "amount": "<xyz_to_swap>" }]`

The `min_output` parameter protects against slippage -- the transaction reverts if you'd receive fewer tokens than specified.

## Swap Tokens for XYZ

To sell tokens for XYZ, the AMM uses the **CW20 Send** pattern. Instead of calling the AMM directly, you send tokens to the AMM contract with an encoded swap message:

```json theme={null}
// Sent to the CW20 TOKEN contract (not the AMM)
{
  "send": {
    "contract": "<amm_contract_address>",
    "amount": "1000000",
    "msg": "<base64_encoded_message>"
  }
}
```

The inner message (before base64 encoding):

```json theme={null}
{
  "min_output": "500000"
}
```

<Tip>
  The TypeScript SDK handles the CW20 Send encoding automatically:

  ```typescript theme={null}
  import { swapTokenForXyz } from "@xyz-chain/sdk";

  await swapTokenForXyz(contractClient, senderAddress, tokenAddress, tokenAmount, minOutput);
  ```
</Tip>

## Simulating Swaps

Preview any swap before executing:

```json theme={null}
{
  "simulate_swap": {
    "token_address": "<cw20_token_address>",
    "offer_xyz": true,
    "offer_amount": "1000000"
  }
}
```

Set `offer_xyz: true` for XYZ to Token, `false` for Token to XYZ.

**Response:**

```typescript theme={null}
{
  output_amount: string;         // Tokens or XYZ you'd receive
  fee_amount: string;            // Total fee deducted
  price_impact: string;          // e.g. "0.53%"
  augmented_fee_amount: string;  // Extra fee if active (0 otherwise)
}
```

## Understanding Price Impact

Price impact is the difference between the current market price and the effective price of your trade. It depends on trade size relative to pool depth:

| Trade Size vs Pool | Typical Price Impact |
| ------------------ | -------------------- |
| \< 0.1% of pool    | \< 0.1%              |
| 1% of pool         | \~1%                 |
| 5% of pool         | \~5%                 |
| 10% of pool        | \~10%                |

<Warning>
  Large trades relative to pool size will have significant price impact. Consider splitting large orders into smaller trades.
</Warning>

## Fees

| Fee           | Rate | Where It Goes                                |
| ------------- | ---- | -------------------------------------------- |
| Base swap fee | 1%   | Stays in pool (auto-compounds for LPs)       |
| Augmented fee | 0-5% | Stays in pool; auto-disables at target value |

Fees are deducted from your input before the swap calculation.

## Next Steps

<CardGroup cols={2}>
  <Card title="Liquidity" icon="droplet" href="/amm/liquidity">
    How LP tokens and pool mechanics work
  </Card>

  <Card title="AMM Overview" icon="arrow-right-arrow-left" href="/amm/overview">
    Constant-product formula and pool basics
  </Card>
</CardGroup>
