Skip to main content

How to get Uniswap V2 pair address

Prerequisites​

Before getting started, make sure you have the following ready:

  • Node v.14+ or Python
  • NPM/Yarn or Pip

Step 1: Setup Moralis​

First register your Moralis account and get your Moralis API Key.

Once you have your Moralis API Key, install the Moralis SDK in your project.

npm install moralis @moralisweb3/common-evm-utils

Step 2: Get Pair Address of A Uniswap V2 Liquidity Pool​

In order to get pair address of a liquidity, Moralis provides you a getPairAddress endpoint to do so.

Here you'll need three parameters: token0, token1 and address.

Once you've obtained all three parameters, you can copy the following code:

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});

const chain = EvmChain.ETHEREUM;

// token 0 address, e.g. WETH token address
const token0Address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

// token 1 address, e.g. LINK token address
const token1Address = "0x514910771AF9Ca656af840dff83E8264EcF986CA";

const response = await Moralis.EvmApi.defi.getPairAddress({
token0Address,
token1Address,
chain,
});

console.log(response.toJSON());
};

runApp();

Step 3: Run the script​

To run the script, enter the following command:

node index.js

In your terminal, you should see the following JSON response:

{
"token0": {
"address": "0x514910771af9ca656af840dff83e8264ecf986ca",
"name": "Chain Link",
"symbol": "LINK",
"decimals": "18",
"logo": "https://cdn.moralis.io/eth/0x514910771af9ca656af840dff83e8264ecf986ca.webp",
"logo_hash": "fd74ea1227adb458733847c09aab05d89c35c14b640c5ee1e0a8bffa79193eb4",
"thumbnail": "https://cdn.moralis.io/eth/0x514910771af9ca656af840dff83e8264ecf986ca_thumb.webp",
"block_number": null,
"validated": null,
"created_at": "2022-01-20T10:39:55.818Z"
},
"token1": {
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"name": "Wrapped Ether",
"symbol": "WETH",
"decimals": "18",
"logo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp",
"logo_hash": "0a7fc292596820fe066ce8ce3fd6e2ad9d479c2993f905e410ef74f2062a83ec",
"thumbnail": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp",
"block_number": null,
"validated": null,
"created_at": "2022-01-20T10:39:55.818Z"
},
"pairAddress": "0xa2107fa5b38d9bbd2c461d6edf11b11a50f6b974"
}

Congratulations 🥳 You just get the pair address of a Uniswap V2 liquidity pool with just a few lines of code using the Moralis DeFi API!

Youtube Video​

API Reference​

If you want to know more details on the endpoint and optional parameters, check out:

Support​

If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.