- Introduction
- Features
- Technology Stack
- Getting Started
- Usage
- Project Structure
- Contributing
- License
- Running with Docker
Welcome to the optibridge project! This backend service is designed to facilitate optimal cross-chain token bridging by calculating the most efficient routes based on user requests. Leveraging multiple blockchain networks, the service ensures minimal fees and optimal transaction times, providing a seamless bridging experience for users.
- Optimal Route Calculation: Determines the best possible paths for token bridging across supported blockchain networks.
- Dynamic Fee Management: Integrates with external APIs to fetch real-time bridge fees and applies fallback mechanisms when necessary.
- Caching with Redis: Implements caching strategies to enhance performance and reduce redundant API calls.
- Robust Error Handling: Comprehensive error management to handle various failure scenarios gracefully.
- Test Mode: Allows simulation of different blockchain balances for testing purposes without interacting with actual networks.
- Scalable Architecture: Built with scalability in mind, accommodating the addition of more blockchain networks with ease.
- Language: TypeScript
- Runtime: Bun
- Framework: Elysia
- Caching: Redis
- Blockchain Interaction: Ethers.js
- Validation: Zod
- HTTP Client: Axios
Ensure you have the following installed on your system:
-
Clone the Repository
git clone https://github.com/takshakmudgal/optibridge.git cd optibridge
-
Install Dependencies
Using Bun:
bun install
-
Environment Variables
Create a
.env
file in the root directory and populate it with the required environment variables:SOCKET_API_KEY=your_socket_api_key REDIS_URL=redis://localhost:6379 PORT=3000
SOCKET_API_KEY
: Your API key for the Socket API service.REDIS_URL
: Connection URL for your Redis instance.PORT
: Port on which the application will run (default is 3000).
-
Supported Chains Configuration
The application supports multiple blockchain networks. You can configure supported chains in the
src/config/chains.ts
file. Each chain configuration includes:name
: Human-readable name of the chain.chainId
: Unique identifier for the blockchain.nativeToken
: The native cryptocurrency of the chain.usdcAddress
: Contract address for USDC on the chain.rpcUrl
: RPC endpoint for interacting with the blockchain.
-
Bridge Fees Configuration
Bridge fees between chains are defined in
src/config/bridgeFees.ts
. You can adjust the fees as needed:export const BRIDGE_FEES: Record<string, Record<string, number>> = { arbitrum: { polygon: 1, }, base: { polygon: 0.5, }, gnosis: { polygon: 0.1, }, blast: { polygon: 0.2, }, };
Start the development server using Bun:
bun run dev
The server will start on the specified port (default: 3000). You should see a console message indicating that Elysia is running:
π¦ Elysia is running at localhost:3000
Calculates the optimal bridging routes based on the user's request.
-
Request Body
{ "targetChain": "string", // The blockchain network to bridge to (e.g., "polygon") "amount": number, // The amount of tokens to bridge "tokenAddress": "string", // The contract address of the token to bridge "userAddress": "string" // The user's wallet address initiating the bridge }
- Validation Rules:
targetChain
: Must be a supported chain.amount
: Must be a positive number.tokenAddress
&userAddress
: Must be valid Ethereum addresses (42 characters, starting with0x
).
- Validation Rules:
-
Response
-
Success
{ "success": true, "data": { "routes": [ { "sourceChain": "string", "amount": number, "fee": number, "estimatedTime": number, "protocol": "string", "gasToken": "string", "sourceBalance": number } ], "totalFee": number, "totalAmount": number, "estimatedTotalTime": number, "availableBalance": number, "requiredAmount": number, "insufficientFunds": false, "noValidRoutes": false, "bridgeRoutes": [ { "sourceChain": "string", "amount": number, "fee": number, "estimatedTime": number, "protocol": "string", "gasToken": "string" } ], "targetChain": "string", "shortfall": number }, "error": null }
-
Failure
{ "success": false, "error": "Error message detailing the issue", "data": { "routes": [], "totalFee": 0, "totalAmount": 0, "estimatedTotalTime": 0, "availableBalance": 0, "requiredAmount": number, "insufficientFunds": true, "noValidRoutes": true, "bridgeRoutes": [], "targetChain": "string", "shortfall": number } }
-
-
Example Request
curl -X POST http://localhost:3000/api/bridge/routes \ -H "Content-Type: application/json" \ -d '{ "targetChain": "arbitrum", "amount": 100, "tokenAddress": "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8", "userAddress": "0xYourEthereumAddressHere" }'
optibridge/
βββ src/
β βββ config/
β β βββ bridgeFees.ts
β β βββ chains.ts
β β βββ env.ts
β β βββ redis.ts
β βββ controllers/
β β βββ bridgeController.ts
β βββ services/
β β βββ routeCalculator.ts
β β βββ socketApi.ts
β βββ types/
β β βββ index.ts
β β βββ socket.ts
β βββ utils/
β β βββ errors.ts
β β βββ validation.ts
β βββ server.ts
βββ .gitignore
βββ package.json
βββ tsconfig.json
βββ README.md
- src/config/: Contains configuration files for environment variables, Redis, supported chains, and bridge fees.
- src/controllers/: Handles incoming API requests and orchestrates responses.
- src/services/: Contains business logic for route calculation and external API interactions.
- src/types/: Defines TypeScript interfaces and types used throughout the application.
- src/utils/: Utility functions and custom error classes.
- src/server.ts: Entry point of the application, sets up the Elysia server and routes.
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeatureName
-
Commit Your Changes
git commit -m "Add your message here"
-
Push to the Branch
git push origin feature/YourFeatureName
-
Open a Pull Request
Please ensure your code adheres to the project's coding standards and passes all existing tests.
To run optibridge using Docker, follow these steps:
- Docker installed on your system.
- Docker Compose installed.
-
Clone the Repository
git clone https://github.com/takshakmudgal/optibridge.git cd optibridge
-
Create a
.env
FileCreate a
.env
file in the root directory and add your environment variables:SOCKET_API_KEY=your_socket_api_key PORT=3000
-
Build and Run with Docker Compose
docker-compose up --build
This command will build the Docker image and start both the application and Redis services. The application will be accessible at http://localhost:3000.
-
Stopping the Services
To stop the services, press
Ctrl + C
in the terminal where Docker Compose is running, then execute:docker-compose down
- The application code is mounted into the Docker container, allowing for live-reloading during development.
- Redis data is persisted using Docker volumes to prevent data loss between restarts.
This project is licensed under the MIT License. See the LICENSE file for details.