- Dev doc: Link (https://docs.google.com/document/d/1xUjNFUcZ7WTVKNHl7sKq-96Xe4kqzSgtSRI1_u38KKQ/edit#heading=h.borgagl1k05d)
A secure token swapping application using a meta-aggregator to find the best swap routes across multiple DEXs.
- Best price discovery across multiple DEXs
- Secure token approvals
- Gas price optimization
- Automatic slippage protection
- Retry mechanism for API calls
- Comprehensive error handling
- Clone the repository and navigate to the
node-metaagggragatordirectory - Install dependencies:
npm install
- Copy
.env.exampleto.env:cp .env.example .env
- Update
.envwith your configuration:PRIVATE_KEY: Your wallet private keyRPC_URL: Your Ethereum RPC URLCHAIN_ID: Chain ID for the network (e.g., 1 for mainnet)
- Never commit your
.envfile - Keep your private keys secure
- Always verify transaction details before confirming
- Review token approvals carefully
Run the script:
npm startFollow the prompts to:
- Enter token A address (sell token)
- Enter token B address (buy token)
- Enter amount to swap
- Enter slippage percentage
- Confirm the swap details
src/
├── config.js # Configuration validation and loading
├── constants.js # Global constants and configuration
├── services/
│ ├── api/
│ │ └── metaAggregatorApi.js # API integration with retry logic
│ ├── gas/
│ │ └── gasService.js # Gas price estimation and optimization
│ ├── ethersService.js # Ethereum provider and signer management
│ ├── metaAggregatorService.js # DEX aggregator integration
│ ├── swapFlowService.js # User interaction flow
│ ├── swapService.js # Core swap logic
│ └── tokenService.js # Token operations (approvals, balances)
└── utils/
├── async.js # Async utilities (sleep, retry)
├── errors.js # Error handling and custom errors
├── format.js # Formatting utilities
└── validation.js # Input validation
-
EthersService: Manages blockchain connection and wallet integration
- Provider initialization
- Signer management
- Network connectivity checks
-
TokenService: Handles ERC20 token operations
- Token approvals
- Balance checks
- Decimals retrieval
-
SwapService: Core swap execution logic
- Transaction preparation
- Gas optimization
- Error handling
-
MetaAggregatorService: DEX aggregator integration
- Quote fetching
- Best route selection
- Price optimization
-
SwapFlowService: User interaction management
- Input collection
- Confirmation flow
- Error reporting
-
errors.js: Custom error classes and error handling
- SwapError for swap-related errors
- NetworkError for connection issues
- Detailed error messages
-
validation.js: Input validation utilities
- Address validation
- Amount validation
- Slippage validation
-
format.js: Formatting utilities
- Token amount formatting
- Display value formatting
-
async.js: Async operation utilities
- Sleep function
- Retry mechanisms
The application implements comprehensive error handling:
- Network connectivity issues
- Invalid token addresses
- Insufficient balances
- Failed transactions
- API timeouts and retries
Gas prices are optimized using:
- Dynamic gas price estimation
- Safety multipliers for gas limit
- Base fee consideration
- Priority fee adjustment
- Comprehensive input validation
- Secure token approvals
- Retry mechanism for API calls
- Error boundary implementation
- Gas price optimization
- Clean code structure
- Modular design
- Detailed logging