Synthetixio / synthetix-subgraph

All sources for the handful of Synthetix subgraphs indexed by The Graph
https://docs.synthetix.io/historical-data/#subgraphs
MIT License
62 stars 63 forks source link
defi ethereum graphql synthetix

Synthetix Subgraph

Overview

The Graph is a decentralized protocol for indexing and querying data on the Ethereum blockchain. Subgraphs define the data The Graph will index and how it will be stored. This data is then provided via a GraphQL API.

The Graph currently consists of a hosted service and a decentralized network. In the future, the hosted service will be gradually sunset after the decentralized network achieves feature parity.

Synthetix currently maintains one official subgraph per network on the hosted services.

Hosted Service

The Synthetix subgraph is available on the hosted service on mainnet, optimism, kovan, and optimism-kovan

⚠️ Using subgraphs with the hosted service may introduce breaking changes. The Synthetix subgraphs are under active development. Because The Graph does not currently support pinning subgraph versions on the hosted service, these subgraphs should be used with caution.

Note that data queried from the Optimism networks may be incomplete due the regenesis on 11/11/21.

The Graph Network

The subgraph can also be found on The Graph’s decentralized network here.

The decentralized network supports pinning versions. Subgraphs on the decentralized network can only query data on mainnet. The latest version available on the decentralized network may not always be synchronized with the latest version on the hosted service.

Usage

Query the hosted service directly

This code snippet demonstrates how to retrieve all exchanges that occured in the last 24 hours:

(async () => {
  const ts = Math.floor(Date.now() / 1e3);
  const oneDayAgo = ts - 3600 * 24;
  const body = JSON.stringify({
    query: `{
      synthExchanges(
        orderBy:timestamp,
        orderDirection:desc,
        where:{timestamp_gt: ${oneDayAgo}}
      )
      {
        fromAmount
        fromAmountInUSD
        fromCurrencyKey
        toCurrencyKey
        block
        timestamp
        toAddress
        toAmount
        toAmountInUSD
        feesInUSD
      }
    }`,
    variables: null,
  });

  const response = await fetch('https://api.thegraph.com/subgraphs/name/synthetixio-team/mainnet-main', {
    method: 'POST',
    body,
  });

  const json = await response.json();
  const { synthExchanges } = json.data;
  // ...
  console.log(synthExchanges);
})();

Explore all of the entities available in the subgraph in the playground.

Due to limitation imposed by The Graph, only 1,000 results will be returned from the query above. Review The Graph's documentation on pagination for more information.

@synthetixio/queries

@synthetixio/queries is a JavaScript library that retrieves Synthetix’s data from The Graph. The library provides TypeScript support for the returned data, automatically handles pagination, and allows you to subscribe to real-time updates.

Subgraph Entities

Find detailed documentation for the more commonly accessed subgraph entities below.

Candle

The Candle entity stores data pertaining to the price of the SNX token and each synth, as reported by Chainlink oracles, over various time periods. This contains all of the data necessary to generate candlestick charts.

SynthExchange

Each time a synth is exchanged, a new SynthExchange entity is created.

Total

The Total entities aggregate SynthExchange data and futures trades over time. Note that we are only collecting a single entity to record all-time totals for each combination of bucketMagnitude and synth filters. (i.e. All totals with a period of 0 have a timestamp of 0.)

SnxHolder

This entity represents each individual holder of SNX tokens.

Build and Deploy

To build and deploy the subgraphs, run npm run deploy for a CLI. You will have the option to update the Synthetix contract ABIs, build the updated subgraph, and deploy to the hosted service and/or decentralized network.

The CLI automatically generates the main subgraph, which is composed of the other subgraph in the subgraphs directory. You can also use the CLI to deploy the component subgraphs to the hosted service for faster development and testing.

All of the prompts in the CLI can be provided via options. For more information, run npm run deploy -- --help.