Cover Image for Ethereum DEX GraphQL APIs with Examples

Ethereum DEX GraphQL APIs with Examples

Product
Tutorial

We are now supporting Ethereum DEX trading data on our GraphQL APIs. You can get DEX trades, OHLC, trading volume, and other DEX related data for more than 18 Ethereum DEX protocols such as Uniswap, Balancer, Kyber, 0x, IDEX, etc.

Before we show some examples, here are a few important things to note: 

  • GraphQL is an API query language, read this create your first GraphQL query.
  • Read this to learn how to call GraphQL queries as APIs programmatically.
  • GraphQL queries are highly flexible; therefore, you can change the following queries based on your needs or create a new one by following our GrpahQL schema (Available on our playground.)
  • You can test the following GraphQL queries on our Playground.
  • Our GraphQL endpoint is — https://graphql.bitquery.io/
  • Our GraphQL APIs support more than 20 blockchains. Check out our blog to see more examples.
  • You can also check DEX data on our blockchain explorer, here, here, and here.

Top DEX Protocols

The following query returns top DEX protocols based on the number of trades.

{
    ethereum(network: ethereum) {
        dexTrades(options: {limit: 100, desc: "trades"}){
            protocol
            started: minimum(of: date)
            trades: count
        }
    }
}

Result

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "protocol": "Uniswap v2",
          "started": "2020-05-05",
          "trades": 24836192
        },
        {
          "protocol": "IDEX",
          "started": "2017-09-27",
          "trades": 11819652
        },
        {
          "protocol": "EtherDelta",
          "started": "2016-07-09",
          "trades": 7441188
        },
        {
          "protocol": "Uniswap",
          "started": "2018-10-27",
          "trades": 4011690
        },
        {
          "protocol": "Zerox Exchange v2",
          "started": "2018-09-18",
          "trades": 2437436
        },
        ...
        ...
        ...

Latest trades on a DEX Protocol

The following query shows how to get the latest trades for any DEX protocol. In this query, we are using the “Uniswap V2” protocol as an example.

{
    ethereum(network: ethereum) {
        dexTrades(options: {limit: 10, desc: "trades"}, protocol: 
        {is: "Uniswap v2"}) {
            protocol
            buyCurrency {
                symbol
                address
            }
            buyAmount
            sellCurrency {
                symbol
                address
            }
            sellAmount
            trades: count
        }
    }
}

Result

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "protocol": "Uniswap v2",
          "buyCurrency": {
            "symbol": "USDT",
            "address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
          },
          "buyAmount": 1842158918.313746,
          "sellCurrency": {
            "symbol": "WETH",
            "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
          },
          "sellAmount": 4977863.830826052,
          "trades": 379716
        },
        {
          "protocol": "Uniswap v2",
          "buyCurrency": {
            "symbol": "WETH",
            "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
          },
          "buyAmount": 4920992.445139087,
          "sellCurrency": {
            "symbol": "USDT",
            "address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
          },
          "sellAmount": 1831207530.946584,
          "trades": 368346
        },
        ...
        ...
        ...

Latest trades of a specific currency pair on a DEX protocol

Now, if you want the latest trades for a specific currency pair, use the following query. We are getting the latest trade for USDT — WETH currency pair on Uniswap protocol (V2) in this query.

As you can see, you need to pass the smart contract address for the tokens to get the latest trades for them.

Here we are passing 0xdac17f958d2ee523a2206206994597c13d831ec7 for buyCurrency which is the USDT token’s address. And, 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 for sellCurrency which is the WETH token’s address.

{
    ethereum(network: ethereum) {
        dexTrades(options: {limit: 10, desc: ["block.height", "tradeIndex"]},
        protocol: {is: "Uniswap v2"}, 
        buyCurrency: {is: "0xdac17f958d2ee523a2206206994597c13d831ec7"},
        sellCurrency: {is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}) {
            block {
                height
                timestamp {
                iso8601
                }
            }
            transaction {
                hash
            }
            tradeIndex
            buyCurrency {
                symbol
                address
            }
            buyAmount
            sellCurrency {
                symbol
                address
            }
            sellAmount
            maker {
                address
                annotation
            }
            taker {
                address
                annotation
            }
            protocol
            smartContract {
                address {
                address
                annotation
                }
                contractType
            }
        }
    }
}

