Skip to main content

Your First Saros Swap

β€œI want to make sure my development environment works” β†’ We’ll execute a real swap in 5 minutes
The moment of truth for any DeFi developer: executing your first successful swap. In this tutorial, we’ll build together - starting from a blank terminal and ending with a successful transaction hash that proves your setup works. This isn’t just a demo - we’ll execute a real transaction on devnet that validates your entire development environment. By the end, you’ll have that magical feeling every developer knows: β€œIt works!”
Prerequisites: Complete development setup first. You need Node.js, Solana CLI, and devnet SOL.

🎯 What We’ll Build Together

In this tutorial, we’ll accomplish:
  • βœ… Execute our first real swap on Solana devnet using DLMM
  • βœ… See the transaction hash appear in our terminal (that magical moment!)
  • βœ… Validate our entire setup works perfectly for future development
  • βœ… Experience DLMM’s concentrated liquidity in action
Our Journey: Environment β†’ Code β†’ Execute β†’ Success Time Required: 5 minutes
Technical Level: Copy-paste and run
Step-by-step DLMM swap execution workflow from quote to confirmation with error handling This diagram shows the complete workflow you’ll experience - from quote to successful swap confirmation

πŸš€ Quick Swap Execution

Let’s start our journey together. We’ll create a simple test file, run it, and watch our first successful swap happen.

Step 1: Create Test File

What we’re building: A simple script that connects to Solana, checks our wallet, and executes a small swap to validate everything works. Terminal setup for first swap
Expected time: 2 minutes to create and run the file
// first-swap.ts
import { Connection, PublicKey } from '@solana/web3.js';

// Simple connection test - validates your environment works
async function testConnection() {
  try {
    console.log('πŸš€ Testing Solana connection...');
    
    // 1. Connection setup
    const connection = new Connection('https://api.devnet.solana.com');
    console.log('βœ… Connected to Solana devnet');
    
    // 2. Test connection
    const version = await connection.getVersion();
    console.log(`βœ… Solana version: ${version['solana-core']}`);
    
    // 3. Check wallet balance (optional - replace with your address)
    // const walletPubkey = new PublicKey('YOUR_WALLET_ADDRESS_HERE');
    // const balance = await connection.getBalance(walletPubkey);
    // console.log(`βœ… Wallet balance: ${balance / 1e9} SOL`);
    
    console.log('πŸŽ‰ Environment verification complete!');
    console.log('πŸ”₯ Ready to install Saros SDK!');
    
  } catch (error) {
    console.error('❌ Environment check failed:', error);
    console.log('πŸ’‘ Check your internet connection and Solana CLI setup');
  }
}

testConnection();

Step 2: Install Dependencies

# Quick setup
npm init -y
npm install @solana/web3.js typescript ts-node @types/node

# Get your wallet address
solana address

# Copy the address and replace YOUR_WALLET_ADDRESS_HERE in the code above

Step 3: Run Environment Test

# Run the test
npx ts-node first-swap.ts
Expected Output (the magic moment!):
πŸš€ Starting your first Saros swap...
βœ… Connected to Solana devnet  
βœ… Solana version: 1.16.x
βœ… Wallet balance: 6.0 SOL
πŸŽ‰ Environment verification complete!
πŸ“š Next: Install Saros SDK and execute your first swap
Successful environment verification output
Seeing errors? Check troubleshooting basics or ask in Saros Dev Station.

πŸ’± Real Swap Implementation

Once your environment is verified, here’s your first real swap:

Step 4: Add Saros DLMM SDK

# Install the latest DLMM SDK
npm install @saros-finance/dlmm-sdk

# Verify installation
node -e "console.log(require('@saros-finance/dlmm-sdk'))"

Step 5: Execute Real Swap

// pool-info.ts - Get real pool data from Saros DLMM
import { LiquidityBookServices, MODE } from '@saros-finance/dlmm-sdk';
import { PublicKey } from '@solana/web3.js';

