Subgraph
Developer documentation for the Rare Protocol Subgraph on The Graph.
Introduction
What is it?
The Graph is a decentralized protocol for indexing and querying data from blockchains. It allows developers to query data that would otherwise be cumbersome or costly to fetch directly from smart contracts. A subgraph defines the specific data The Graph will index from the blockchain and how it will store it. Once deployed, a subgraph provides a convenient GraphQL endpoint for data queries.
This guide outlines how to interact with Rare Protocol's Staking Subgraph, focusing on data indexed from several contracts, including the staking pool creation factory, and reward accumulators.
What is it for?
You should use the Rare Protocol Subgraph to query the current state of on-chain staking activity, such as pools, stakes, and rewards. It is not good for past data, user data, timeseries, analytics, or metadata (how to use the API). It also does not include transactions occurring on the bazaar marketplace or NFT contracts.
Prerequisites
Before you begin, you should have:
Familiarity with GraphQL and TypeScript.
Node.js and TypeScript installed on your system.
Accessing the Rare Protocol Subgraph on Mainnet
The following section covers the official Rare Protocol Subgraph, and is best for querying pools, staking events, tokens from SuperRare users, and more. For data on SuperRare marketplace events, please see the SuperRare subgraph below. Details, including schema and available queries on the Rare Protocol Subgraph, are available at the following URL
This next url is your gateway to querying staking data indexed by the Rare Protocol subgraph.
API keys
are required to query data from Subgraphs. You can create/manage API keys after signing up for The Graph's Subgraph Studio. https://thegraph.com/studio/apikeys/
Accessing the Subgraph on Sepolia
Access our testnet subgraph at the following URL
https://api.studio.thegraph.com/query/51818/rare-protocol-sepolia/v0.0.1
Schema Documentation
The schema documents the data structure for queries, emphasizing Pools
and Stakes
as key entities.
Once you have an api key, explore the schema and test queries using Apollo's environment:
https://studio.apollographql.com/
Getting Started
Step 1: Install Dependencies
First, install Apollo Client and GraphQL: Open your terminal and run the following command in your project directory:
This will install Apollo Client and the GraphQL library, which are recommended to connect and interact with the GraphQL server.
Step 2: Set Up Apollo Client
Create a new TypeScript module named client.ts
. This module will set up the Apollo Client. Insert the following code:
Replace ${YOUR_API_KEY}
with your actual API key.
This code initializes an instance of Apollo Client, pointing to the GraphQL API endpoint and setting up a cache with InMemoryCache.
Step 3: Define the Query and Types Next, define the GraphQL query and the corresponding TypeScript types for the response. This helps with type-safety and autocompletion in TypeScript. Add the following to your client.ts
:
Step 4: Execute the Query
Now, execute a query using the Apollo Client. We will query for a list of pool data sorted by total $RARE staked. Append this code to your client.ts
:
This code sends a query to the subgraph and logs the response. The query method takes an object with two properties: query and variables. The query is defined using the gql template literal tag, and the variables object provides the specific target addresses.
Step 5: Run Your Code
To run your code, simply execute the client.ts script using Node.js or any TypeScript execution environment. You should see the query result output to your console.
With this you've successfully set up your environment and executed a query to our GraphQL API using Apollo Client in TypeScript. This basic setup can be expanded to include more complex queries as needed for your application.
Accessing the SuperRare Subgraph on Mainnet
Thanks to user ethafrica.eth, a community-built subgraph offering data on marketplace events is available. It is currently only available for mainnet.
Last updated