How to Get Uniswap V2 Pool Data

How to Get Uniswap V2 Pool Data

mumu

mumu

Chainbase Intern

Introduction

In this tutorial, we'll be using the Chainbase DeFi dataset to retrieve the addresses of Uniswap V2 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.

Retrieve Pool Data Via Chainbase Dataset

In this section, we will learn how to retrieve pool data from the Chainbase dataset. Let's use the following query:


query SpecificTokenInfo {
  liquidityPool(id: "0xd6e3f90f531f8dc9229ddbd2e59b4a6c7a5f5de0") {
    inputTokens {
      symbol
      id
      decimals
    }
    outputToken {
      symbol
      id
      decimals
    }
  }
}

This query allows us to obtain information about a specific liquidity pool by providing its ID. The response will include details about the input and output tokens, such as their symbols, IDs, and decimals.

Get Pool Information with Pagination

Now, let's explore how to get pool information using pagination. The following query demonstrates this:

query GetPoolInfo($inputTokens: String, $skip: Int!) {
  liquidityPools(
    first: 100
    skip: $skip
    orderBy: createdTimestamp
    where: {inputTokens_: {name: $inputTokens}}
  ) {
    createdTimestamp
    deposits {
      hash
      _walletAddress
    }
  }
}

#eg.
{
  "inputTokens": "USDT",
  "skip": 2
}

This query enables us to retrieve pool information in batches of 100 pools at a time, skipping the desired number of pools using the $skip variable. The pools are ordered by their creation timestamp, and we can filter the results based on the input tokens' name.

Query Withdraw and Deposit Information

To query the withdraw and deposit information from a specific address, we can use the following query:

# Query the withdraw and deposit information from a specific address
query GetWallet($wallet: String) {
withdraws(where: {from: $wallet}) {
	blockNumber
	from
	hash
	id
	logIndex
	timestamp
	to
}
deposits(where: {from: $wallet}) {
	blockNumber
	from
	hash
	id
	logIndex
	timestamp
	to
	}
}

# eg.
{
  "wallet": "0xb862cd7c725139bbed253bbc7f06e359a89bdea7"
}

By providing the wallet address through the $wallet variable, we can obtain details about both withdrawals and deposits associated with that address.

Query Pool Transactions

Lastly, let's explore how to query pool transactions. The following query accomplishes this:

query GetPoolTx {
  liquidityPools(
    first: 5,
    orderBy: createdTimestamp, 
    orderDirection: desc) {
    createdTimestamp
    deposits {
      id
      _walletAddress
      from
      to
      inputTokenAmounts
      inputTokens {
        symbol
      }
      outputTokenAmount
      outputToken {
        symbol
      }
    }
    swaps {
      id
      _walletAddress
      from
      to
    }
    withdraws {
      id
      _walletAddress
      from
      to
    }
  }
}

This query fetches data for the five most recent liquidity pools. It includes details about deposits, swaps, and withdrawals associated with these pools.

Using the GraphQL Query UniV2Pool

The GraphQL query UniV2Pool allows us to retrieve specific information about the top Uniswap V2 liquidity pools. Let's dive into the details of the query:

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

In this query, we are using the liquidityPools field to fetch data for the first five pools. The pools are sorted based on their creation timestamp in descending order, meaning we get the most recently created pools first.

Results

"liquidityPools": [
      {
        "id": "0xd6e3f90f531f8dc9229ddbd2e59b4a6c7a5f5de0",
        "name": "Uniswap V2 Worldcoin/Wrapped Ether",
        "createdBlockNumber": "17761399",
        "inputTokens": [
          {
            "symbol": "WLD"
          },
          {
            "symbol": "WETH"
          }
        ],
        "swaps": []
      },
      {
        "id": "0x26bdfc68454a5028de4109007c8e2f6cbf0af33f",
        "name": "Uniswap V2 TAGToken/Wrapped Ether",
        "createdBlockNumber": "17761364",
        "inputTokens": [
          {
            "symbol": "TAG"
          },
          {
            "symbol": "WETH"
          }
        ],
        "swaps": []
      },
      {
        "id": "0x50a516b47e4a3da12bced268645baa1da34b25b5",
        "name": "Uniswap V2 X.com/Wrapped Ether",
        "createdBlockNumber": "17761362",
        "inputTokens": [
          {
            "symbol": "X.com"
          },
          {
            "symbol": "WETH"
          }
        ],

Conclusion

In conclusion, using our DeFi dataset to retrieve Uniswap V2 pool addresses is a powerful tool for blockchain developers. By following the provided queries and steps, you can efficiently access and analyze on-chain data. Chainbase opens up new possibilities for building DeFi applications and understanding blockchain ecosystems.

FAQs

  1. What is Chainbase? Chainbase is a platform that provides a comprehensive set of blockchain data, allowing developers to access and utilize on-chain information for their projects.
  2. Why do I need an API key for Chainbase? The API key serves as a security measure to authenticate your requests and ensure that only authorized users can access Chainbase's data.
  3. Can I use other programming languages instead of JavaScript for the examples? Yes, you can use other programming languages that are compatible with the Chainbase API, but the examples in this tutorial are demonstrated in JavaScript.
  4. How does pagination help in retrieving pool information? Pagination breaks down the data retrieval into smaller, manageable chunks. It allows you to retrieve a limited number of pools at a time, making the process more efficient.
  5. What data can I obtain from querying pool transactions? By querying pool transactions, you can get valuable information about deposits, swaps, and withdrawals associated with specific liquidity pools.

About Chainbase

Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.

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