This repository contains the core smart contracts for the Dex Router on Solana Blockchain. The router enables efficient token swaps across multiple liquidity sources and protocols through a unified interface.
- Multi-DEX Aggregation: Supports 30+ Solana ecosystem DEXs with automatic best path finding
- Smart Routing Algorithm: X Routing algorithm automatically selects optimal trading paths to maximize user returns
- Fee Management: Flexible fee system supporting proxy trading and platform fees
- Basic Swap: Standard token exchange
- Proxy Swap: Trading through proxy accounts
- Commission Swap: Trading mode with commission collection
- Platform Fee Swap: Trading mode with platform fee collection
- Wrap/Unwrap: SOL to wSOL conversion
- Solana CLI: 2.2.12+
- Anchor Framework: 0.31.1+
- Rust: 1.85+
- Node.js: 23+
For Mac and Linux, run the following single command to install all dependencies:
curl --proto '=https' --tlsv1.2 -sSfL https://solana-install.solana.workers.dev | bashWindows users: You must first install WSL (see Installation Dependencies). Then run the above command in the Ubuntu (Linux) terminal.
After installation, you should see output similar to:
Installed Versions:
Rust: rustc 1.86.0 (05f9846f8 2025-03-31)
Solana CLI: solana-cli 2.2.12 (src:0315eb6a; feat:1522022101, client:Agave)
Anchor CLI: anchor-cli 0.31.1
Node.js: v23.11.0
Yarn: 1.22.1
If the quick installation doesn't work, please refer to the Solana Installation Guide for detailed manual installation instructions for each component.
Verify that all components are properly installed:
# Verify Rust
rustc --version
# Verify Solana CLI
solana --version
# Verify Anchor CLI
anchor --version
# Verify Node.js
node --version
# Verify Yarn
yarn --versiongit clone <repository-url>
cd DEX-Router-Solana-V1# Build all programs
anchor build
# Build specific program
anchor build -p dex-solana# Run all tests
anchor test
# Run specific program tests
anchor test -p dex-solanaBefore calling swap methods, you need to assemble the required parameters:
interface SwapArgs {
amountIn: BN; // Input amount
expectAmountOut: BN; // Expected output amount
minReturn: BN; // Minimum return amount
amounts: BN[]; // 1st level split amounts
routes: Route[][]; // 2nd level split routes
}
interface Route {
dexes: Dex[]; // DEX types for this route
weights: number[]; // Weights for each DEX
}
enum Dex {
SplTokenSwap,
StableSwap,
Whirlpool,
MeteoraDynamicpool,
RaydiumSwap,
RaydiumStableSwap,
RaydiumClmmSwap,
// ... more DEX types
}For commission and platform fee methods, commission info is encoded as a 32-bit integer:
// Commission info encoding
const commissionDirection = true; // true: from input, false: from output
const commissionRate = 100; // Rate in basis points (0.01%)
const commissionInfo =
(commissionDirection ? 1 << 31 : 0) | (commissionRate & ((1 << 30) - 1));// Basic token exchange
const swapArgs = {
amountIn: new BN(1000000), // 1 token (assuming 6 decimals)
expectAmountOut: new BN(950000), // Expected output
minReturn: new BN(900000), // Minimum return
amounts: [new BN(1000000)], // Single split
routes: [[{ dexes: [Dex.RaydiumSwap], weights: [100] }]], // Single route
};
const swapTx = await program.methods
.swap(swapArgs, orderId)
.accounts(swapAccounts)
.rpc();// Trading through proxy account
const proxySwapTx = await program.methods
.proxySwap(swapArgs, orderId)
.accounts(proxySwapAccounts)
.rpc();// Trading with commission collection
const commissionSwapArgs = {
...swapArgs,
commissionRate: 100, // 1% commission
commissionDirection: true, // Commission from input
};
const commissionSwapTx = await program.methods
.commissionSplSwap(commissionSwapArgs, orderId)
.accounts(commissionSwapAccounts)
.rpc();// Trading with platform fee collection
const platformFeeRate = 50; // 0.5% platform fee
const trimRate = 10; // 1% trim rate
const platformFeeSwapTx = await program.methods
.platformFeeSolProxySwapV2(
swapArgs,
commissionInfo,
platformFeeRate,
trimRate,
orderId
)
.accounts(platformFeeAccounts)
.rpc();// Swap with commission and optional platform fee
const commissionInfo =
(commissionDirection ? 1 << 31 : 0) | (commissionRate & ((1 << 30) - 1));
const platformFeeRate = 50; // 0.5% platform fee
const trimRate = 10; // 1% trim rate
const swapToBTx = await program.methods
.swapTobV3(swapArgs, commissionInfo, trimRate, platformFeeRate, orderId)
.accounts(swapV3Accounts)
.rpc();// Swap with commission and platform fee (no trim)
const swapToCTx = await program.methods
.swapV3(swapArgs, commissionInfo, platformFeeRate, orderId)
.accounts(swapV3Accounts)
.rpc();// Complex routing with multiple hops and DEXs
const complexSwapArgs = {
amountIn: new BN(1000000),
expectAmountOut: new BN(950000),
minReturn: new BN(900000),
amounts: [
new BN(600000), // 60% through route 1
new BN(400000), // 40% through route 2
],
routes: [
[
{ dexes: [Dex.RaydiumSwap], weights: [100] },
{ dexes: [Dex.Whirlpool], weights: [100] },
],
[{ dexes: [Dex.MeteoraDynamicpool], weights: [100] }],
],
};DEX-Router-Solana-V1/
βββ Anchor.toml # Anchor configuration
βββ Cargo.toml # Rust workspace configuration
βββ programs/
β βββ dex-solana/ # Main program
β βββ src/
β β βββ adapters/ # DEX adapters
β β βββ instructions/ # Instruction handlers
β β βββ state/ # State definitions
β β βββ utils/ # Utility functions
β β βββ lib.rs # Program entry point
β βββ Cargo.toml
βββ tests/ # Test files
βββ README.md
- Mainnet:
6m2CDdhRgxpH4WjvdzxAYbGxwdGUz5MziiL5jek2kBma - Testnet: Configure based on deployment environment
# Run all tests
anchor test
# Run specific test file
anchor test tests/specific_test.ts
# Run tests with detailed logs
anchor test --skip-lintThis project is licensed under the MIT License - see the LICENSE file for details.
We welcome contributions! Please see our Discord community for technical discussions and support.
- Join Community Discussions - Help other developers in our Discord
- Open Issues - Report bugs or suggest features
- Submit Pull Requests - Contribute code improvements
- Discuss non-trivial changes in an issue first
- Include tests for new functionality
- Update documentation as needed
- Add a changelog entry describing your changes