Cover Image for Uniswap API: Get Pools Data, Tokens and Create Charts

Uniswap API: Get Pools Data, Tokens and Create Charts

Blockchain

In the last article, we showed you how to get Uniswap trades using simple rest APIs. Today, we will show how you get the latest Uniswap data using a single GraphQL apis.

Before we move forward, let’s understand what Uniswap pair listing is?

Anyone can create a pool of two tokens on Uniswap. While creating, they need to provide initial liquidity (whatever they want) to both tokens for price discovery. This pool is an ERC20 token smart contract, which was created by the Uniswap Factory contract.

Don’t worry; we will show you an example of everything as we will progress.

For now, let’s get the newly created pools listing from the Uniswap.

Note: We are Uniswap V2 in this article.

Uniswap Pools APIs

To get the newly creates Uniswap pools, we need to use the following GraphQL API. Here —  https://graphql.bitquery.io/. This is also our endpoint for GraphQL APIs.

{
    ethereum {
        arguments(smartContractAddress: {is: "0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f"}, smartContractEvent: {is: "PairCreated"}, options: {desc: "block.height", limit: 3}) {
            block {
                height
            }
            argument {
                name
            }
            reference {
                address
            }
        }
    }
}

Result

{
    "data": {
        "ethereum": {
            "arguments": [
                {
                    "block": {
                        "height": 10836664
                    },
                    "argument": {
                        "name": "token0"
                    },
                    "reference": {
                        "address": "0x930ed81ad809603baf727117385d01f04354612e"
                    }
                },
                {
                    "block": {
                        "height": 10836664
                    },
                    "argument": {
                        "name": "pair"
                    },
                    "reference": {
                        "address": "0x7bc3a77b1b8daa4bf4b38c710119b932c5b27925"
                    }
                },
                {
                    "block": {
                        "height": 10836664
                    },
                    "argument": {
                        "name": "token1"
                    },
                    "reference": {
                        "address": "0xdf5e0e81dff6faf3a7e52ba697820c5e32d806a8"
                    }
                }
            ]
        }
    }
}

Understing the query and the result

In this query, we are pulling data from Uniswap events. Whenever a pool is created Uniswap emits an event name ‘PairCreated’ with 3 main parameters.

  • token0 — A ERC20 token
  • token1 — Another ERC20 token
  • pair — Newly created Pair (aka pool) token address

Pair Created Event

We will be using ‘PairCreated’ events in our query to get the latest Uniswap pairs/pools.

If you notice, we are using Uniswap Factory contact address (0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f) in our query and a limit 3 to get the latest 3 arguments.

As you can see in the result, these arguments are token0 address, token1 address, and the newly created pair address.

Learning about the Tokens

To know the token details, just run the following query with the token address.

For example, if we want to know the details of token0 from the above result.

{
    ethereum {
        address(address: {is: "0x930ed81ad809603baf727117385d01f04354612e"}) {
            annotation
            address
            smartContract {
                contractType
                currency {
                    symbol
                    name
                    decimals
                    tokenType
                }
            }
            balance
        }
    }
}

Result

{
    "data": {
        "ethereum": {
            "address": [
                {
                    "annotation": "",
                    "address": "0x930ed81ad809603baf727117385d01f04354612e",
                    "smartContract": {
                        "contractType": "Token",
                        "currency": {
                            "symbol": "SOLARITE",
                            "name": "SOLARITE",
                            "decimals": 18,
                            "tokenType": "ERC20"
                        }
                    },
                    "balance": 0
                }
            ]
        }
    }
}

Learning about the Pair(Pool)

Now let’s look at some detail of the newly created pool.

APIs to get Initial Liquidity of the Pools

As we mentioned above, while creating a pool, you need to provide liquidity for both tokens; therefore, if we want to know the initial amount for both tokens in the pool, run the following query.

Here we are getting 1st two transactions received by the pool (pair).

Note: 0x7bc3a77b1b8daa4bf4b38c710119b932c5b27925 is pool (pair) address from the result above.

{
    ethereum {
        transfers(options: {asc: "block.timestamp.time", limit: 2}, amount: {gt: 0}, receiver: {is: "0x7bc3a77b1b8daa4bf4b38c710119b932c5b27925"}) {
            block {
                timestamp {
                    time(format: "%Y-%m-%d %H:%M:%S")
                }
                height
            }
            address: sender {
                address
                annotation
            }
            currency {
                address
                symbol
            }
            amount
            transaction {
                hash
            }
            external
        }
    }
}

Result

{
    "data": {
        "ethereum": {
            "transfers": [
                {
                    "block": {
                        "timestamp": {
                        "time": "2020-09-10 22:10:16"
                        },
                        "height": 10836664
                    },
                    "address": {
                        "address": "0x37d50885b44500a2eacab7c93dd349590600f05f",
                        "annotation": null
                    },
                    "currency": {
                        "address": "0x930ed81ad809603baf727117385d01f04354612e",
                        "symbol": "SOLARITE"
                    },
                    "amount": 0.08,
                    "transaction": {
                        "hash": "0xf69e4560ae36fd2948cfe5b6a5d22654b46307fe0ef04022bc68c8a3ee55f957"
                    },
                    "external": true
                },
                {
                    "block": {
                        "timestamp": {
                        "time": "2020-09-10 22:10:16"
                        },
                        "height": 10836664
                    },
                    "address": {
                        "address": "0x37d50885b44500a2eacab7c93dd349590600f05f",
                        "annotation": null
                    },
                    "currency": {
                        "address": "0xdf5e0e81dff6faf3a7e52ba697820c5e32d806a8",
                        "symbol": "yDAI+yUSDC+yUSDT+yTUSD"
                    },
                    "amount": 200,
                    "transaction": {
                        "hash": "0xf69e4560ae36fd2948cfe5b6a5d22654b46307fe0ef04022bc68c8a3ee55f957"
                    },
                    "external": true
                }
            ]
        }
    }
}

Getting Total Balance of different tokens in the Pool

Let’s say now you want to check the balance of both tokens in the Uniswap pool. You can use the below query for that.

By default, every pool/pair currency name is “UNI-V2”. Remember, we mentioned that pool itself is an ERC20 token.


{
    ethereum {
        address(address: {is: "0x7bc3a77b1b8daa4bf4b38c710119b932c5b27925"}) {
            balances {
                currency {
                    symbol
                }
                value
            }
        }
    }
}

Results

{
    "data": {
        "ethereum": {
            "address": [
                {
                    "balances": [
                        {
                            "currency": {
                                "symbol": "UNI-V2"
                            },
                            "value": 0
                        },
                        {
                            "currency": {
                                "symbol": "SOLARITE"
                            },
                            "value": 619.42286725
                        },
                        {
                            "currency": {
                                "symbol": "yDAI+yUSDC+yUSDT+yTUSD"
                            },
                            "value": 199915.11344483
                        }
                    ]
                }
            ]
        }
    }
}

Wrapping Up

Our GraphQL APIs are highly flexible and performative. You can write GraphQL queries to get any data from any Ethereum protocol. In future articles, we will show how to get data for different DeFi protocol. If you have any questions, ping us on our Telegram channel.

Also Read:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Coinpath® APIs provide blockchain money flow analysis for more than 24 blockchains. With Coinpath’s APIs, you can monitor blockchain transactions, investigate crypto crimes such as bitcoin money laundering, and create crypto forensics tools. Read this to get started with Coinpath®.

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.

  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at sales@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Subscribe to our newsletter

Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.