A simple, educational DEX (Decentralized Exchange) frontend for swapping tokens on Stellar Soroban. This project demonstrates how to interact with a Soroban AMM (Automated Market Maker) smart contract using TypeScript and React.
Youtube link - https://youtu.be/ZGt_1orN4vo
Stellar.vidio1.mp4
Contract ID: CDRUJA7RWIJNPD4GHXIPC5PAPKXJKXGYJXZKUQ3HKLNLCXY4JBFZXS3E
- Swap Tokens: Exchange between Token A and Token B using an AMM formula
- View Pool Reserves: See real-time liquidity pool data
- Price Estimation: Calculate expected output and price impact before swapping
- Slippage Protection: Set tolerance levels to protect against unfavorable price changes
Before you begin, make sure you have:
- Node.js (v18 or higher) - Download here
- npm or yarn package manager (comes with Node.js)
- Stellar testnet account with XLM for gas fees
- Your Soroban contract deployed on testnet
- Contract ID:
CDRUJA7RWIJNPD4GHXIPC5PAPKXJKXGYJXZKUQ3HKLNLCXY4JBFZXS3E
- Contract ID:
Navigate to the project folder and install all required packages:
cd Basic-Token-Swap-Interface
npm installThis installs:
- React + TypeScript for the UI
- Vite for fast development
- Stellar SDK for blockchain interactions
- Tailwind CSS for styling
Create a .env file by copying the example:
cp .env.example .envThen edit .env and add your values:
# Stellar Network Configuration
VITE_STELLAR_NETWORK=testnet
VITE_STELLAR_RPC_URL=https://soroban-testnet.stellar.org
# Your deployed contract ID
VITE_CONTRACT_ID=CDRUJA7RWIJNPD4GHXIPC5PAPKXJKXGYJXZKUQ3HKLNLCXY4JBFZXS3E
# FOR LOCAL TESTING ONLY - Get from Stellar Laboratory
VITE_TEST_SECRET_KEY=YOUR_SECRET_KEY_HERE.env file or share your secret key! The secret key is only for local testing. In production, use Freighter wallet instead.
Your Soroban contract might use different method names than what we guessed. Open src/soroban/soroban.ts and update these:
// Line ~150 - Update this to match your contract
contract.call(
'get_reserves' // β οΈ Change to your actual method name
)
// Line ~250 - Update this to match your contract
contract.call(
'swap', // β οΈ Change to your actual method name
fromTokenScVal,
amountInScVal,
minOutputScVal
)How to find your contract's method names:
- Check your contract source code (e.g.,
contract/src/lib.rs) - Look for
#[contractimpl]functions - Use those exact function names in the
contract.call()statements
Start the local development server:
npm run devYou should see output like:
VITE v5.0.8 ready in 500 ms
β Local: http://localhost:5173/
β Network: use --host to expose
Open your browser and go to http://localhost:5173/
-
Check Pool Reserves
- When the app loads, it should display Token A and Token B reserves
- If you see an error, verify your contract ID in
.env
-
Perform a Test Swap
- Select Token A or Token B from the dropdown
- Enter an amount (e.g.,
10) - Review the estimated output and price impact
- Adjust slippage tolerance if needed (default: 1%)
- Click "Swap Tokens"
-
Monitor the Transaction
- Watch the transaction status at the bottom
- After success, pool reserves should update automatically
You'll need testnet tokens to test swaps:
-
Get XLM for gas fees:
- Visit: https://laboratory.stellar.org/#account-creator?network=test
- Create an account and save the secret key
- The account comes with 10,000 test XLM
-
Get test tokens (if your contract supports initialization):
- You may need to call your contract's initialization method
- Or deploy test tokens using Stellar CLI
Problem: "Failed to fetch pool data"
- Solution: Check if your contract is initialized on testnet
- Verify the contract ID in
.envmatches your deployed contract
Problem: "Transaction simulation failed"
- Solution: Check contract method names in
src/soroban/soroban.ts - Ensure your account has enough XLM for gas fees
- Verify network configuration (testnet vs futurenet)
Problem: "No test secret key found"
- Solution: Add
VITE_TEST_SECRET_KEYto your.envfile - Get a testnet account from Stellar Laboratory
src/
βββ main.tsx # App entry point
βββ App.tsx # Main app component
βββ components/
β βββ SwapWidget.tsx # Token swap UI
β βββ PoolInfo.tsx # Pool reserves display
βββ soroban/
β βββ soroban.ts # Smart contract interactions
βββ utils/
βββ amm.ts # AMM math formulas (x*y=k)
For production use, you should integrate Freighter wallet instead of using secret keys:
- Install Freighter extension: https://www.freighter.app/
- Uncomment the
executeSwapWithFreighterfunction insrc/soroban/soroban.ts - Add a "Connect Wallet" button to your UI
- Replace
executeSwapcalls withexecuteSwapWithFreighter
- Stellar Documentation: https://developers.stellar.org/
- Soroban Smart Contracts: https://soroban.stellar.org/docs
- Stellar SDK: https://github.com/stellar/js-stellar-sdk
- AMM Basics: https://www.coinbase.com/learn/crypto-basics/what-is-an-automated-market-maker
When you're ready to deploy:
npm run buildThis creates an optimized build in the dist/ folder that you can deploy to:
- Vercel
- Netlify
- GitHub Pages
- Any static hosting service
## -
Contract Method Names: This project guesses common method names (
get_reserves,swap). You MUST update these to match your actual contract. -
Secret Key Security: Never use secret keys in production. Always use Freighter or another wallet solution.
-
Token Precision: The code assumes 6 decimal places (1_000_000 scaling). Adjust if your tokens use different precision.
-
Error Handling: This is an educational project. Add more robust error handling for production use.
- Check the Stellar Discord: https://discord.gg/stellardev
- Review Soroban examples: https://github.com/stellar/soroban-examples
- Read the Stellar docs: https://developers.stellar.org/
Happy swapping! π
