4 minutos
Como instalar o Graph CLI
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.
Visão geral
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.
Como Começar
Como instalar o Graph CLI
A CLI do The Graph é escrita em TypeScript, e é necessário ter o node
, e npm
ou yarn
, instalados para usá-la. Verifique a versão mais recente da CLI.
Execute um dos seguintes comandos na sua máquina local:
Uso do npm
1npm install -g @graphprotocol/graph-cli@latest
Uso do yarn
1yarn 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.
Crie um Subgraph
De um Contrato Existente
The following command creates a Subgraph that indexes all events of an existing contract:
1graph init \2 --product subgraph-studio3 --from-contract <CONTRACT_ADDRESS> \4 [--network <ETHEREUM_NETWORK>] \5 [--abi <FILE>] \6 <SUBGRAPH_SLUG> [<DIRECTORY>]
-
O comando tenta resgatar o contrato da ABI do Etherscan.
- A CLI do The Graph depende de um endpoint público de RPC. Enquanto falhas ocasionais são de se esperar, basta tentar de novo para resolver. Se as falhas persistirem, considere usar uma ABI local.
-
Se faltar algum dos argumentos opcionais, você será guiado para um formulário interativo.
-
The
<SUBGRAPH_SLUG>
is the ID of your Subgraph in Subgraph Studio. It can be found on your Subgraph details page.
De um Exemplo de Subgraph
The following command initializes a new project from an example Subgraph:
1graph 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
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.
Como Adicionar Novos dataSources
para um Subgraph Existente
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:
1graph add <address> [<subgraph-manifest default: "./subgraph.yaml">]23Opções:45 --abi <path> Caminho à ABI do contrato (padrão: baixar do Etherscan)6 --contract-name Nome do contrato (padrão: Contract)7 --merge-entities Se fundir ou não entidades com o mesmo nome (padrão: false)8 --network-file <path> Caminho ao arquivo de configuração das redes (padrão: "./networks.json")
Especificações
O comando graph add
pegará a ABI do Etherscan (a não ser que um local de ABI seja especificado com a opção —abi), e criará um novo dataSource
da mesma maneira que o comando graph init
cria um dataSource
--from-contract
, assim atualizando o schema e os mapeamentos de acordo.
-
A opção
--merge entities
identifica como o programador gostaria de lidar com conflitos de nome ementity
eevent
:-
Se for
true
: o novodataSource
deve usareventHandlers
eentities
existentes. -
Se for
false
: um novo handler deentity
eevent
deve ser criado com${dataSourceName}{EventName}
.
-
-
O
address
(endereço de contrato) será escrito nonetworks.json
para a rede relevante.
Observação: Quando usar a CLI interativa, após executar o graph init
com êxito, você receberá uma solicitação para adicionar um novo dataSource
.
Como Obter as ABIs
Os arquivos da ABI devem combinar com o(s) seu(s) contrato(s). Há algumas maneiras de obter estes arquivos:
- Caso construa o seu próprio projeto, provavelmente terá acesso às suas ABIs mais recentes.
- 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 usingsolc
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.