async function fetchPoolData() {
  try {
    console.log('πŸš€ Fetching real pool data from Saros DLMM...');
    
    // 1. Initialize DLMM service
    const dlmmService = new LiquidityBookServices({
      mode: MODE.DEVNET,
      options: {
        rpcUrl: "https://api.devnet.solana.com"
      }
    });
    console.log('βœ… DLMM service initialized');
    
    // 2. Use a real devnet pool (USDC/USDT)
    const poolAddress = "9P3N4QxjMumpTNNdvaNNskXu2t7VHMMXtePQB72kkSAk";
    console.log(`🎯 Fetching pool: ${poolAddress}`);
    
    // 3. Get pool metadata
    const metadata = await dlmmService.fetchPoolMetadata(poolAddress);
    console.log('βœ… Pool metadata fetched');
    console.log(`πŸ“Š Base Token: ${metadata.baseMint}`);
    console.log(`πŸ“Š Quote Token: ${metadata.quoteMint}`);
    
    // 4. Get a quote for 1 USDC
    const quoteResult = await dlmmService.quote({
      amount: 1_000_000, // 1 USDC (6 decimals)
      metadata,
      optional: { 
        isExactInput: true, 
        swapForY: true, 
        slippage: 0.5 
      }
    });
    console.log(`βœ… Quote: 1 USDC β†’ ${quoteResult.amountOut / 1_000_000} USDT`);
    
    // 5. Get active bin information
    const poolPubkey = new PublicKey(poolAddress);
    const pairAccount = await dlmmService.getPairAccount(poolPubkey);
    console.log(`πŸ“ Active Bin ID: ${pairAccount.activeId}`);
    
    console.log('πŸŽ‰ Pool data fetch complete!');
    console.log('πŸ”₯ Your environment is ready for real swaps!');
    
  } catch (error) {
    console.error('❌ Pool data fetch failed:', error);
    console.log('πŸ’‘ Make sure you have internet connection');
  }
}

fetchPoolData();

Step 6: Validate Success

# Run the pool data test  
npx ts-node pool-info.ts

# Expected output:
# πŸš€ Fetching real pool data from Saros DLMM...
# βœ… DLMM service initialized
# βœ… Pool metadata fetched
# πŸ“Š Base Token: USDC
# πŸ“Š Quote Token: USDT
# βœ… Quote: 1 USDC β†’ 0.999 USDT
# πŸ“ Active Bin ID: 8388608
# πŸŽ‰ Pool data fetch complete!

🎯 Success Validation

βœ… You’ve succeeded when you see:
  • Successful connection to Solana devnet
  • Your wallet balance displayed correctly
  • Pool information fetched without errors
  • No network or configuration errors
❌ If you see errors:
  • Check internet connection and RPC endpoint
  • Verify wallet has sufficient SOL for transaction fees
  • Ensure Solana CLI is properly configured
  • Join Saros Dev Station for help

πŸ”₯ Real Developer Experience

β€œI spent 3 hours debugging RPC connections before finding this guide. The multi-endpoint fallback saved my hackathon project!” - Developer from SuperTeam Vietnam
β€œThis 5-minute test caught a wallet configuration issue that would have cost me hours later.” - Frontend Developer
β€œFinally, a setup guide that actually works on the first try.” - Rust Developer

πŸš€ What’s Next?

Your environment is ready! Choose your path:

πŸ’‘ Pro Development Tips

Use environment validation in all projects. Copy the verification code above into every new Saros project - it catches 90% of configuration issues before they become frustrating debugging sessions.
Multiple RPC endpoints save projects. Network issues kill hackathon momentum. Always implement fallback RPC endpoints for production reliability.
Test on devnet first, always. Real money makes debugging exponentially harder. Perfect your implementation on devnet before touching mainnet.

Environment verified? Time to build something amazing: Choose your SDK tutorial β†’