Result

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "block": {
            "height": 11067734,
            "timestamp": {
              "iso8601": "2020-10-16T15:21:58Z"
            }
          },
          "transaction": {
            "hash": "0xa71a2c54355fb04b93d891a0750d4006238d8960da19db39e33d15c43debe08b"
          },
          "tradeIndex": "8",
          "buyCurrency": {
            "symbol": "USDT",
            "address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
          },
          "buyAmount": 472.192504,
          "sellCurrency": {
            "symbol": "WETH",
            "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
          },
          "sellAmount": 1.2889359336599604,
          "maker": {
            "address": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
            "annotation": "Router 2, Uniswap, Router"
          },
          "taker": {
            "address": "0x0b7ded783106803dc03197028d26c86e548910a5",
            "annotation": null
          },
          "protocol": "Uniswap v2",
          "smartContract": {
            "address": {
              "address": "0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852",
              "annotation": null
            },
            "contractType": "DEX"
          }
        },
       ...
       ...
       ...

OHLC data for currency pair on DEX

Open-high-low-close(also OHLC) data used to create an OHLC chart, which illustrates movements in the price of a financial instrument over time.

Using the following query, you can get the OHLC data for any currency pair for any DEX protocol.

In this query, we are getting USDT-WETH currency pair’s OHLC data for 5 minutes interval.

We support the following time interval units. 

  • second
  • minute
  • hour
  • day
  • month
  • year

Using the above units, you can create any type of chart candles. For example

  • 5-minute interval
timeInterval {  
        minute(count: 5)  
}
  • 1-day interval 
timeInterval {  
        day(count: 1)  
}

Here is the query to get 5-minute OHLC data for the USDT-WETH currency pair on Uniswap protocol.

{
  ethereum(network: ethereum) {
        dexTrades(options: {limit: 10, asc: "timeInterval.minute"}, 
        protocol: {is: "Uniswap v2"}, 
        date: {is: "2020-10-10"}
        buyCurrency: {is: "0xdac17f958d2ee523a2206206994597c13d831ec7"}, 
        sellCurrency: {is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}) {
            timeInterval {
                minute(count: 5)
            }
            buyCurrency {
                symbol
                address
            }
            buyAmount
            sellCurrency {
                symbol
                address
            }
            sellAmount
            trades: count
            maximum_price: price(calculate: maximum)
            minimum_price: price(calculate: minimum)
            open_price: minimum(of: block, get: price)
            close_price: maximum(of: block, get: price)
        }
    }
}

Result

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "timeInterval": {
            "minute": "2020-10-10 00:00:00"
          },
          "buyCurrency": {
            "symbol": "USDT",
            "address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
          },
          "buyAmount": 15094.490436,
          "sellCurrency": {
            "symbol": "WETH",
            "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
          },
          "sellAmount": 41.44903367094046,
          "trades": 15,
          "maximum_price": 0.002746776341103444,
          "minimum_price": 0.002737048849470252,
          "open_price": "0.002738970885840717",
          "close_price": "0.002746681535948033"
        },
        {
          "timeInterval": {
            "minute": "2020-10-10 00:05:00"
          },
          "buyCurrency": {
            "symbol": "USDT",
            "address": "0xdac17f958d2ee523a2206206994597c13d831ec7"
          },
          "buyAmount": 20978.805418,
          "sellCurrency": {
            "symbol": "WETH",
            "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
          },
          "sellAmount": 57.611814558084596,
          "trades": 25,
          "maximum_price": 0.002746774536198633,
          "minimum_price": 0.002738320503436713,
          "open_price": "0.002739092079021605",
          "close_price": "0.002746774536198633"
        },
        ...
        ...
        ...

Monthly DEX trading volume for a specific Token

The following query gives us aggregated trading volume for a specific token on all DEX protocols. For example, in the query below, we are getting the trading volume for USDT token on all DEXs.

Note: We are passing the USDT token’s contract address, 0xdac17f958d2ee523a2206206994597c13d831ec7 to get the data in the query below. Our queries do not recognize token names. They only recognize smart contract addresses. So if you want to get trading volume for any other token, use the contract address for that token.

{
    ethereum(network: ethereum) {
        dexTrades(options: {asc: "date.date"}, 
        buyOrSellCurrency: {is: "0xdac17f958d2ee523a2206206994597c13d831ec7"}) {
            date {
                date(format: "%Y-%m")
            }
            amount
            currency {
                symbol
            }
        }
    }
}

Result

{
  "data": {
    "ethereum": {
      "dexTrades": [
        {
          "date": {
            "date": "2018-08"
          },
          "amount": 195789.099416,
          "currency": {
            "symbol": "USDT"
          }
        },
        {
          "date": {
            "date": "2018-09"
          },
          "amount": 304211.822172,
          "currency": {
            "symbol": "USDT"
          }
        },
        ...
        ...
        ...

If you have any questions or need help with your blockchain investigation, just hop on our Telegram channel. Also, let us know if you are looking for blockchain data APIs.

You might also be interested in:

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.