subgraphs > Developing > Creating > Installation du Graph CLI

Installation du Graph CLI

Reading time: 4 min

In order to use your subgraph on The Graph's decentralized network, you will need to create an API key in Subgraph Studio. It is recommended that you add signal to your subgraph with at least 3,000 GRT to attract 2-3 Indexers. To learn more about signaling, check out curating.

Aperçu

Lien vers cette section

The Graph CLI is a command-line interface that facilitates developers' commands for The Graph. It processes a subgraph manifest and compiles the mappings to create the files you will need to deploy the subgraph to Subgraph Studio and the network.

Installation du Graph CLI

Lien vers cette section

The Graph CLI is written in TypeScript, and you must have node and either npm or yarn installed to use it. Check for the most recent CLI version.

Sur votre machine locale, exécutez l'une des commandes suivantes :

npm install -g @graphprotocol/graph-cli@latest
npm install -g @graphprotocol/graph-cli

The graph init command can be used to set up a new subgraph project, either from an existing contract or from an example subgraph. If you already have a smart contract deployed to your preferred network, you can bootstrap a new subgraph from that contract to get started.

Créer un subgraph

Lien vers cette section

À partir d'un contrat existant

Lien vers cette section

La commande suivante crée un subgraph qui indexe tous les événements d'un contrat existant :

graph init \
--product subgraph-studio
--from-contract <CONTRACT_ADDRESS> \
[--network <ETHEREUM_NETWORK>] \
[--abi <FILE>] \
<SUBGRAPH_SLUG> [<DIRECTORY>]
  • La commande tente de récupérer l'ABI du contrat depuis Etherscan.

    • Graph CLI repose sur un endpoint RPC public. Bien que des échecs occasionnels soient attendus, les réessais résolvent généralement ce problème. Si les échecs persistent, envisagez d'utiliser un ABI local.
  • Si certains arguments optionnels manquent, il vous guide à travers un formulaire interactif.

  • The <SUBGRAPH_SLUG> is the ID of your subgraph in Subgraph Studio. It can be found on your subgraph details page.

À partir d'un exemple de subgraph

Lien vers cette section

La commande suivante initialise un nouveau projet à partir d'un exemple de subgraph :

graph init <SUBGRAPH_SLUG> --from-example=example-subgraph
  • The example subgraph is based on the Gravity contract by Dani Grant, which manages user avatars and emits NewGravatar or UpdateGravatar events whenever avatars are created or updated.

  • The subgraph handles these events by writing Gravatar entities to the Graph Node store and ensuring these are updated according to the events.

Add New dataSources to an Existing Subgraph

Lien vers cette section

dataSources are key components of subgraphs. They define the sources of data that the subgraph indexes and processes. A dataSource specifies which smart contract to listen to, which events to process, and how to handle them.

Recent versions of the Graph CLI supports adding new dataSources to an existing subgraph through the graph add command:

graph add <address> [<subgraph-manifest default: "./subgraph.yaml">]
Options:
--abi <path> Path to the contract ABI (default: download from Etherscan)
--contract-name Name of the contract (default: Contract)
--merge-entities Whether to merge entities with the same name (default: false)
--network-file <path> Networks config file path (default: "./networks.json")

Spécificités⁠

Lien vers cette section

The graph add command will fetch the ABI from Etherscan (unless an ABI path is specified with the --abi option) and creates a new dataSource, similar to how the graph init command creates a dataSource --from-contract, updating the schema and mappings accordingly. This allows you to index implementation contracts from their proxy contracts.

  • The --merge-entities option identifies how the developer would like to handle entity and event name conflicts:

    • If true: the new dataSource should use existing eventHandlers & entities.

    • If false: a new entity & event handler should be created with ${dataSourceName}{EventName}.

  • The contract address will be written to the networks.json for the relevant network.

Note: When using the interactive CLI, after successfully running graph init, you'll be prompted to add a new dataSource.

Récupération des ABIs

Lien vers cette section

Le(s) fichier(s) ABI doivent correspondre à votre(vos) contrat(s). Il existe plusieurs façons d'obtenir des fichiers ABI :

  • Si vous construisez votre propre projet, vous aurez probablement accès à vos ABI les plus récents.
  • If you are building a subgraph for a public project, you can download that project to your computer and get the ABI by using npx hardhat compile or using solc to compile.
  • You can also find the ABI on Etherscan, but this isn't always reliable, as the ABI that is uploaded there may be out of date. Make sure you have the right ABI, otherwise running your subgraph will fail.

Versions disponibles de SpecVersion

Lien vers cette section
VersionNotes de version
1.2.0Added support for Indexed Argument Filtering & declared eth_call
1.1.0Supports Timeseries & Aggregations. Added support for type Int8 for id.
1.0.0Supports indexerHints feature to prune subgraphs
0.0.9Supports endBlock feature
0.0.8Added support for polling Block Handlers and Initialisation Handlers.
0.0.7Added support for File Data Sources.
0.0.6Supports fast Proof of Indexing calculation variant.
0.0.5Ajout de la prise en charge des gestionnaires d'événement ayant accès aux reçus de transactions.
0.0.4Ajout de la prise en charge du management des fonctionnalités de subgraph.
Modifier une page

Précédente
Starting Your Subgraph
Suivante
Subgraph Manifest
Modifier une page