To create a Node.js app that can trade using Uniswap V3 SDK, follow the steps below:
- Install necessary tools
Ensure that you have Node.js installed on your machine. If not, you can download it from the official site:
https://nodejs.org/
- Set up the project directory
Create a new directory for your Uniswap trading app and navigate to it:
mkdir uniswap-v3-app
cd uniswap-v3-app
- Initialize NPM
Run the following command to create a package.json file:
npm init -y
- Install required packages
Install the required packages using npm:
npm install ethers @uniswap/sdk-core @uniswap/v3-sdk dotenv
- Set up environment variables
Create a .env file in your project directory for storing your Infura API key and your Ethereum private key:
touch .env
Open the .env file and add the following lines:
INFURA_API_KEY=your_infura_api_key_here
PRIVATE_KEY=your_ethereum_private_key_here
- Create main JavaScript file
Create a new file called 'trade.js' in your project directory:
touch trade.js
- Add code to trade.js
Open the trade.js file and paste the following code:
require('dotenv').config();
const { ethers } = require('ethers');
const { ChainId, Token, TokenAmount, Pair } = require('@uniswap/sdk-core');
const { SwapRouter, RouteV2, Trade } = require('@uniswap/v3-sdk');
const infuraApiKey = process.env.INFURA_API_KEY;
const privateKey = process.env.PRIVATE_KEY;
const provider = new ethers.providers.InfuraProvider(ChainId.MAINNET, infuraApiKey);
const wallet = new ethers.Wallet(privateKey, provider);
async function trade() {
// Define the tokens
const tokenIn = new Token(ChainId.MAINNET, '0x6B175474E89094C44D_a1976B3156ccccD', 18); // Replace this -- ETH
const tokenOut = new Token(ChainId.MAINNET, '0xC02aaA39abc/2228D_Dab09f202b507A1e', 6); // Replace this -- DAI
// Fetch the pool
const pool = await Pair.fetchData(tokenIn, tokenOut, provider);
// Define the trade details
const tradeAmount = 1_000_000; // Replace with the desired amount in the smallest unit
const amountIn = new TokenAmount(tokenIn, tradeAmount);
const route = new RouteV2([pool.liquidityProvider], tokenIn, tokenOut);
const trade = await Trade.fromRoute(route, amountIn, Trade.Type.EXACT_INPUT);
// Prepare transaction
const swapRouter = new ethers.Contract(SwapRouter.ADDRESS, SwapRouter.ABI, wallet);
const deadline = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
const slippage = 50; // Replace with desired slippage tolerance in basis points (0.50% in this example)
const amountOutMin = trade.minimumAmountOut(slippage / 10_000).toFixed();
// Execute trade
const approval = await tokenIn.approve(swapRouter.address, ethers.constants.MaxUint256);
await approval.wait();
const swapTx = await swapRouter.exactInputSingle(tokenIn.address, tokenOut.address, amountIn.quotient, amountOutMin, deadline);
const receipt = await swapTx.wait();
console.log('Transaction successful:', receipt);
}
trade().catch(console.error);
- Configure the tokens, trading amount, and slippage
Replace the token addresses (in 7th step) with the Ethereum addresses of your preferred input and output tokens, and replace the 'tradeAmount' and 'slippage' numbers with the desired trading amount and slippage tolerance accordingly.
- Run the code
Start trading by executing the 'trade.js' file:
node trade.js
You should see a transaction receipt printed out on successful trade execution.