Swap Tokens

How to swap tokens using Vertigo pools

Overview

The SDK v2 provides a unified swap interface that automatically detects whether you're buying or selling:

  • swap() - Execute token swaps in one function call

  • getQuote() - Get swap quotes with slippage calculation

  • simulateSwap() - Simulate swaps before executing

  • buildSwapTransaction() - Build swap transactions manually for advanced use cases

When to use each method

  • swap() is the simplest approach and recommended for most use cases

  • getQuote() should be called before swapping to show users expected output

  • simulateSwap() is useful for testing and error checking

  • buildSwapTransaction() is for advanced users who need full control over transaction building

Prerequisites

  • The input token account must have sufficient balance

  • You must have a wallet configured (use Vertigo.load() instead of Vertigo.loadReadOnly())

Parameters

For getQuote():

  • inputMint - The public key of the input token

  • outputMint - The public key of the output token

  • amount - The amount of input tokens to swap (in base units)

  • slippageBps - (Optional) Slippage tolerance in basis points (default: 50 = 0.5%)

For swap():

  • inputMint - The public key of the input token

  • outputMint - The public key of the output token

  • amount - The amount of input tokens to swap (in base units)

  • options - (Optional) Configuration object:

    • slippageBps - Slippage tolerance in basis points (default: 50 = 0.5%)

    • priorityFee - Priority fee: "auto" for automatic calculation, or a specific number in micro-lamports

    • wrapSol - Auto-wrap SOL if needed (default: true)

    • simulateFirst - Simulate before executing (default: true)

Example: Basic swap with quote

This example shows how to get a quote and execute a swap from SOL to a custom token.

Example: Selling tokens back to SOL

The same swap() method works in reverse - just swap the input and output mints:

Example: Simulating a swap

Use simulateSwap() to test a swap before executing:

Example: Advanced usage with buildSwapTransaction()

For advanced users who need full control over transaction building:

Error Handling

The SDK provides detailed error messages:

Tips

  • Always get a quote before swapping to show users expected output

  • Use appropriate slippage tolerance (50-100 bps for normal conditions, higher for volatile tokens)

  • Set priorityFee: "auto" to ensure your transaction gets processed quickly

  • Use simulateSwap() when testing or for important transactions

  • The SDK automatically handles token account creation and SOL wrapping

Last updated