6

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 '20 at 12:49
13

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.

4
  • Thanks a lot. I'll check them all. You are a life saver. – Hentai Ousama Sep 9 '20 at 9:31
  • Is it possible to get the marginal price (eg. the price after the trade)? – wanheda Jan 8 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. – Markus - soliditydeveloper.com Jan 10 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 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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