This feature implements a price impact estimation endpoint for the TradeFlow API. It calculates the potential slippage a user might experience when trading large amounts of tokens.
- URL:
POST /api/v1/swap/estimate - Description: Estimates price impact based on trade amount
{
"amountIn": 5000
}{
"amountIn": 5000,
"priceImpact": "1.33%",
"timestamp": "2024-01-15T10:30:00.000Z"
}- amountIn < 1000: Returns 0.1% price impact
- amountIn > 10000: Returns 2.5% price impact
- 1000 ≤ amountIn ≤ 10000: Linear interpolation between 0.1% and 2.5%
- Invalid (negative or zero) amounts return 400 Bad Request
src/swap/swap.controller.ts- REST controller with Swagger documentationsrc/swap/swap.service.ts- Business logic for price impact calculationsrc/swap/swap.module.ts- NestJS module configurationtest-swap-endpoint.js- Test script for endpoint validation
src/app.module.ts- Added SwapModule to imports
- Start the server:
npm run start:dev - Run the test script:
node test-swap-endpoint.js
- Small trade (500): Expected 0.1%
- Large trade (15000): Expected 2.5%
- Medium trade (5500): Expected ~1.33% (interpolated)
- Invalid input (-100): Expected 400 error
The endpoint is fully documented with Swagger annotations and available at /api/docs when the server is running.
- Real pool size integration
- Dynamic price impact calculation based on liquidity
- Multi-token support
- Historical price impact tracking