Skip to main content

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

{
  "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:
// 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):
{
  "min_output": "500000"
}
The TypeScript SDK handles the CW20 Send encoding automatically:
import { swapTokenForXyz } from "@xyz-chain/sdk";

await swapTokenForXyz(contractClient, senderAddress, tokenAddress, tokenAmount, minOutput);

Simulating Swaps

Preview any swap before executing:
{
  "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:
{
  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 PoolTypical Price Impact
< 0.1% of pool< 0.1%
1% of pool~1%
5% of pool~5%
10% of pool~10%
Large trades relative to pool size will have significant price impact. Consider splitting large orders into smaller trades.

Fees

FeeRateWhere It Goes
Base swap fee1%Stays in pool (auto-compounds for LPs)
Augmented fee0-5%Stays in pool; auto-disables at target value
Fees are deducted from your input before the swap calculation.

Next Steps