This application finds the most efficient routes for bridging tokens (USDC) across different chains, optimizing for gas fees and bridging costs.
- Finds optimal bridging routes based on fees and time
- Supports multiple source chains (Polygon, Arbitrum, Base, Gnosis, Blast)
- Takes into account local balances on target chain
- Provides multiple route options sorted by efficiency
- Caches responses for better performance
- Redis server running
- Socket/Bungee API Key
- Clone the repository:
git clone <repository-url>
cd route-optimization
- Install dependencies:
npx bun install
- Set up environment variables:
cp .env.example .env
Edit .env
with your configuration:
# Server Configuration
PORT=3001
# Bungee/Socket API Configuration
BUNGEE_API_KEY=your_api_key_here
# Redis Configuration
REDIS_URL=redis://localhost:6379
CACHE_TTL=30
- Start Redis server:
# Using Docker
docker run --name redis -p 6379:6379 -d redis
# Or if you have Redis installed locally
redis-server
- Run the application:
# Development mode with hot reload
npx bun dev
# Production mode
npx bun start
The server will start at http://localhost:3001
(or the port specified in your .env file).
Health check endpoint
Find optimal bridging routes
Request body:
{
"targetChain": "137",
"amount": "100",
"tokenAddress": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
"userAddress": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
}
Response:
{
"status": "success",
"data": {
"success": true,
"routes": [
{
"steps": [
{
"fromChain": "137",
"toChain": "137",
"amount": "50",
"fee": "0",
"protocol": "local"
},
{
"fromChain": "Gnosis",
"toChain": "137",
"amount": "25",
"fee": "0.1",
"protocol": "socket"
}
],
"totalFee": "0.3",
"totalAmount": "100",
"isOptimal": true,
"explanation": "Best route using local balance + bridging"
}
],
"targetChain": "137",
"requestedAmount": "100"
}
}
Run the test scenarios:
npx bun run tests/scenarios.ts
Note: Currently using mock balances for testing purposes. To implement real balance fetching:
- Update
BalanceService.getAllBalances()
to use actual RPC calls or indexers - Remove the mock data in
getAllBalances()
- Implement proper balance fetching for each chain
bridge-route-optimizer/
├── src/
│ ├── config/
│ │ └── constants.ts # Chain configurations
│ ├── services/
│ │ ├── BalanceService.ts # Balance fetching (currently mocked)
│ │ ├── BungeeService.ts # Socket/Bungee API integration
│ │ ├── CacheService.ts # Redis caching
│ │ └── RouteOptimizer.ts # Route optimization logic
│ ├── controllers/
│ │ └── RouteController.ts
│ ├── types/
│ │ └── index.ts
│ └── index.ts
├── tests/
│ └── scenarios.ts
├── .env
└── package.json
- Implement real balance fetching across chains
- Add more test scenarios
- Add route success probability
- Add gas estimates
- Add monitoring and metrics
- Add rate limiting
- Add API documentation (Swagger)
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'feat: add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request