Find Liquidity Addresses for a Particular Token Pair on Uniswap and Sushiswap by Code
Aghasi Gasparyan
Co-founder at Galileo Figaro. Solidity & Web3 - Smart Contract Developer - galileofigaro.io
Hello there, coders! Are you interested in building your own DAPP that can find liquidity addresses for a particular token pair on Uniswap and Sushiswap? In this step-by-step guide, I'll show you how to create a full-stack DAPP that can do just that! But first, let's talk about why it's so important to be able to detect liquidity addresses by code instead of manually.
Why Detecting Liquidity Addresses by Code is Important
In the DeFi world, there are many opportunities for arbitrage trading. Arbitrage trading is when you buy a cryptocurrency on one exchange where it is priced lower and sell it on another exchange where it is priced higher, thereby making a profit. However, to be successful in arbitrage trading, you need to be able to act quickly. If you're manually searching for liquidity addresses, you may not be able to find them in time to take advantage of a good arbitrage opportunity. This is where coding comes in.
When you write code to detect liquidity addresses, you can do it much more quickly and accurately than you could manually. This means that you'll be able to find and take advantage of arbitrage opportunities much faster. Plus, coding is a valuable skill to have in today's world, so it's a great opportunity to learn something new and useful.
Now, let's get into the step-by-step process of building a full-stack DAPP that can find liquidity addresses for a particular token pair on Uniswap and Sushiswap.
Part 1: Setting up the backend
Step 1. Open your terminal and navigate to the directory where you want to create the backend.
Step 2. Run the following command to create a new Node.js project:
mkdir backend
cd backend
yarn init -y
Step 3. Install the necessary dependencies:
yarn add @uniswap/v2-periphery @uniswap/v3-core express ethers dotenv cors
Step 4. Create a new file called?index.js?in the root of the project with the following code:
const express = require('express')
const { ethers } = require('ethers')
const cors = require('cors');
require('dotenv').config();
const app = express();
app.use(cors());
app.use(express.json());
const factoryArtifact = require('@uniswap/v2-core/build/UniswapV2Factory.json')
const provider = new ethers.WebSocketProvider(`wss://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`)
const SUSHI_FACTORY = '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac'
const UNI_FACTORY = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const WETH = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const sushiFactory = new ethers.Contract(SUSHI_FACTORY, factoryArtifact.abi, provider)
const uniFactory = new ethers.Contract(UNI_FACTORY, factoryArtifact.abi, provider)
app.get('/detect-pools', async (req, res) => {
? ? const sushiPair = await sushiFactory.getPair(USDC, WETH)
? ? const uniPair = await uniFactory.getPair(USDC, WETH)
? ? console.log('sushiPair', sushiPair)
? ? console.log('uniPair', uniPair)
? ? res.send({"uniPair": uniPair, "sushiPair": sushiPair});
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
? console.log(`Server running on port ${port}`);
});;
Step 5. Create a new file called?.env?in the root of the project with the following content:
ALCHEMY_API_KEY=<your-alchemy-api-key>
You can find <your-alchemy-api-key> for Ethereum Mainnet from https://alchemy.com/
Step 6. Start the server by running the following command:
node index.js
Your backend is now ready to serve requests on https://localhost:5000/detect-pools address.
A Step-by-Step explanation?of backend code.
This code is a simple Node.js server that uses the Web3 ethers.js library to interact with the Ethereum blockchain. Here's what each line does:
const { ethers } = require('ethers')
This line imports the ethers library from the ethers npm package. ethers is a popular library for interacting with Ethereum, providing a simple API for working with Ethereum wallets, contracts, and other Ethereum-related functionality.
领英推荐
const factoryArtifact = require('@uniswap/v2-core/build/UniswapV2Factory.json')
This line imports a JSON file that contains the ABI (Application Binary Interface) of the Uniswap V2 factory contract. The ABI is a description of the methods and properties of a smart contract, and is used by ethers to interact with the contract.
const provider = new ethers.WebSocketProvider(`wss://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`)
This line creates a new WebSocketProvider object, which is used to connect to an Ethereum node over a WebSocket connection. In this case, the provider is connecting to the Alchemy Ethereum node, which is specified using the ALCHEMY_API_KEY environment variable. ethers provides a number of different provider classes for connecting to different types of Ethereum nodes.
const sushiFactory = new ethers.Contract(SUSHI_FACTORY, factoryArtifact.abi, provider
const uniFactory = new ethers.Contract(UNI_FACTORY, factoryArtifact.abi, provider))
These lines create instances of the Uniswap V2 factory contract using the ethers.Contract class. The Contract class takes three arguments: the contract address, the ABI of the contract, and the provider to use for interacting with the contract. The factoryArtifact.abi argument is the ABI imported earlier from the Uniswap V2 factory contract.
app.get('/detect-pools', async (req, res) =>
? ? const sushiPair = await sushiFactory.getPair(USDC, WETH)
? ? const uniPair = await uniFactory.getPair(USDC, WETH)
? ? console.log('sushiPair', sushiPair)
? ? console.log('uniPair', uniPair)
? ? res.send({"uniPair": uniPair, "sushiPair": sushiPair});
});
This code sets up an HTTP endpoint at /detect-pools that returns the addresses of the Uniswap V2 liquidity pool contracts for the USDC/WETH pair on both SushiSwap and Uniswap. The getPair method is called on both the sushiFactory and uniFactory instances, passing in the USDC and WETH contract addresses as arguments. The addresses of the liquidity pool contracts are returned, and are logged to the console and sent back in the HTTP response as JSON.
Part 2: Setting up the frontend
Navigate to root directory where you have backend folder.
Step 7.Create a new React app
npx create-react-app frontend
Step 8. Install dependencies
Next, let's install the necessary dependencies for our frontend. Run the following command in your terminal to install?axios:
cd frontend
yarn add axios
Step 9. Update src/App.js
Now, let's update the?src/App.js?file in our React app. Replace the existing code with the following:
import React, { useState, useEffect } from 'react'
import axios from 'axios';
function App() {
? const [uniswapPool, setUniswapPool] = useState('');
? const [sushiswapPool, setSushiswapPool] = useState('');
? useEffect(() => {
? ? async function fetchData() {
? ? ? const result = await axios('https://localhost:5000/detect-pools');
? ? ? setUniswapPool(result.data.uniPair);
? ? ? setSushiswapPool(result.data.sushiPair);
? ? }
? ? fetchData();
? }, []);
? return (
? ? <div style={{textAlign:'center'}}>
? ? ? <div>
? ? ? ? <h1 style={{textAlign: "center", fontSize: "3em"}}>USDC/WETH on Uniswap</h1>
? ? ? </div>
? ? ? <div>
? ? ? ? <a href={`https://etherscan.io/address/${uniswapPool}`} target="_blank">Check on Etherscan</a>
? ? ? </div>
? ? ? <div>
? ? ? ? <h1 style={{textAlign: "center", fontSize: "3em"}}>USDC/WETH on Sushiswap</h1>
? ? ? </div>
? ? ? <div>
? ? ? ? <a href={`https://etherscan.io/address/${sushiswapPool}`} target="_blank">Check on Etherscan</a>
? ? ? </div>
? ? </div>
? );
}
export default App;
This code defines a functional component in React called App, which makes use of the useState and useEffect hooks provided by React. The component fetches data from the https://localhost:5000/detect-pools endpoint using Axios library. The fetched data is used to update the state of uniswapPool and sushiswapPool variables which are then used to display links to check the addresses on Etherscan for the respective pools. Finally, the component returns JSX, which renders the links for both pools on the web page.
Step 10: Start the app
Now that we've updated?App.js, let's start the app by running the following command in your terminal:
yarn start
This will start the app and open it in your default browser on this address https://localhost:3000 address.
Congratulations, coders! You've just built a full-stack DAPP that can find liquidity addresses for a particular token pair on Uniswap and Sushiswap. Not only have you learned a valuable coding skill, but you've also gained insight into the world of DeFi and arbitrage trading.
If you want to learn more about coding and DeFi, I encourage you to check out my other articles on my LinkedIn account. You'll find a wealth of information on topics like blockchain technology, smart contracts, and cryptocurrency. By staying up-to-date with the latest trends in the industry, you'll be better equipped to build innovative and successful projects.
Keep coding and keep learning! The world is full of possibilities, and with your skills, you can make a difference.