Swap Tokens
How to swap tokens using Vertigo pools
Last updated
import { Vertigo } from "@vertigo-amm/vertigo-sdk";
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import * as anchor from "@coral-xyz/anchor";
import { NATIVE_MINT } from "@solana/spl-token";
async function main() {
// Connect to Solana
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
// Load wallet
const walletKeypair = anchor.Wallet.local();
// Initialize Vertigo SDK
const vertigo = await Vertigo.load({
connection,
wallet: walletKeypair,
network: "devnet",
});
// Define the swap
const inputMint = NATIVE_MINT; // SOL
const outputMint = new PublicKey("<token-mint-address>");
const amount = LAMPORTS_PER_SOL; // 1 SOL
// Get a quote first
const quote = await vertigo.swap.getQuote({
inputMint,
outputMint,
amount,
slippageBps: 50, // 0.5% slippage tolerance
});
console.log("Quote:");
console.log(` Input: ${quote.inputAmount} (${amount / LAMPORTS_PER_SOL} SOL)`);
console.log(` Expected output: ${quote.outputAmount}`);
console.log(` Minimum received: ${quote.minimumReceived}`);
console.log(` Price impact: ${quote.priceImpact}%`);
// Execute the swap
const result = await vertigo.swap.swap({
inputMint,
outputMint,
amount,
options: {
slippageBps: 100, // 1% slippage for execution
priorityFee: "auto", // Automatically calculate priority fee
wrapSol: true, // Auto-wrap SOL if needed
},
});
console.log(`Swap successful!`);
console.log(` Signature: ${result.signature}`);
console.log(` Input amount: ${result.inputAmount}`);
console.log(` Output amount: ${result.outputAmount}`);
}
main();import { Vertigo } from "@vertigo-amm/vertigo-sdk";
import { Connection, PublicKey } from "@solana/web3.js";
import * as anchor from "@coral-xyz/anchor";
import { NATIVE_MINT } from "@solana/spl-token";
async function main() {
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
const walletKeypair = anchor.Wallet.local();
const vertigo = await Vertigo.load({
connection,
wallet: walletKeypair,
network: "devnet",
});
const tokenMint = new PublicKey("<token-mint-address>");
const DECIMALS = 6;
// Sell 100,000 tokens
const sellAmount = 100_000 * (10 ** DECIMALS);
// Get quote
const quote = await vertigo.swap.getQuote({
inputMint: tokenMint, // Selling tokens
outputMint: NATIVE_MINT, // For SOL
amount: sellAmount,
slippageBps: 50,
});
console.log(`Selling ${100_000} tokens for ~${quote.outputAmount / 1e9} SOL`);
// Execute the swap
const result = await vertigo.swap.swap({
inputMint: tokenMint,
outputMint: NATIVE_MINT,
amount: sellAmount,
options: {
slippageBps: 100,
priorityFee: "auto",
},
});
console.log(`Sold tokens for ${result.outputAmount / 1e9} SOL`);
console.log(`Transaction: ${result.signature}`);
}
main();import { Vertigo } from "@vertigo-amm/vertigo-sdk";
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import * as anchor from "@coral-xyz/anchor";
import { NATIVE_MINT } from "@solana/spl-token";
async function main() {
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
const walletKeypair = anchor.Wallet.local();
const vertigo = await Vertigo.load({
connection,
wallet: walletKeypair,
network: "devnet",
});
const inputMint = NATIVE_MINT;
const outputMint = new PublicKey("<token-mint-address>");
const amount = LAMPORTS_PER_SOL;
// Simulate first
const simulation = await vertigo.swap.simulateSwap({
inputMint,
outputMint,
amount,
options: { slippageBps: 100 },
});
if (simulation.success) {
console.log("Simulation successful!");
console.log(`Expected output: ${simulation.outputAmount}`);
// Proceed with actual swap
const result = await vertigo.swap.swap({
inputMint,
outputMint,
amount,
options: { slippageBps: 100 },
});
console.log(`Swap completed: ${result.signature}`);
} else {
console.error(`Simulation failed: ${simulation.error}`);
}
}
main();import { Vertigo } from "@vertigo-amm/vertigo-sdk";
import { Connection, LAMPORTS_PER_SOL, PublicKey, Transaction } from "@solana/web3.js";
import * as anchor from "@coral-xyz/anchor";
import { NATIVE_MINT } from "@solana/spl-token";
async function main() {
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
const walletKeypair = anchor.Wallet.local();
const vertigo = await Vertigo.load({
connection,
wallet: walletKeypair,
network: "devnet",
});
// Get a quote first
const quote = await vertigo.swap.getQuote({
inputMint: NATIVE_MINT,
outputMint: new PublicKey("<token-mint-address>"),
amount: LAMPORTS_PER_SOL,
slippageBps: 50,
});
// Build the transaction manually
const swapTx = await vertigo.swap.buildSwapTransaction(quote, {
priorityFee: "auto",
computeUnits: 200_000,
});
// You can now add more instructions to the transaction
// or modify it as needed before sending
// Sign and send
const signature = await connection.sendTransaction(swapTx, [walletKeypair.payer]);
await connection.confirmTransaction(signature, "confirmed");
console.log(`Transaction: ${signature}`);
}
main();try {
const result = await vertigo.swap.swap({
inputMint,
outputMint,
amount,
options: { slippageBps: 50 },
});
console.log(`Success: ${result.signature}`);
} catch (error) {
if (error.code === "SLIPPAGE_EXCEEDED") {
console.error("Price moved too much. Try increasing slippage tolerance.");
} else if (error.code === "INSUFFICIENT_FUNDS") {
console.error("Not enough tokens in your account.");
} else if (error.code === "POOL_NOT_FOUND") {
console.error("No pool exists for this token pair.");
} else {
console.error(`Swap failed: ${error.message}`);
}
}