7

What I am trying to do is this :-

Given an pair (say DAI/ETH pair), i want to display the latest 10-15 trades (buy, sell, add to pool and remove from pool) and also display a graph of price for that pair.

How can i get the relevent data using uniswap js sdk/api?

1
  • love your nick name =-D
    – Klesun
    Nov 13, 2020 at 12:49

1 Answer 1

16

You can use the Uniswap TheGraph for this! For example a query to get the last 10 swaps from the DAI/ETH pair would be:

{
    swaps(first: 10, where: { pair: "0xa478c2975ab1ea89e8196811f51a7b7ade33eb11" } orderBy: timestamp, orderDirection: desc) {
      transaction {
        id
        timestamp
      }
      id
      pair {
        token0 {
          id
          symbol
        }
        token1 {
          id
          symbol
        }
      }
      amount0In
      amount0Out
      amount1In
      amount1Out
      amountUSD
      to
    }
}

You can play around with queries at: https://thegraph.com/explorer/subgraph/uniswap/uniswap-v2. Integration into the frontend would happen with something like Apollo Client.

In fact this is also what the Uniswap Info is doing. It's open-source, so look around for example queries and how to use them.

For more information on how TheGraph and queries work, I just posted a blog-post about it: https://soliditydeveloper.com/thegraph.

10
  • Thanks a lot. I'll check them all. You are a life saver. Sep 9, 2020 at 9:31
  • Is it possible to get the marginal price (eg. the price after the trade)?
    – wanheda
    Jan 8, 2021 at 20:16
  • 1
    @wanheda What exactly do you want to achieve? You can use time-travel queries with TheGraph, see thegraph.com/docs/graphql-api#queries under time-travel. But there's also built-in oracle functionality: uniswap.org/docs/v2/core-concepts/oracles. Jan 10, 2021 at 4:31
  • @Markus-soliditydeveloper.com praise to you! The time-travel functionality was what I needed (to get the pair statistics at the block of the trade). Note that it still won't be exact as there can be many trades within a block, making the price (and even the constant) deviate.
    – wanheda
    Jan 13, 2021 at 19:54
  • Hi @markus, I'm fairly new to the graph search for uniswap. So, sorry for sounding like a noob. But, Can you guide me how could we find the token pair address programatically? Thanks for the help! Jun 19, 2021 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.