A TypeScript example demonstrating how to use the Soroswap SDK to interact with the Soroswap DEX on Stellar network.
This project showcases how to:
- Quote token swaps using the Soroswap API
- Build swap transactions
- Sign and submit transactions to the Stellar network
- Node.js (v18 or higher)
- npm or pnpm
- A Stellar account with:
- Testnet XLM for fees
- Trustlines established for the tokens you want to trade
- Soroswap API key
-
Clone this repository
-
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env
-
Edit the
.envfile with your actual values:SOROSWAP_API_KEY: Your API key from api.soroswap.financeUSER_SECRET: Your Stellar account secret keySOROSWAP_API_URL: API endpoint (optional, defaults to production)
npm run devnpm run build
npm run start:buildThe example is configured to:
- Trade on Stellar Testnet
- Swap 1 XLM for USDC
- Use 0.8% slippage tolerance
- Route through Soroswap protocol
- XLM:
CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC - USDC:
CDWEFYYHMGEZEFC5TBUDXM3IJJ7K7W5BDGE765UIYQEV4JFWDOLSTOEK
src/index.ts: Main example implementation.env.example: Environment variables templatepackage.json: Project dependencies and scripts
The example includes rate limiting delays to respect API limits. Each API call is followed by a 1-second delay.
The code includes basic error handling that will log any issues encountered during the swap process.
- @soroswap/sdk: Official Soroswap SDK
- @stellar/stellar-sdk: Stellar SDK for transaction handling
- dotenv: Environment variable management
- TypeScript: Type safety and development experience
- Visit api.soroswap.finance
- Register for an API key
- Add the key to your
.envfile
- Never commit your
.envfile with real credentials - Use testnet for development and testing
- Ensure your account has sufficient XLM for transaction fees
- Verify token addresses before trading
ISC