This service provides an API for finding the best prices for swaps between two tokens on the Uniswap protocol. It leverages the Uniswap Smart Order Router to calculate optimal routes and execute swaps efficiently.
- Get the best price for token swaps on Uniswap.
- Support for exact input (EXACT_IN) and exact output (EXACT_OUT) trades.
- Provides pre-encoded calldata for direct transaction execution.
- Includes gas and price adjustments in the response.
- Supports slippage tolerance and custom deadlines.
- Node.js (v14 or higher).
- Yarn package manager.
-
Clone the repository:
git clone <repository_url> cd <repository_folder>
-
Install dependencies:
yarn install
-
Create a
.env
file with the following variables:PORT=8000 # Port to run the server on CHAIN_ID=1 # Chain ID of the network to connect to JSON_RPC_URL="https://mainnet.infura.io/v3/<api_key>" # JSON RPC URL of the network (e.g., Infura)
-
Run the server:
- For development:
yarn dev
- For production (build first):
yarn build yarn start
- For development:
To run the service using Docker:
-
Build the Docker image:
docker build -t uniswap-router-service .
-
Run the Docker container:
docker run -d --name uniswap-router-service -p 8000:8000 --env-file .env uniswap-router-service
This endpoint calculates the optimal swap route for a trade.
Example in Postman:
{
"amount": 200,
"tradeType": 0,
"recipient": "0x23A902a839E2F688178d0cDC89F367293C8dB38e",
"currencyAmount": {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"decimals": 18,
"symbol": "WETH",
"name": "Wrapped Ether"
},
"currency": {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"decimals": 18,
"symbol": "DAI",
"name": "Dai Stablecoin"
}
}
- amount (number): The amount of currency to swap.
- tradeType (number):
0
: Exact input (EXACT_IN).1
: Exact output (EXACT_OUT).
- recipient (string): The recipient address for the output token.
- currencyAmount (object):
address
(string): Address of the input token.decimals
(number): Decimals of the input token.symbol
(string): (Optional) Symbol of the input token.name
(string): (Optional) Name of the input token.
- currency (object):
address
(string): Address of the output token.decimals
(number): Decimals of the output token.symbol
(string): (Optional) Symbol of the output token.name
(string): (Optional) Name of the output token.
{
"amount": 100,
"tradeType": 0,
"recipient": "0x23A902a839E2F688178d0cDC89F367293C8dB38e",
"currencyAmount": {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"decimals": 18,
"symbol": "WETH",
"name": "Wrapped Ether"
},
"currency": {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"decimals": 18,
"symbol": "DAI",
"name": "Dai Stablecoin"
}
}
{
"quote": {
"amount": "0.000000000000029754",
"currency": "DAI"
},
"gasDetails": {
"estimatedGasUsedUSD": "0.000000000416069983"
},
"route": [
{
"protocol": "V3",
"input": {
"amount": "0.000000000000000010",
"currency": "WETH"
},
"output": {
"amount": "0.000000000000029754",
"currency": "DAI"
}
}
],
"methodParameters": {
"calldata": "0x5ae401dc00000000000000000000000000000000000000000000000000000000678f89e1...",
"value": "0x00",
"to": "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
},
"blockNumber": null,
"hitsCachedRoute": false
}
- quote: The quote for the trade.
amount
: Amount of the output token.currency
: Symbol of the output token.
- gasDetails:
estimatedGasUsedUSD
: Estimated gas cost in USD.
- route: Details about the trade route.
protocol
: The protocol used for the swap (e.g., V3).input
: Input token details.amount
: Amount of input token.currency
: Symbol of the input token.
output
: Output token details.amount
: Amount of output token.currency
: Symbol of the output token.
- methodParameters:
calldata
: Pre-encoded calldata for executing the swap.value
: ETH value sent with the transaction (if any).to
: Address of the contract to execute the swap.
- blockNumber: Block number when the quote was generated.
- hitsCachedRoute: Indicates if a cached route was used.
Swap 1 WETH for the maximum amount of DAI, ensuring a slippage tolerance of 0.5%.
{
"amount": 1,
"tradeType": 0,
"recipient": "0xYourWalletAddress",
"currencyAmount": {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"decimals": 18,
"symbol": "WETH",
"name": "Wrapped Ether"
},
"currency": {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"decimals": 18,
"symbol": "DAI",
"name": "Dai Stablecoin"
}
}
- Node.js: Backend runtime.
- Express.js: Web framework for handling API requests.
- Uniswap Smart Order Router: For route calculation.
- Ethers.js: For handling Ethereum interactions.
- dotenv: For environment variable management.
- Cors: For cross-origin request handling.
src/app.ts
: Main application logic.src/utils.ts
: Helper functions, including token parsing and response formatting.
This project is licensed under the MIT License. See the LICENSE file for details.