Execute Swap

Perform token swaps through DLMM pairs using the bin-based liquidity system. Swaps traverse through multiple bins as needed to fill the order.

Prerequisites

  • Valid sender address with tokens

  • Pair address for trading

  • Sufficient balance of input token

  • Gas for transaction execution

Basic Swap

const pair = await sdk.Pair.getPair(pairAddress);

// Swap 1 SUI for USDC
const tx = await sdk.Swap.prepareSwap(pair, {
  amount: 1000000000n,  // 1 SUI (9 decimals)
  xtoy: true,           // SUI (X) to USDC (Y)
  recipient: userAddress // Optional, defaults to sender
});

await sdk.fullClient.signAndExecuteTransaction({
  transaction: tx,
  signer: keypair
});

Swap Parameters

Swap Directions

X to Y Swap

Y to X Swap

Understanding Token Order

Complete Swap Example

Swap with Custom Recipient

How Swaps Work

  1. Start at active bin: Current trading price

  2. Consume liquidity: Use available reserves

  3. Move to next bin: If more needed

  4. Continue until filled: Or liquidity exhausted

Common Patterns

Swap Exact Input

Check Balance First

Error Handling

Gas Optimization

  • Swaps through fewer bins cost less gas

  • Large swaps may traverse many bins

  • Consider splitting very large swaps

  • Active bin swaps are most efficient

Last updated