This guide shows how to use the Saros TypeScript/JavaScript SDK (@saros-finance/sdk) on Solana’s Devnet. We cover environment setup, wallet generation and funding, connecting to Devnet, swapping tokens, adding liquidity, and staking LP tokens (with reward claiming). All steps include code samples (Node.js) and references to official docs or guides. Short paragraphs and lists make it easy to scan; each key statement cites a source.
Node.js: Install Node.js (v16 or later) from the official site. In your project folder, initialize NPM and install packages:
npm init -y
npm install @saros-finance/sdk @solana/web3.js @solana/spl-token bn.js dotenv
These include the Saros SDK, Solana Web3, SPL-Token helpers, big-number library, and dotenv for env vars. (If using TypeScript, add typescript and configure tsconfig.json.)
Imports: Add the required modules to your script. For example, this is how your lines should appear if you are setting up AMM and Farming
import { Connection, Keypair, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
import { SarosAMM, SarosFarmService } from "@saros-finance/sdk";
import { createMint, getOrCreateAssociatedTokenAccount, mintTo } from "@solana/spl-token";
import BN from "bn.js";
These allow creating tokens, fetching pool info, depositing liquidity, and staking LP.
Devnet Connection: Use Solana’s Devnet RPC. For example:
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
The "confirmed" commitment ensures reliable finality. (You can also use clusterApiUrl('devnet') which defaults to confirmed commitment.)
Create Keypair: Generate a new Devnet keypair. For CLI users:
solana-keygen new --no-bip39-passphrase --outfile ./devnet.json
Or in code, use Keypair.generate() and save the secret. Print the public key to confirm:
const wallet = Keypair.generate();
console.log("New wallet address:", wallet.publicKey.toBase58());
Keep the keypair file (or private key) secure.
Airdrop SOL: Use Devnet SOL to load the wallet and pay fees. To obtain Devnet SOL in Node.js, use the example below:
const airdropSig = await connection.requestAirdrop(wallet.publicKey, 2 * LAMPORTS_PER_SOL);
await connection.confirmTransaction(airdropSig, "confirmed");
console.log("Airdropped 2 SOL (tx):", airdropSig);
You can also use solana airdrop 2 (Devnet) from the CLI. Devnet faucets are rate-limited: 2 SOL per request and about 24 SOL per day per account. If you hit a limit, wait or use another funded account. Always log the transaction signature and verify it on the **Solana Explorer.**
Initialize the Saros AMM client and fetch available pools:
const amm = new SarosAMM(connection, wallet);
const pools = await amm.getPools();
console.log("Found AMM pools:", pools);
This returns an array of pool objects (address, token mints, etc.). For example:
for (const pool of pools) {
console.log(`Pool ${pool.address}: tokens = ${pool.tokenA.symbol}-${pool.tokenB.symbol}`);
}
Make sure you have SOL (Devnet) in the wallet before querying or swapping. You may want to try/catch these calls to handle errors (e.g. network issues).
Once you have a funded wallet and know the pool addresses and token mints, you can swap tokens. For example, to swap 1 SOL → USDC:
const SOL_MINT = new PublicKey("So11111111111111111111111111111111111111112"); // Wrapped SOL
const USDC_MINT = new PublicKey("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"); // Official Devnet USDC
try {
const swapSig = await amm.swap({
poolAddress: pools[0].address, // pick a suitable pool from getPools()
fromMint: SOL_MINT,
toMint: USDC_MINT,
amountIn: 1 * LAMPORTS_PER_SOL, // 1 SOL
slippage: 0.5 // 0.5% tolerance
});
console.log("Swap tx signature:", swapSig);
console.log(`Explorer: https://explorer.solana.com/tx/${swapSig}?cluster=devnet`);
} catch (err) {
console.error("Swap failed:", err);
}
This is similar to Saros's example. For your convenience, it logs the transaction signature. On Devnet, use the explorer URL with ?cluster=devnet. If you see an “invalid pool” error or swap failure, first check that the pool address matches the token mints. If the price moved significantly, increase the slippage (e.g. to 1.0%). Also, ensure you’re using at least "confirmed" commitment (default above) and have enough SOL for fees.