Graph CLI'ı Yükleyin
Reading time: 4 min
In order to use your subgraph on The Graph's decentralized network, you will need to in . 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 .
The is a command-line interface that facilitates developers' commands for The Graph. It processes a and compiles the to create the files you will need to deploy the subgraph to and the network.
The Graph CLI is written in TypeScript, and you must have node
and either npm
or yarn
installed to use it. Check for the CLI version.
Yerel makinenizde aşağıdaki komutlardan birini çalıştırın:
npm install -g @graphprotocol/graph-cli@latest
yarn global add @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.
Aşağıdaki komut, mevcut bir sözleşmenin tüm olaylarını endeksleyen bir subgraph oluşturur:
graph init \--product subgraph-studio--from-contract <CONTRACT_ADDRESS> \[--network <ETHEREUM_NETWORK>] \[--abi <FILE>] \<SUBGRAPH_SLUG> [<DIRECTORY>]
-
Bu komut, sözleşme ABI'sini Etherscan’den almaya çalışır.
- Graph CLI, bir umumi RPC uç noktasına bağlıdır. Ara sıra başarısızlık beklenebilir, ancak genellikle yeniden denemeler sorunu çözer. Sorunlar devam ederse, yerel bir ABI kullanmayı düşünün.
-
İsteğe bağlı argümanlar eksikse, komut sizi bir etkileşimli forma yönlendirir.
-
The
<SUBGRAPH_SLUG>
is the ID of your subgraph in . It can be found on your subgraph details page.
Aşağıdaki komut, örnek bir subgraph'ten yeni bir proje ilklendirir:
graph init <SUBGRAPH_SLUG> --from-example=example-subgraph
-
The is based on the Gravity contract by Dani Grant, which manages user avatars and emits
NewGravatar
orUpdateGravatar
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.
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")
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 handleentity
andevent
name conflicts:-
If
true
: the newdataSource
should use existingeventHandlers
&entities
. -
If
false
: a newentity
&event
handler should be created with${dataSourceName}{EventName}
.
-
-
The contract
address
will be written to thenetworks.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
.
ABI dosya(lar)ı sözleşme(ler) inizle uygun olmalıdır. ABI dosyalarını edinmek için birkaç yol vardır:
- Kendi projenizi oluşturuyorsanız, muhtemelen en güncel ABI'lerinize erişiminiz olacaktır.
- If you are building a subgraph for a public project, you can download that project to your computer and get the ABI by using or using
solc
to compile. - You can also find the ABI on , 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.