How to Get Uniswap V3 Pool data by Chainbase Dataset?

How to Get Uniswap V3 Pool data by Chainbase Dataset?

masterdai

masterdai

Devrel

In the present day, decentralized finance protocols have become increasingly intricate, interdependent, and reminiscent of a Lego structure. Uniswap V3, as a pivotal decentralized exchange protocol, undoubtedly plays a significant role within this interconnected system.

As a developer, you may also want to build a DApp based on the Uniswap V3 protocol, such as an on-chain options protocol like Panoptic or a DeFi aggregation analysis platform like Defillama. This means you would need to access real-time data or historical datasets from Uniswap pools, including pool addresses, total value locked (TVL), symbols, and daily transactions.

Introduction

In this tutorial, we'll be using the Chainbase DeFi dataset to retrieve the addresses of Uniswap V3 pools. Chainbase provides a wealth of blockchain data, enabling developers to access, analyze, and utilize on-chain data easily and efficiently.

Prerequisites

  1. A free account atĀ ChainbaseĀ with an API key.
  2. An IDE. Our examples are shown in JavaScript, you can useĀ VS CodeĀ as your IDE for example.
  3. A contract address of an ERC20 token as your input.

Register and Get API Key

To begin with, you'll need to register on Chainbase and obtain an API key. This key will be used to authenticate your requests to the Chainbase API.

There are two ways to retrieve Uniswap V3 pool data. I will guide you step-by-step.

Retrieve Pool Data via RPC Call

First, create a folder.

mkdir Chainbase-tutorial

cd Chainbase-toturial

Install the necessary dependencies:

npm install ethers @uniswap/v3-core dotenv

Open Visual Studio Code and create a file named 'uniswapPool.js'. Paste the following code into it.This code connects to an Ethereum provider, creates an instance of the Uniswap V3 factory contract, and calls the getPool function to retrieve the pool address for a given pair of tokens."

const { ethers } = require('ethers')
const { abi: UniswapV3Factory } = require('@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json')
require('dotenv').config()

const CHAINBASE_URL = process.env.CHAINBASE_URL

const address0 = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const address1 = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const factoryAddress = '0x1F98431c8aD98523631AE4a59f267346ea31F984'

async function main() {
  const provider = new ethers.JsonRpcProvider(CHAINBASE_URL)

  const factoryContract = new ethers.Contract(
    factoryAddress,
    UniswapV3Factory,
    provider
  )

  const poolAddress = await factoryContract.getPool(address0, address1, 500)
  console.log('poolAddress', poolAddress)

}
main()

Additionally, create a .env file and paste your API key into it.

CHAINBASE_URL=https://ethereum-mainnet.s.chainbase.online/v1/api-key

run command.

node uniswapPool.js

You will get the result.

poolAddress 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640

Retrieve Pool Data Via Chainbase Dataset

You can also get much more specific pool data through our dataset .

This sample GraphQL query retrieves data about the top 5 liquidity pools, including their identifiers, names, creation block numbers, input token symbols, total value locked in USD, cumulative swap counts, and information about the swaps made in each pool.

query UniV3Pool {
  liquidityPools(orderBy: totalValueLockedUSD, first: 5, orderDirection: desc) {
    id
    name
    createdBlockNumber
    inputTokens {
      symbol
    }
    totalValueLockedUSD
    cumulativeSwapCount
    swaps(first: 5, orderBy: timestamp, orderDirection: desc) {
      hash
      amountIn
      amountOut
    }
  }
}

Here is the output.

{
"data": {
"liquidityPools": [
{
"id": "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
"name": "Uniswap V3 USD Coin/Wrapped Ether 0.05%",
"createdBlockNumber": "12376729",
"inputTokens": [
{
"symbol": "USDC"
},
{
"symbol": "WETH"
}
],
"totalValueLockedUSD": "222256778.1147989581618490618581225",
"cumulativeSwapCount": 4934671,
"swaps": [
{
"hash": "0xe4db8619ee5c3213fe971b3973d1d8cf0f9df77528a8b72837514aa2cf9193e2",
"amountIn": "72403717",
"amountOut": "39925865656714033"
},
{
"hash": "0x575d7d1977574f883a1d450303b7cce5dfaeae6b6c6da0c560bb677a83420dfe",
"amountIn": "135657763581",
"amountOut": "74809276525566713204"
},

Conclusion

Chainbase provides a powerful tool for accessing and utilizing on-chain data. With the Chainbase DeFi dataset, you can swiftly and effortlessly retrieve the addresses of Uniswap V3 pools, enabling you to build more powerful and efficient DeFi applications.

Remember, this is a general framework. You can also use GraphQL to customize the data you want, such as retrieving all swap data within specific pools like USDC/ETH.

Happy coding!

Want to learn more about Chainbase?

Visit our websiteĀ chainbase.com Sign up for aĀ free account, and Check out ourĀ documentation.

Websiteļ½œBlogļ½œTwitterļ½œDiscordļ½œLink3