A Node.js implementation of an Automated Market Maker (AMM) with constant product formula, similar to Uniswap v1. This implementation demonstrates the core concepts of decentralized exchanges including liquidity pools, price impact, and automated price discovery.
- Constant Product AMM (x * y = k)
- Real-time price updates based on pool reserves
- Slippage calculation and price impact analysis
- 0.3% trading fee implementation
- Support for ETH/USDC trading pair
- Detailed pool statistics and pricing information
- ETH Reserve: 500 ETH
- USDC Reserve: 1,000,000 USDC (1000 USDC accounting for 6 decimals)
- Initial K: ETH_reserve * USDC_reserve
The AMM uses the constant product formula:
x * y = k
where:
- x = ETH reserve
- y = USDC reserve
- k = constant product
Spot price is calculated as:
price = USDC_reserve / ETH_reserve
Effective price including slippage for a trade is calculated using:
effectivePrice = outputAmount / inputAmount
- 0.3% fee on all trades
- Fee is included in the constant product formula calculation
GET /price/eth
Returns:
- Current spot price
- Price impact for different trade sizes
- Pool statistics
Example Response:
{
"spotPrice": 2000,
"tradeImpact": [
{
"tradeSize": 0.1,
"effectivePrice": 1995.2,
"priceImpact": "0.24%"
},
{
"tradeSize": 1,
"effectivePrice": 1960.5,
"priceImpact": "1.97%"
}
],
"poolStats": {
"ethReserve": 500,
"usdcReserve": 1000,
"k": 500000
}
}
POST /swap/eth-to-usdc
Body:
{
"ethAmount": 1.0
}
POST /swap/usdc-to-eth
Body:
{
"usdcAmount": 1000.0
}
GET /pool/stats
Returns current pool reserves, constant k, and current ETH price.
- Clone the repository
- Install dependencies:
npm install
- Start the server:
node index.js
The server will start on http://localhost:3000
Here are some curl commands to test the API:
# Check ETH price
curl http://localhost:3000/price/eth
# Swap 1 ETH for USDC
curl -X POST http://localhost:3000/swap/eth-to-usdc \
-H "Content-Type: application/json" \
-d '{"ethAmount": 1}'
# Swap 1000 USDC for ETH
curl -X POST http://localhost:3000/swap/usdc-to-eth \
-H "Content-Type: application/json" \
-d '{"usdcAmount": 1000}'
Price impact increases with trade size relative to pool reserves. For example:
- Small trades (< 0.1% of pool) : minimal impact
- Medium trades (0.1-1% of pool) : noticeable impact
- Large trades (>1% of pool) : significant impact
The formula for calculating output amount with fee is:
amountOut = (amountIn * 0.997 * reserveOut) / (reserveIn + amountIn * 0.997)
- Express.js
- Body-parser (included in Express)
- Constant K verification after each trade
- Input validation for swap amounts
- Decimal precision handling for USDC (6 decimals)
- Error handling for invalid inputs and failed trades
- Single trading pair (ETH/USDC)
- No persistence (state is lost on server restart) || can be improved by making a db
- No authentication/authorization
- No support for limit orders
- Simplified fee model
- Add multiple trading pairs
- Implement persistent storage
- Add authentication and rate limiting
- Support for limit orders
- More sophisticated fee models
- Price oracle integration
- Multi-hop trades
- Flash loan protection