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
Start at active bin: Current trading price
Consume liquidity: Use available reserves
Move to next bin: If more needed
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
Related Topics
Swap Direction - Understanding X→Y vs Y→X
Calculate Swap Output - Preview results
Price Impact - Large swap effects
Slippage Protection - Set minimum output
Last updated