A Node.js Solana token swap service using Jupiter Aggregator for optimal swap routes across Solana DEXs.
This project enables seamless token swaps on Solana by leveraging Jupiter's liquidity aggregation. It provides a simple Node.js service for executing swaps directly from the command line.
- Token Swaps: Swap any SPL token using Jupiter's liquidity aggregation
- Optimal Routes: Automatically finds the best swap routes across multiple DEXs
- Wallet Integration: Secure wallet signing and transaction execution
- Real-time Quotes: Live price quotes and slippage protection
solana-swap/
├── swap.js # Main swap execution script
├── package.json # Node.js dependencies
├── .env.example # Environment variables template
├── .gitignore # Git ignore rules
└── README.md # Documentation
- Node.js 18+
- A Solana wallet with SOL for transaction fees
- RPC endpoint access (uses public RPC by default)
- Clone the repository:
git clone https://github.com/yuvrajnode/swap-contract.git
cd swap-contract- Install dependencies:
npm install- Configure environment variables:
# Create .env file
echo "PRIVATE_KEY=your_wallet_private_key_here" > .envExecute a swap from the command line:
npm startThis will:
- Fetch a quote for swapping SOL → USDC
- Request a swap transaction from Jupiter
- Sign and submit the transaction
- Output the Solscan transaction URL
Create a .env file in the project root:
| Variable | Description | Required |
|---|---|---|
PRIVATE_KEY |
Base58 encoded private key for signing transactions | Yes |
The default swap configuration in swap.js:
- Input: SOL (So11111111111111111111111111111111111111112)
- Output: USDC (EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)
- Amount: 0.1 SOL (100000000 lamports)
- Slippage: 0.5% (50 bps)
Modify these values in swap.js to customize your swap.
This project uses Jupiter's v1 API:
- Quote API:
https://lite-api.jup.ag/swap/v1/quote - Swap API:
https://lite-api.jup.ag/swap/v1/swap
For production use, consider:
- Using your own RPC endpoint
- Implementing rate limiting
- Adding retry logic for failed transactions
- Never commit your
.envfile or private keys - Use a dedicated wallet for swaps (not your main wallet)
- Always verify transaction details before signing
- Test with small amounts first
Any SPL token can be swapped using Jupiter. Common token mints:
| Token | Mint Address |
|---|---|
| SOL | So11111111111111111111111111111111111111112 |
| USDC | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v |
| USDT | Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB |
| BONK | DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263 |
- Insufficient balance: Ensure you have enough SOL for the swap amount plus transaction fees
- Transaction failed: Check slippage settings and token account existence
- RPC errors: The public RPC may be rate-limited; use a private RPC for better reliability
- Jupiter Documentation
- Solana Web3.js Docs
- Open an issue on GitHub
@solana/web3.js: Solana blockchain interaction@project-serum/anchor: Wallet and transaction handlingaxios: HTTP client for API requestsbs58: Base58 encoding for private keysdotenv: Environment variable management
Contributions are welcome! Please feel free to submit issues or pull requests.
ISC
This software is provided for educational purposes. Use at your own risk. The authors are not responsible for any financial losses incurred through the use of this software.