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
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.
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
π± 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 β