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. Only mainnet is on The Graph’s decentralized network.
The Synthetix subgraph is available on the hosted service on mainnet, optimism, kovan, and optimism-kovan
Note that data queried from the Optimism networks may be incomplete due the regenesis on 11/11/21.
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.
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 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.
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
.