sushiswap-subgraph icon indicating copy to clipboard operation
sushiswap-subgraph copied to clipboard

Example query to grab token price on a certain date?

Open classicrob opened this issue 3 years ago • 2 comments

Hi! I was wondering if someone could write an example query to grab a specific token's price on a certain day? I'm struggling to figure it out by looking through the schema and I'm not yet super familiar with GraphQL. Thank you!

I recognize that token price fluctuates a lot throughout the day, so maybe an average price, or a price at a specific time stamp?

classicrob avatar Sep 12 '21 15:09 classicrob

https://medium.com/coinmonks/how-to-use-coingeckos-free-api-to-get-digital-asset-data-just-the-basics-d46f232926cb

bcstryker avatar Nov 20 '21 06:11 bcstryker

@classicrob try this:

{ 
  token(id: "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2") {
    dayData(where: {date: 1632096000}) {
      date
      priceUSD
    }
  }
}

Where id is the token address (in lowercase), and the date timestamp is the start of any day 00:00 UTC

Also you can do something like this to get multiple days in one query:

{ 
  token(id: "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2") {
    dayData(where: {date_in: [1632096000, 1634688000, 1618876800]}) {
      date
      priceUSD
    }
  }
}

And if you want to only grab the prices for each day in a certain range do such:

{ 
  token(id: "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2") {
    dayData(where: {date_gte: 1618876800, date_lt: 1619308800}) {
      date
      priceUSD
    }
  }
}

jiro-ono avatar Nov 20 '21 08:11 jiro-ono