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 callgetQuote()- Get swap quotes with slippage calculationsimulateSwap()- Simulate swaps before executingbuildSwapTransaction()- Build swap transactions manually for advanced use cases
When to use each method
swap()is the simplest approach and recommended for most use casesgetQuote()should be called before swapping to show users expected outputsimulateSwap()is useful for testing and error checkingbuildSwapTransaction()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 ofVertigo.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 quicklyUse
simulateSwap()when testing or for important transactionsThe SDK automatically handles token account creation and SOL wrapping
Last updated

